diff --git a/.gitea/workflows/promote-dev.yaml b/.gitea/workflows/promote-dev.yaml index bdeee51..5ea146b 100644 --- a/.gitea/workflows/promote-dev.yaml +++ b/.gitea/workflows/promote-dev.yaml @@ -36,6 +36,7 @@ jobs: HARBOR_REPOSITORY: ${{ env.HARBOR_REPOSITORY }} HARBOR_USERNAME: ${{ env.HARBOR_USERNAME }} HARBOR_PASSWORD: ${{ env.HARBOR_PASSWORD }} + GITEA_API_TOKEN: ${{ github.token }} run: | set -eu python3 - <<'PY' >> "$GITHUB_OUTPUT" @@ -44,23 +45,31 @@ jobs: registry = os.environ['HARBOR_REGISTRY'] repository = f"{os.environ['HARBOR_PROJECT']}/{os.environ['HARBOR_REPOSITORY']}" encoded_repo = urllib.parse.quote(repository, safe='/') + api_root = 'https://git.aeternalisgames.com/api/v1/repos/dingo/AEvolution' + api_headers = {'Authorization': f"token {os.environ['GITEA_API_TOKEN']}"} + runs_request = urllib.request.Request(f'{api_root}/actions/runs?limit=50', headers=api_headers) + with urllib.request.urlopen(runs_request, timeout=30) as response: + runs = json.load(response).get('workflow_runs') or [] + candidates = [] + for run in runs: + if (run.get('event') != 'push' or run.get('head_branch') != 'main' + or run.get('status') != 'completed' or run.get('conclusion') != 'success'): + continue + jobs_request = urllib.request.Request(f"{api_root}/actions/runs/{run['id']}/jobs", headers=api_headers) + with urllib.request.urlopen(jobs_request, timeout=30) as response: + jobs = json.load(response).get('jobs') or [] + if any(job.get('name') == 'server-image' and job.get('conclusion') == 'success' for job in jobs): + candidates.append(run) + if not candidates: + raise SystemExit('No successful server-image workflow run was found') + run = max(candidates, key=lambda item: (item.get('run_number', 0), item.get('run_attempt', 0))) + commit = run['head_sha'] + tag = f"sha-{commit}-run-{run['run_number']}-{run.get('run_attempt', 1)}" credentials = base64.b64encode( f"{os.environ['HARBOR_USERNAME']}:{os.environ['HARBOR_PASSWORD']}".encode() ).decode() headers = {'Authorization': f'Basic {credentials}'} - tags_url = f'https://{registry}/v2/{encoded_repo}/tags/list?n=1000' - request = urllib.request.Request(tags_url, headers=headers) - with urllib.request.urlopen(request, timeout=30) as response: - tags = json.load(response).get('tags') or [] - tag_re = re.compile(r'^sha-([0-9a-f]{40})-run-([0-9]+)-([0-9]+)$') - candidates = [] - for tag in tags: - match = tag_re.fullmatch(tag) - if match: - candidates.append((int(match.group(2)), int(match.group(3)), tag, match.group(1))) - if not candidates: - raise SystemExit('No immutable server-image tags were found in the registry') - _, _, tag, commit = max(candidates) + manifest_url = f'https://{registry}/v2/{encoded_repo}/manifests/{urllib.parse.quote(tag, safe="")}' manifest_headers = { **headers, diff --git a/.gitea/workflows/promote-stage.yaml b/.gitea/workflows/promote-stage.yaml index b0a9193..2e643cc 100644 --- a/.gitea/workflows/promote-stage.yaml +++ b/.gitea/workflows/promote-stage.yaml @@ -36,6 +36,7 @@ jobs: HARBOR_REPOSITORY: ${{ env.HARBOR_REPOSITORY }} HARBOR_USERNAME: ${{ env.HARBOR_USERNAME }} HARBOR_PASSWORD: ${{ env.HARBOR_PASSWORD }} + GITEA_API_TOKEN: ${{ github.token }} run: | set -eu python3 - <<'PY' >> "$GITHUB_OUTPUT" @@ -44,23 +45,31 @@ jobs: registry = os.environ['HARBOR_REGISTRY'] repository = f"{os.environ['HARBOR_PROJECT']}/{os.environ['HARBOR_REPOSITORY']}" encoded_repo = urllib.parse.quote(repository, safe='/') + api_root = 'https://git.aeternalisgames.com/api/v1/repos/dingo/AEvolution' + api_headers = {'Authorization': f"token {os.environ['GITEA_API_TOKEN']}"} + runs_request = urllib.request.Request(f'{api_root}/actions/runs?limit=50', headers=api_headers) + with urllib.request.urlopen(runs_request, timeout=30) as response: + runs = json.load(response).get('workflow_runs') or [] + candidates = [] + for run in runs: + if (run.get('event') != 'push' or run.get('head_branch') != 'main' + or run.get('status') != 'completed' or run.get('conclusion') != 'success'): + continue + jobs_request = urllib.request.Request(f"{api_root}/actions/runs/{run['id']}/jobs", headers=api_headers) + with urllib.request.urlopen(jobs_request, timeout=30) as response: + jobs = json.load(response).get('jobs') or [] + if any(job.get('name') == 'server-image' and job.get('conclusion') == 'success' for job in jobs): + candidates.append(run) + if not candidates: + raise SystemExit('No successful server-image workflow run was found') + run = max(candidates, key=lambda item: (item.get('run_number', 0), item.get('run_attempt', 0))) + commit = run['head_sha'] + tag = f"sha-{commit}-run-{run['run_number']}-{run.get('run_attempt', 1)}" credentials = base64.b64encode( f"{os.environ['HARBOR_USERNAME']}:{os.environ['HARBOR_PASSWORD']}".encode() ).decode() headers = {'Authorization': f'Basic {credentials}'} - tags_url = f'https://{registry}/v2/{encoded_repo}/tags/list?n=1000' - request = urllib.request.Request(tags_url, headers=headers) - with urllib.request.urlopen(request, timeout=30) as response: - tags = json.load(response).get('tags') or [] - tag_re = re.compile(r'^sha-([0-9a-f]{40})-run-([0-9]+)-([0-9]+)$') - candidates = [] - for tag in tags: - match = tag_re.fullmatch(tag) - if match: - candidates.append((int(match.group(2)), int(match.group(3)), tag, match.group(1))) - if not candidates: - raise SystemExit('No immutable server-image tags were found in the registry') - _, _, tag, commit = max(candidates) + manifest_url = f'https://{registry}/v2/{encoded_repo}/manifests/{urllib.parse.quote(tag, safe="")}' manifest_headers = { **headers,