From 9371bfefe93e2018dc77729c38f970ec0ee96aa3 Mon Sep 17 00:00:00 2001 From: Meni Yakove Date: Sun, 2 Jun 2024 12:47:05 +0300 Subject: [PATCH 1/2] build container: checkout the right branch if PR is merged --- webhook_server_container/libs/github_api.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/webhook_server_container/libs/github_api.py b/webhook_server_container/libs/github_api.py index d62d6b5f..7dd74b0a 100644 --- a/webhook_server_container/libs/github_api.py +++ b/webhook_server_container/libs/github_api.py @@ -1489,7 +1489,7 @@ def _run_build_container( build_cmd += f" && podman push --creds {repository_creds} {_container_repository_and_tag}" podman_build_cmd = f"podman build {build_cmd}" - rc, out, err = self._run_in_container(command=podman_build_cmd, ignore_pull_request=is_merged is not None) + rc, out, err = self._run_in_container(command=podman_build_cmd, is_merged=is_merged) output = { "title": "Build container", "summary": "", @@ -1642,7 +1642,7 @@ def set_check_run_status(self, check_run, status=None, conclusion=None, output=N self.repository_by_github_app.create_check_run(**kwargs) return f"Done setting check run status: {kwargs}" - def _run_in_container(self, command, env=None, ignore_pull_request=False): + def _run_in_container(self, command, env=None, is_merged=False): podman_base_cmd = ( "podman run --network=host --privileged -v /tmp/containers:/var/lib/containers/:Z " f"--rm {env if env else ''} --entrypoint bash quay.io/myakove/github-webhook-server -c" @@ -1659,8 +1659,12 @@ def _run_in_container(self, command, env=None, ignore_pull_request=False): clone_base_cmd += " && git config --local --add remote.origin.fetch +refs/pull/*/head:refs/remotes/origin/pr/*" clone_base_cmd += " && git remote update >/dev/null 2>&1" + # Checkout the branch if pull request is merged + if is_merged: + clone_base_cmd += f" && git checkout {self.pull_request_branch}" + # Checkout the pull request - if self.pull_request and not ignore_pull_request: + else: clone_base_cmd += f" && git checkout origin/pr/{self.pull_request.number}" # final podman command From da2ffcb3a7ac5813c77c983b5b6a6097c5e4dd6a Mon Sep 17 00:00:00 2001 From: Meni Yakove Date: Sun, 2 Jun 2024 13:40:57 +0300 Subject: [PATCH 2/2] no-cache for merged PRs --- webhook_server_container/libs/github_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webhook_server_container/libs/github_api.py b/webhook_server_container/libs/github_api.py index 7dd74b0a..ecc85f58 100644 --- a/webhook_server_container/libs/github_api.py +++ b/webhook_server_container/libs/github_api.py @@ -1474,7 +1474,7 @@ def _run_build_container( tag=tag, is_merged=is_merged, ) - no_cache = " --no-cache" if (tag or self.container_tag == _container_repository_and_tag.split(":")[-1]) else "" + no_cache = " --no-cache" if is_merged else "" build_cmd = f"--network=host {no_cache} -f {self.container_repo_dir}/{self.dockerfile} . -t {_container_repository_and_tag}" if self.container_build_args: