132 lines
6.0 KiB
YAML
132 lines
6.0 KiB
YAML
name: Promote server image to dev
|
|
|
|
on:
|
|
workflow_dispatch:
|
|
|
|
concurrency:
|
|
group: promote-server-dev
|
|
cancel-in-progress: false
|
|
|
|
permissions:
|
|
contents: write
|
|
|
|
env:
|
|
HARBOR_REGISTRY: registry.aeternalisgames.com
|
|
HARBOR_PROJECT: ${{ vars.HARBOR_PROJECT || 'aeternalis' }}
|
|
HARBOR_REPOSITORY: ${{ vars.HARBOR_SERVER_REPOSITORY || 'aevolution-server' }}
|
|
HARBOR_USERNAME: ${{ secrets.HARBOR_USERNAME }}
|
|
HARBOR_PASSWORD: ${{ secrets.HARBOR_PASSWORD }}
|
|
|
|
jobs:
|
|
promote:
|
|
runs-on: ${{ vars.CI_RUNNER_LABEL || 'ubuntu-22.04' }}
|
|
steps:
|
|
- name: Checkout main
|
|
uses: https://github.com/actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
|
|
with:
|
|
ref: main
|
|
fetch-depth: 0
|
|
persist-credentials: true
|
|
|
|
- name: Resolve newest published server image
|
|
id: image
|
|
env:
|
|
HARBOR_REGISTRY: ${{ env.HARBOR_REGISTRY }}
|
|
HARBOR_PROJECT: ${{ env.HARBOR_PROJECT }}
|
|
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"
|
|
import base64, json, os, re, urllib.parse, urllib.request
|
|
|
|
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 []
|
|
for job in jobs:
|
|
if job.get('name') == 'server-image' and job.get('conclusion') == 'success':
|
|
candidates.append((run, job))
|
|
if not candidates:
|
|
raise SystemExit('No successful server-image workflow run was found')
|
|
run, job = max(candidates, key=lambda item: (item[0].get('run_number', 0), item[0].get('run_attempt', 0)))
|
|
commit = run['head_sha']
|
|
tag = f"sha-{commit}-run-{run['run_number']}-{run.get('run_attempt', 1)}"
|
|
logs_url = f"{api_root}/actions/jobs/{job['id']}/logs"
|
|
request = urllib.request.Request(logs_url, headers=api_headers)
|
|
with urllib.request.urlopen(request, timeout=30) as response:
|
|
logs = response.read().decode(errors='replace')
|
|
digest_match = re.search(r'Digest:\s+registry\.aeternalisgames\.com/[^@\s]+@(sha256:[0-9a-f]{64})', logs)
|
|
if not digest_match:
|
|
raise SystemExit(f'No published digest found in successful server-image job {job["id"]}')
|
|
digest = digest_match.group(1)
|
|
print(f'digest={digest}')
|
|
print(f'build={digest.removeprefix("sha256:")}')
|
|
print(f'tag={tag}')
|
|
print(f'commit={commit}')
|
|
PY
|
|
test -n "${{ steps.image.outputs.digest }}"
|
|
echo "Selected immutable image ${{ steps.image.outputs.tag }}"
|
|
echo "Digest: ${{ steps.image.outputs.digest }}"
|
|
|
|
- name: Update dev image and build identity
|
|
env:
|
|
IMAGE_DIGEST: ${{ steps.image.outputs.digest }}
|
|
BUILD_ID: ${{ steps.image.outputs.build }}
|
|
run: |
|
|
set -eu
|
|
python3 - <<'PY'
|
|
import os, re
|
|
digest = os.environ['IMAGE_DIGEST']
|
|
build = os.environ['BUILD_ID']
|
|
if not re.fullmatch(r'sha256:[0-9a-f]{64}', digest):
|
|
raise SystemExit('invalid image digest')
|
|
if not re.fullmatch(r'[0-9a-f]{64}', build) or build != digest[7:]:
|
|
raise SystemExit('invalid build identity')
|
|
files = [
|
|
'deploy/kubernetes/region/dev/kustomization.yaml',
|
|
'deploy/kubernetes/region/dev/region-values.yaml',
|
|
]
|
|
for name in files:
|
|
text = open(name, encoding='utf-8').read()
|
|
if name.endswith('kustomization.yaml'):
|
|
text, count = re.subn(r'(?m)^ digest: sha256:[0-9a-f]{64}$', f' digest: {digest}', text)
|
|
if count != 1:
|
|
raise SystemExit(f'{name}: expected one server digest, found {count}')
|
|
else:
|
|
text, count = re.subn(r'(?m)(name: REGION_BUILD, value: )[0-9a-f]{64}', rf'\g<1>{build}', text)
|
|
if count != 2:
|
|
raise SystemExit(f'{name}: expected two REGION_BUILD values, found {count}')
|
|
open(name, 'w', encoding='utf-8').write(text)
|
|
PY
|
|
|
|
- name: Validate and commit promotion
|
|
run: |
|
|
set -eu
|
|
git diff --check
|
|
python3 tools/ci/deploy-policy-check.py
|
|
git diff --quiet -- \
|
|
deploy/kubernetes/region/dev/kustomization.yaml \
|
|
deploy/kubernetes/region/dev/region-values.yaml && {
|
|
echo 'dev already points at the newest published server image'; exit 0;
|
|
}
|
|
git config user.name 'aevolution-gitops-bot'
|
|
git config user.email 'aevolution-gitops-bot@aeternalisgames.com'
|
|
git add deploy/kubernetes/region/dev/kustomization.yaml deploy/kubernetes/region/dev/region-values.yaml
|
|
git commit -m "deploy(dev): promote latest server image"
|
|
git push origin HEAD:main
|