-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement github release runner for github 'release' events where act…
…ion is 'published' When release event action is 'published' and job definition listen 'release' events It will install release assets file from <release_files> to <release_base_dir>/<event release tag_name>/ for each <file> from <release_files> download <repository html_url>/<event release tag_name>/<file> install downloaded file to <release_base_dir>/<event release tag_name>/ Example: # job definition: { "https://github.com/orga/repo": { "release": { "runner_lib": "release_example", "release_files": ["README.md", "package.json"], "release_base_dir": "/repo" } } } # log INFO:root:loaded lib: context.github_release INFO:root:create new job for https://github.com/orga/repo, event release INFO:root:loaded lib: runner.release_example INFO:root:loaded lib: job_payload.null INFO:root:download https://github.com/orga/repo/releases/download/v1.0.42/README.md ... INFO:root:installed /repo/1/1.0.42/README.md INFO:root:download https://github.com/orga/repo/releases/download/v1.0.42/package.json ... INFO:root:installed /repo/1/1.0.42/package.json INFO:root:job status: 200, response: { "message": "release v1.0.42 https://github.com/orga/repo, event: release, deployed file: {'/repo/1/1.0.42/README.md', '/repo/1/1.0.42/package.json'}, current link updated:...", "description": { "action": "published", "name": "https://github.com/orga/repo", "tag_name": "v1.0.42", "draft": false, "prerelease": false, "target_commitish": "main", "html_url": "https://github.com/orga/repo" } }
- Loading branch information
Showing
7 changed files
with
311 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from runner.release import Runner as ReleaseRunner | ||
|
||
|
||
class Runner(ReleaseRunner): | ||
def extra_action(self, job): | ||
return " current link updated:..." | ||
|
||
def release_dir(self, job): | ||
""" | ||
define release dir from event tag name value | ||
examples: | ||
v1.9.5 -> <release_base_dir>/1/1.9.5/ | ||
v2.0.0 -> <release_base_dir>/2/2.0.0/ | ||
""" | ||
tag_name = job.context.tag_name.lstrip("v") | ||
api_version = tag_name.split(".")[0] | ||
return "%s/%s/%s" % (job.release_base_dir, api_version, tag_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from context.github_abstract import GitHubAbstract | ||
|
||
|
||
class Context(GitHubAbstract): | ||
""" | ||
Context class for github release events | ||
""" | ||
|
||
@property | ||
def event(self): | ||
return 'release' | ||
|
||
@property | ||
def action(self): | ||
return self.payload['action'] | ||
|
||
@property | ||
def release(self): | ||
return self.payload['release'] | ||
|
||
@property | ||
def download_url(self): | ||
return "%s/releases/download/%s" % (self.html_url, self.tag_name) | ||
|
||
@property | ||
def release_name(self): | ||
return self.release['name'] | ||
|
||
@property | ||
def tag_name(self): | ||
return self.release['tag_name'] | ||
|
||
@property | ||
def draft(self): | ||
return self.release['draft'] | ||
|
||
@property | ||
def prerelease(self): | ||
return self.release['prerelease'] | ||
|
||
@property | ||
def target_commitish(self): | ||
return self.release['target_commitish'] | ||
|
||
@property | ||
def repository(self): | ||
return self.payload['repository'] | ||
|
||
@property | ||
def html_url(self): | ||
return self.repository['html_url'] | ||
|
||
@property | ||
def description(self): | ||
return { | ||
'action': self.action, | ||
'name': self.name, | ||
'tag_name': self.tag_name, | ||
'draft': self.draft, | ||
'prerelease': self.prerelease, | ||
'target_commitish': self.target_commitish, | ||
'html_url': self.html_url, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import logging | ||
import os | ||
|
||
import requests | ||
|
||
from runner.runner_abstract import RunnerAbstract | ||
|
||
|
||
def download_to(url, dest): | ||
logging.info("download %s ...", url) | ||
r = requests.get(url, allow_redirects=True) | ||
open(dest, 'wb').write(r.content) | ||
logging.info("installed %s", dest) | ||
|
||
|
||
class Runner(RunnerAbstract): | ||
def download_url(self, job, name): | ||
return "%s/%s" % (job.context.download_url, name) | ||
|
||
def extra_action(self, job): | ||
""" | ||
extra actions to be run after asset files installed | ||
return message | ||
""" | ||
return "" | ||
|
||
def release_dir(self, job): | ||
""" | ||
default destination dir for release files (<release_base_dir>/<event release tag_name>/) | ||
""" | ||
return "%s/%s" % (job.release_base_dir, job.context.tag_name) | ||
|
||
def execute(self, job): | ||
""" | ||
runner example: | ||
- download assets files from DEPLOY_FILES list | ||
- install files to release_dir/ | ||
""" | ||
if job.context.action != "published": | ||
self._data = { | ||
"message": f'release action {job.context.action} {job.context.name} nothing to do' | ||
} | ||
return | ||
if not job.release_base_dir: | ||
self._data = { | ||
"message": f'release action {job.context.action} {job.context.name} skipped (undefined release_base_dir)' | ||
} | ||
return | ||
dest_dir = self.release_dir(job) | ||
os.makedirs(dest_dir, exist_ok=True) | ||
deployed_files = set() | ||
for file in job.release_files: | ||
dest = f"{dest_dir}/{file}" | ||
download_to(self.download_url(job, file), dest) | ||
deployed_files.add(dest) | ||
extra_action_message = self.extra_action(job) | ||
message = f'release {job.context.release_name} {job.context.name}, '\ | ||
f'event: {job.context.event}, '\ | ||
f'deployed file: {deployed_files}' | ||
if extra_action_message: | ||
message = f'{message}, {extra_action_message}' | ||
self._data = {'message': message} |
Oops, something went wrong.