From 6756b91c86578a0a7a20505ace261094de18fb2f Mon Sep 17 00:00:00 2001 From: Meni Yakove Date: Wed, 21 Feb 2024 15:00:02 +0200 Subject: [PATCH] support push new container on each release --- README.md | 1 + example.config.yaml | 1 + webhook_server_container/libs/github_api.py | 18 +++++++++++------- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a30a122c..93f778e1 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,7 @@ Once the PR is merged, the container will be build and push to the repository * `password`: The password for the username * `repository`: the repository to push the container, for example `quay.io/myakove/github-webhook-server` * `tag`: The container tag to use when pushing the container +* `release`: if `true` a new container will be pushed with the release version as the tag ```yaml diff --git a/example.config.yaml b/example.config.yaml index 2d4f810a..89242fc5 100644 --- a/example.config.yaml +++ b/example.config.yaml @@ -47,6 +47,7 @@ repositories: password: repository: tag: + release: true # Push image to registry on new release with release as the tag build-args: # build args to send to podman build command - build-arg1=1 - build-arg2=2 diff --git a/webhook_server_container/libs/github_api.py b/webhook_server_container/libs/github_api.py index 3d94b935..0f0a71d0 100644 --- a/webhook_server_container/libs/github_api.py +++ b/webhook_server_container/libs/github_api.py @@ -284,6 +284,7 @@ def _repo_data_from_config(self): self.container_tag = self.build_and_push_container.get("tag", "latest") self.container_build_args = self.build_and_push_container.get("build-args") self.container_command_args = self.build_and_push_container.get("args") + self.container_release = self.build_and_push_container.get("release") self.auto_verified_and_merged_users = config_data.get( "auto-verified-and-merged-users", repo_data.get("auto-verified-and-merged-users", []) @@ -762,10 +763,13 @@ def process_pull_request_webhook_data(self): def process_push_webhook_data(self): tag = re.search(r"refs/tags/?(.*)", self.hook_data["ref"]) - if tag and self.pypi: + if tag: tag_name = tag.group(1) - self.app.logger.info(f"{self.log_prefix} Processing push for tag: {tag_name}") - self.upload_to_pypi(tag_name=tag_name) + if self.pypi: + self.app.logger.info(f"{self.log_prefix} Processing push for tag: {tag_name}") + self.upload_to_pypi(tag_name=tag_name) + if self.container_release: + self._run_build_container(push=True, set_check=False, tag=tag_name) def process_pull_request_review_webhook_data(self): if not self.pull_request: @@ -1208,11 +1212,11 @@ def _comment_with_details(title, body): """ - def _container_repository_and_tag(self): - tag = self.container_tag if self.pull_request.is_merged() else f"pr-{self.pull_request.number}" + def _container_repository_and_tag(self, tag=None): + tag = tag or self.container_tag if self.pull_request.is_merged() else f"pr-{self.pull_request.number}" return f"{self.container_repository}:{tag}" - def _run_build_container(self, set_check=True, push=False, is_merged=None): + def _run_build_container(self, set_check=True, push=False, is_merged=None, tag=None): if not self.build_and_push_container: return False @@ -1223,7 +1227,7 @@ def _run_build_container(self, set_check=True, push=False, is_merged=None): self.set_container_build_in_progress() - _container_repository_and_tag = self._container_repository_and_tag() + _container_repository_and_tag = self._container_repository_and_tag(tag=tag) build_cmd = f"--network=host -f {self.container_repo_dir}/{self.dockerfile} -t {_container_repository_and_tag}" if self.container_build_args: build_args = [f"--build-arg {b_arg}" for b_arg in self.container_build_args][0]