feat(torrserv_proxy): delegate docker build to GitHub Action
Python package / build (flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics, 3.12.3) (pull_request) Successful in 46s
Python package / build (flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics, 3.13) (pull_request) Successful in 38s
Python package / build (flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics, 3.14) (pull_request) Successful in 42s
Python package / build (flake8 . --count --select=E9,F63,F7,F82 --ignore=F824 --show-source --statistics, 3.12.3) (pull_request) Successful in 38s
Python package / build (flake8 . --count --select=E9,F63,F7,F82 --ignore=F824 --show-source --statistics, 3.13) (pull_request) Successful in 37s
Python package / build (flake8 . --count --select=E9,F63,F7,F82 --ignore=F824 --show-source --statistics, 3.14) (pull_request) Successful in 40s
Python package / build (mypy --no-incremental --config-file mypy.ini backend tests, 3.12.3) (pull_request) Failing after 1m46s
Python package / build (mypy --no-incremental --config-file mypy.ini backend tests, 3.13) (pull_request) Failing after 1m36s
Python package / build (mypy --no-incremental --config-file mypy.ini backend tests, 3.14) (pull_request) Failing after 1m55s
Python package / build (pip list, 3.12.3) (pull_request) Successful in 33s
Python package / build (pip list, 3.13) (pull_request) Successful in 30s
Python package / build (pip list, 3.14) (pull_request) Successful in 31s
Python package / build (python -c "import sys; print(f'Python {sys.version}'); print(f'Build: {sys.version_info}'); print(f'Implementation: {sys.implementation}')", 3.12.3) (pull_request) Successful in 36s
Python package / build (python -c "import sys; print(f'Python {sys.version}'); print(f'Build: {sys.version_info}'); print(f'Implementation: {sys.implementation}')", 3.13) (pull_request) Successful in 32s
Python package / build (python -c "import sys; print(f'Python {sys.version}'); print(f'Build: {sys.version_info}'); print(f'Implementation: {sys.implementation}')", 3.14) (pull_request) Successful in 39s
Python package / build (flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics, 3.12.3) (pull_request) Successful in 46s
Python package / build (flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics, 3.13) (pull_request) Successful in 38s
Python package / build (flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics, 3.14) (pull_request) Successful in 42s
Python package / build (flake8 . --count --select=E9,F63,F7,F82 --ignore=F824 --show-source --statistics, 3.12.3) (pull_request) Successful in 38s
Python package / build (flake8 . --count --select=E9,F63,F7,F82 --ignore=F824 --show-source --statistics, 3.13) (pull_request) Successful in 37s
Python package / build (flake8 . --count --select=E9,F63,F7,F82 --ignore=F824 --show-source --statistics, 3.14) (pull_request) Successful in 40s
Python package / build (mypy --no-incremental --config-file mypy.ini backend tests, 3.12.3) (pull_request) Failing after 1m46s
Python package / build (mypy --no-incremental --config-file mypy.ini backend tests, 3.13) (pull_request) Failing after 1m36s
Python package / build (mypy --no-incremental --config-file mypy.ini backend tests, 3.14) (pull_request) Failing after 1m55s
Python package / build (pip list, 3.12.3) (pull_request) Successful in 33s
Python package / build (pip list, 3.13) (pull_request) Successful in 30s
Python package / build (pip list, 3.14) (pull_request) Successful in 31s
Python package / build (python -c "import sys; print(f'Python {sys.version}'); print(f'Build: {sys.version_info}'); print(f'Implementation: {sys.implementation}')", 3.12.3) (pull_request) Successful in 36s
Python package / build (python -c "import sys; print(f'Python {sys.version}'); print(f'Build: {sys.version_info}'); print(f'Implementation: {sys.implementation}')", 3.13) (pull_request) Successful in 32s
Python package / build (python -c "import sys; print(f'Python {sys.version}'); print(f'Build: {sys.version_info}'); print(f'Implementation: {sys.implementation}')", 3.14) (pull_request) Successful in 39s
Replace the inline DockerComposeBuild step in the torrserv_proxy config with a new GitHubActionTrigger step that POSTs to /api/v1/repos/admin/reverseproxy/actions/workflows/build.yml/dispatches on the commit SHA produced by CheckoutMerged, then polls the run status until completion. CheckoutMerged gets a new push_branch_template knob so it pushes the merge result to 'auto-deploy/torrserv_proxy/<commit_sha>' on origin. That branch is what the workflow sees as its ref. DockerSwarmDeploy stays in the pipeline (per Alex: "deploy step leave as is") -- buildDocker is set to None since the image is now produced by the action. New step: backend/steps/github_action.py. Uses BRENCHER_GITEA_TOKEN env var for auth.
This commit is contained in:
@@ -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,31 @@ state = SharedStateHolderInMemory(unmerge=unmerge)
|
||||
|
||||
checkoutMerged = CheckoutMerged(clone,
|
||||
desired_branches=state,
|
||||
push=False,
|
||||
push=True,
|
||||
push_branch_template="auto-deploy/torrserv_proxy/{commit_sha}",
|
||||
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 +60,7 @@ torrserv_proxy = Environment(
|
||||
clone,
|
||||
state,
|
||||
checkoutMerged,
|
||||
buildDocker,
|
||||
triggerAction,
|
||||
dockerSwarmCheck,
|
||||
unmerge,
|
||||
deployDocker,
|
||||
|
||||
+17
-3
@@ -149,6 +149,7 @@ class CheckoutAndMergeResult:
|
||||
remote_branch_name: str | None
|
||||
commit_hash: str
|
||||
version: str
|
||||
push_branch: str | None = None
|
||||
|
||||
|
||||
def _commits_childs(repo: git.Repo) -> Dict[Commit, List[Commit]]:
|
||||
@@ -184,6 +185,7 @@ class CheckoutMerged(AbstractStep[CheckoutAndMergeResult]):
|
||||
git_user_email: str,
|
||||
git_user_name: str,
|
||||
push: bool = True,
|
||||
push_branch_template: str | None = None,
|
||||
**kwargs: Any):
|
||||
super().__init__(**kwargs)
|
||||
self.wd = wd
|
||||
@@ -191,6 +193,7 @@ class CheckoutMerged(AbstractStep[CheckoutAndMergeResult]):
|
||||
self.git_user_email = git_user_email
|
||||
self.git_user_name = git_user_name
|
||||
self.push = push
|
||||
self.push_branch_template = push_branch_template
|
||||
|
||||
def _find_merge_childs(self, tree: Dict[Commit, List[Commit]], commit: Commit) -> List[Commit]:
|
||||
|
||||
@@ -307,18 +310,29 @@ class CheckoutMerged(AbstractStep[CheckoutAndMergeResult]):
|
||||
remote_branch_name = branch_name if remote_name == 'origin' else ref.name
|
||||
break
|
||||
if self.push and remote_branch_name is None:
|
||||
auto_branch_hash = hashlib.sha1(''.join([x.hexsha for x in sorted_commits]).encode()).hexdigest()
|
||||
auto_branch_name = f"auto-{version}"
|
||||
if self.push_branch_template:
|
||||
auto_branch_name = self.push_branch_template.format(
|
||||
commit_sha=repo.head.commit.hexsha,
|
||||
commit_sha_short=repo.head.commit.hexsha[:8],
|
||||
version=version,
|
||||
)
|
||||
else:
|
||||
auto_branch_hash = hashlib.sha1(''.join([x.hexsha for x in sorted_commits]).encode()).hexdigest()
|
||||
auto_branch_name = f"auto-{version}"
|
||||
|
||||
logger.info(f"Pushing {repo.head.commit.hexsha} -> {auto_branch_name}")
|
||||
repo.git.push('-f', 'origin', f"HEAD:refs/heads/{auto_branch_name}")
|
||||
remote_branch_name = auto_branch_name
|
||||
push_branch = auto_branch_name
|
||||
else:
|
||||
push_branch = None
|
||||
|
||||
return CheckoutAndMergeResult(
|
||||
wd=repo_path,
|
||||
remote_branch_name=remote_branch_name,
|
||||
commit_hash=repo.head.commit.hexsha,
|
||||
version=version
|
||||
version=version,
|
||||
push_branch=push_branch,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -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")
|
||||
Reference in New Issue
Block a user