feat(torrserv_proxy): delegate docker build to GitHub Action #35

Open
hermes wants to merge 4 commits from hermes/brencher:feature/trigger-build-via-action into main_corrupted
2 changed files with 144 additions and 14 deletions
+19 -14
View File
@@ -1,6 +1,7 @@
from enironment import Environment
from steps.docker import DockerComposeBuild, DockerSwarmCheck, DockerSwarmDeploy
from steps.docker import DockerSwarmCheck, DockerSwarmDeploy
from steps.git import GitClone, CheckoutMerged, GitUnmerge
from steps.github_action import GitHubActionTrigger
from steps.shared_state import SharedStateHolderInMemory
clone = GitClone(remotes=[
@@ -18,25 +19,29 @@ state = SharedStateHolderInMemory(unmerge=unmerge)
checkoutMerged = CheckoutMerged(clone,
desired_branches=state,
push=False,
git_user_email="rudolfss13@gmail.com",
git_user_name="brencher_bot"
)
buildDocker = DockerComposeBuild(clone,
docker_repo_username="",
docker_repo_password="",
docker_compose_path="docker-compose.yml",
docker_repo_url="https://registry.rudolf.keenetic.link",
publish=False,
envs=lambda: {
"version": "auto-" + checkoutMerged.progress().version,
},
)
triggerAction = GitHubActionTrigger(
wd=clone,
gitea_base_url="https://gitea.rudolf.keenetic.link",
gitea_token_env="BRENCHER_GITEA_TOKEN",
owner="admin",
repo="reverseproxy",
workflow_file="build.yml",
ref_expr=lambda: checkoutMerged.progress().commit_hash,
inputs=lambda: {
"ref": checkoutMerged.progress().commit_hash,
"image_name": "admin/reverseproxy",
},
poll_interval_seconds=15,
timeout_seconds=1800,
)
deployDocker = DockerSwarmDeploy(
wd=clone,
buildDocker=buildDocker,
buildDocker=None,
stackChecker=dockerSwarmCheck,
envs=lambda: {
"version": "auto-" + checkoutMerged.progress().version
@@ -53,7 +58,7 @@ torrserv_proxy = Environment(
clone,
state,
checkoutMerged,
buildDocker,
triggerAction,
dockerSwarmCheck,
unmerge,
deployDocker,
+125
View File
@@ -0,0 +1,125 @@
import os
import time
import logging
from dataclasses import dataclass
from typing import Any, Callable, Dict, Optional
import requests
from step import AbstractStep
logger = logging.getLogger(__name__)
@dataclass
class GitHubActionTriggerResult:
run_id: int
conclusion: str
image_tag: Optional[str]
class GitHubActionTrigger(AbstractStep[GitHubActionTriggerResult]):
def __init__(self,
wd: AbstractStep,
gitea_base_url: str,
gitea_token_env: str,
owner: str,
repo: str,
workflow_file: str,
ref_expr: Callable[[], str],
inputs: Callable[[], Dict[str, Any]],
poll_interval_seconds: int = 15,
timeout_seconds: int = 1800,
**kwargs: Any):
super().__init__(**kwargs)
self.wd = wd
self.gitea_base_url = gitea_base_url.rstrip("/")
self.gitea_token_env = gitea_token_env
self.owner = owner
self.repo = repo
self.workflow_file = workflow_file
self.ref_expr = ref_expr
self.inputs = inputs
self.poll_interval_seconds = poll_interval_seconds
self.timeout_seconds = timeout_seconds
def _token(self) -> str:
token = os.environ.get(self.gitea_token_env)
if not token:
raise BaseException(f"Environment variable {self.gitea_token_env} is not set")
return token
def _url(self, path: str) -> str:
return f"{self.gitea_base_url}/api/v1{path}"
def _headers(self) -> Dict[str, str]:
return {
"Authorization": f"token {self._token()}",
"Accept": "application/json",
"Content-Type": "application/json",
}
def _dispatch(self, ref: str, inputs: Dict[str, Any]) -> None:
url = self._url(f"/repos/{self.owner}/{self.repo}/actions/workflows/{self.workflow_file}/dispatches")
resp = requests.post(url, headers=self._headers(), json={"ref": ref, "inputs": inputs})
if resp.status_code not in (200, 201, 202, 204):
raise BaseException(f"Failed to dispatch action: {resp.status_code} {resp.text}")
def _latest_run_for_ref(self, ref: str) -> Optional[Dict[str, Any]]:
url = self._url(f"/repos/{self.owner}/{self.repo}/actions/runs")
resp = requests.get(url, headers=self._headers(), params={"ref": ref})
if resp.status_code != 200:
raise BaseException(f"Failed to list runs: {resp.status_code} {resp.text}")
runs = resp.json().get("workflow_runs", [])
for r in runs:
if r.get("head_branch") == ref or r.get("head_sha") == ref:
return r
return None
def _get_run(self, run_id: int) -> Dict[str, Any]:
url = self._url(f"/repos/{self.owner}/{self.repo}/actions/runs/{run_id}")
resp = requests.get(url, headers=self._headers())
if resp.status_code != 200:
raise BaseException(f"Failed to get run {run_id}: {resp.status_code} {resp.text}")
return resp.json()
def progress(self) -> GitHubActionTriggerResult:
ref = self.ref_expr()
inputs = self.inputs()
logger.info(f"Dispatching action {self.workflow_file} on {self.owner}/{self.repo} ref={ref}")
self._dispatch(ref, inputs)
deadline = time.time() + self.timeout_seconds
run = None
while time.time() < deadline:
run = self._latest_run_for_ref(ref)
if run is not None:
break
logger.info(f"Waiting for workflow run to appear...")
time.sleep(self.poll_interval_seconds)
if run is None:
raise BaseException(f"No workflow run appeared within {self.timeout_seconds}s for ref={ref}")
run_id = run["id"]
logger.info(f"Watching workflow run {run_id}")
while time.time() < deadline:
run = self._get_run(run_id)
status = run.get("status")
conclusion = run.get("conclusion")
if status == "completed":
logger.info(f"Workflow run {run_id} completed: {conclusion}")
if conclusion != "success":
raise BaseException(
f"Workflow run {run_id} finished with conclusion={conclusion}: {run.get('html_url')}"
)
return GitHubActionTriggerResult(
run_id=run_id,
conclusion=conclusion,
image_tag=run.get("outputs", {}).get("image_tag") if isinstance(run.get("outputs"), dict) else None,
)
time.sleep(self.poll_interval_seconds)
raise BaseException(f"Workflow run {run_id} did not complete within {self.timeout_seconds}s")