fix(promote): require explicit immutable image digest

This commit is contained in:
aevolution-gitops-bot
2026-10-01 09:33:00 -04:00
parent d105cbf5e7
commit 26056b7c9f
2 changed files with 24 additions and 158 deletions
+12 -79
View File
@@ -2,6 +2,11 @@ name: Promote server image to dev
on:
workflow_dispatch:
inputs:
image_digest:
description: 'Immutable aevolution-server digest, including sha256:'
required: true
type: string
concurrency:
group: promote-server-dev
@@ -10,13 +15,6 @@ concurrency:
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' }}
@@ -28,83 +26,17 @@ jobs:
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, subprocess, 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)}"
image = f"{registry}/{repository}:{tag}"
login = subprocess.run(
['docker', 'login', registry, '--username', os.environ['HARBOR_USERNAME'], '--password-stdin'],
input=os.environ['HARBOR_PASSWORD'].encode(), stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE, check=False,
)
if login.returncode:
raise SystemExit(f'docker login failed with status {login.returncode}')
subprocess.run(['docker', 'pull', image], check=True)
repo_digest = subprocess.check_output(
['docker', 'image', 'inspect', '--format', '{{index .RepoDigests 0}}', image], text=True
).strip()
digest_match = re.search(r'@(sha256:[0-9a-f]{64})$', repo_digest)
if not digest_match:
raise SystemExit(f'No immutable digest found after pulling {image}')
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 }}
IMAGE_DIGEST: ${{ inputs.image_digest }}
run: |
set -eu
python3 - <<'PY'
import os, re
digest = os.environ['IMAGE_DIGEST']
build = os.environ['BUILD_ID']
digest = os.environ['IMAGE_DIGEST'].strip()
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')
raise SystemExit('image_digest must be sha256: followed by 64 lowercase hexadecimal characters')
build = digest[7:]
files = [
'deploy/kubernetes/region/dev/kustomization.yaml',
'deploy/kubernetes/region/dev/region-values.yaml',
@@ -120,6 +52,7 @@ jobs:
if count != 2:
raise SystemExit(f'{name}: expected two REGION_BUILD values, found {count}')
open(name, 'w', encoding='utf-8').write(text)
print(f'Promoting dev to {digest}')
PY
- name: Validate and commit promotion
@@ -130,10 +63,10 @@ jobs:
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;
echo 'dev already points at this 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 commit -m "deploy(dev): promote selected server image"
git push origin HEAD:main
+12 -79
View File
@@ -2,6 +2,11 @@ name: Promote server image to stage
on:
workflow_dispatch:
inputs:
image_digest:
description: 'Immutable aevolution-server digest, including sha256:'
required: true
type: string
concurrency:
group: promote-server-stage
@@ -10,13 +15,6 @@ concurrency:
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' }}
@@ -28,83 +26,17 @@ jobs:
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, subprocess, 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)}"
image = f"{registry}/{repository}:{tag}"
login = subprocess.run(
['docker', 'login', registry, '--username', os.environ['HARBOR_USERNAME'], '--password-stdin'],
input=os.environ['HARBOR_PASSWORD'].encode(), stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE, check=False,
)
if login.returncode:
raise SystemExit(f'docker login failed with status {login.returncode}')
subprocess.run(['docker', 'pull', image], check=True)
repo_digest = subprocess.check_output(
['docker', 'image', 'inspect', '--format', '{{index .RepoDigests 0}}', image], text=True
).strip()
digest_match = re.search(r'@(sha256:[0-9a-f]{64})$', repo_digest)
if not digest_match:
raise SystemExit(f'No immutable digest found after pulling {image}')
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 stage image and build identity
env:
IMAGE_DIGEST: ${{ steps.image.outputs.digest }}
BUILD_ID: ${{ steps.image.outputs.build }}
IMAGE_DIGEST: ${{ inputs.image_digest }}
run: |
set -eu
python3 - <<'PY'
import os, re
digest = os.environ['IMAGE_DIGEST']
build = os.environ['BUILD_ID']
digest = os.environ['IMAGE_DIGEST'].strip()
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')
raise SystemExit('image_digest must be sha256: followed by 64 lowercase hexadecimal characters')
build = digest[7:]
files = [
'deploy/kubernetes/region/stage/kustomization.yaml',
'deploy/kubernetes/region/stage/region-values.yaml',
@@ -120,6 +52,7 @@ jobs:
if count != 2:
raise SystemExit(f'{name}: expected two REGION_BUILD values, found {count}')
open(name, 'w', encoding='utf-8').write(text)
print(f'Promoting stage to {digest}')
PY
- name: Validate and commit promotion
@@ -130,10 +63,10 @@ jobs:
git diff --quiet -- \
deploy/kubernetes/region/stage/kustomization.yaml \
deploy/kubernetes/region/stage/region-values.yaml && {
echo 'stage already points at the newest published server image'; exit 0;
echo 'stage already points at this image'; exit 0;
}
git config user.name 'aevolution-gitops-bot'
git config user.email 'aevolution-gitops-bot@aeternalisgames.com'
git add deploy/kubernetes/region/stage/kustomization.yaml deploy/kubernetes/region/stage/region-values.yaml
git commit -m "deploy(stage): promote latest server image"
git commit -m "deploy(stage): promote selected server image"
git push origin HEAD:main