fix(promote): use registry API for image discovery

This commit is contained in:
aevolution-gitops-bot
2026-10-01 09:16:12 -04:00
parent a65612926c
commit e069ecd1f6
2 changed files with 62 additions and 35 deletions
+31 -18
View File
@@ -42,27 +42,40 @@ jobs:
import base64, json, os, re, urllib.parse, urllib.request import base64, json, os, re, urllib.parse, urllib.request
registry = os.environ['HARBOR_REGISTRY'] registry = os.environ['HARBOR_REGISTRY']
project = os.environ['HARBOR_PROJECT'] repository = f"{os.environ['HARBOR_PROJECT']}/{os.environ['HARBOR_REPOSITORY']}"
repository = os.environ['HARBOR_REPOSITORY'] encoded_repo = urllib.parse.quote(repository, safe='/')
path = urllib.parse.quote(f'{project}/{repository}', safe='') credentials = base64.b64encode(
params = urllib.parse.urlencode({'with_tag': 'true', 'page': 1, 'page_size': 100, 'sort': '-push_time'}) f"{os.environ['HARBOR_USERNAME']}:{os.environ['HARBOR_PASSWORD']}".encode()
url = f'https://{registry}/api/v2.0/projects/{urllib.parse.quote(project, safe="")}/repositories/{urllib.parse.quote(repository, safe="")}/artifacts?{params}' ).decode()
token = base64.b64encode(f"{os.environ['HARBOR_USERNAME']}:{os.environ['HARBOR_PASSWORD']}".encode()).decode() headers = {'Authorization': f'Basic {credentials}'}
request = urllib.request.Request(url, headers={'Authorization': f'Basic {token}'}) 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: with urllib.request.urlopen(request, timeout=30) as response:
artifacts = json.load(response) tags = json.load(response).get('tags') or []
tag_re = re.compile(r'^sha-([0-9a-f]{40})-run-[0-9]+-[0-9]+$') tag_re = re.compile(r'^sha-([0-9a-f]{40})-run-([0-9]+)-([0-9]+)$')
candidates = [] candidates = []
for artifact in artifacts: for tag in tags:
digest = artifact.get('digest', '') match = tag_re.fullmatch(tag)
for tag in artifact.get('tags') or []: if match:
match = tag_re.fullmatch(tag.get('name', '')) candidates.append((int(match.group(2)), int(match.group(3)), tag, match.group(1)))
if match and re.fullmatch(r'sha256:[0-9a-f]{64}', digest):
candidates.append((artifact.get('push_time', ''), digest, tag['name'], match.group(1)))
if not candidates: if not candidates:
raise SystemExit('No immutable server-image tags were found in Harbor') raise SystemExit('No immutable server-image tags were found in the registry')
candidates.sort(reverse=True) _, _, tag, commit = max(candidates)
_, digest, tag, commit = candidates[0] manifest_url = f'https://{registry}/v2/{encoded_repo}/manifests/{urllib.parse.quote(tag, safe="")}'
manifest_headers = {
**headers,
'Accept': ', '.join([
'application/vnd.oci.image.index.v1+json',
'application/vnd.docker.distribution.manifest.list.v2+json',
'application/vnd.oci.image.manifest.v1+json',
'application/vnd.docker.distribution.manifest.v2+json',
]),
}
request = urllib.request.Request(manifest_url, headers=manifest_headers, method='HEAD')
with urllib.request.urlopen(request, timeout=30) as response:
digest = response.headers.get('Docker-Content-Digest', '')
if not re.fullmatch(r'sha256:[0-9a-f]{64}', digest):
raise SystemExit(f'Registry returned invalid manifest digest for {tag}')
print(f'digest={digest}') print(f'digest={digest}')
print(f'build={digest.removeprefix("sha256:")}') print(f'build={digest.removeprefix("sha256:")}')
print(f'tag={tag}') print(f'tag={tag}')
+31 -17
View File
@@ -42,26 +42,40 @@ jobs:
import base64, json, os, re, urllib.parse, urllib.request import base64, json, os, re, urllib.parse, urllib.request
registry = os.environ['HARBOR_REGISTRY'] registry = os.environ['HARBOR_REGISTRY']
project = os.environ['HARBOR_PROJECT'] repository = f"{os.environ['HARBOR_PROJECT']}/{os.environ['HARBOR_REPOSITORY']}"
repository = os.environ['HARBOR_REPOSITORY'] encoded_repo = urllib.parse.quote(repository, safe='/')
params = urllib.parse.urlencode({'with_tag': 'true', 'page': 1, 'page_size': 100, 'sort': '-push_time'}) credentials = base64.b64encode(
url = f'https://{registry}/api/v2.0/projects/{urllib.parse.quote(project, safe="")}/repositories/{urllib.parse.quote(repository, safe="")}/artifacts?{params}' f"{os.environ['HARBOR_USERNAME']}:{os.environ['HARBOR_PASSWORD']}".encode()
token = base64.b64encode(f"{os.environ['HARBOR_USERNAME']}:{os.environ['HARBOR_PASSWORD']}".encode()).decode() ).decode()
request = urllib.request.Request(url, headers={'Authorization': f'Basic {token}'}) 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: with urllib.request.urlopen(request, timeout=30) as response:
artifacts = json.load(response) tags = json.load(response).get('tags') or []
tag_re = re.compile(r'^sha-([0-9a-f]{40})-run-[0-9]+-[0-9]+$') tag_re = re.compile(r'^sha-([0-9a-f]{40})-run-([0-9]+)-([0-9]+)$')
candidates = [] candidates = []
for artifact in artifacts: for tag in tags:
digest = artifact.get('digest', '') match = tag_re.fullmatch(tag)
for tag in artifact.get('tags') or []: if match:
match = tag_re.fullmatch(tag.get('name', '')) candidates.append((int(match.group(2)), int(match.group(3)), tag, match.group(1)))
if match and re.fullmatch(r'sha256:[0-9a-f]{64}', digest):
candidates.append((artifact.get('push_time', ''), digest, tag['name'], match.group(1)))
if not candidates: if not candidates:
raise SystemExit('No immutable server-image tags were found in Harbor') raise SystemExit('No immutable server-image tags were found in the registry')
candidates.sort(reverse=True) _, _, tag, commit = max(candidates)
_, digest, tag, commit = candidates[0] manifest_url = f'https://{registry}/v2/{encoded_repo}/manifests/{urllib.parse.quote(tag, safe="")}'
manifest_headers = {
**headers,
'Accept': ', '.join([
'application/vnd.oci.image.index.v1+json',
'application/vnd.docker.distribution.manifest.list.v2+json',
'application/vnd.oci.image.manifest.v1+json',
'application/vnd.docker.distribution.manifest.v2+json',
]),
}
request = urllib.request.Request(manifest_url, headers=manifest_headers, method='HEAD')
with urllib.request.urlopen(request, timeout=30) as response:
digest = response.headers.get('Docker-Content-Digest', '')
if not re.fullmatch(r'sha256:[0-9a-f]{64}', digest):
raise SystemExit(f'Registry returned invalid manifest digest for {tag}')
print(f'digest={digest}') print(f'digest={digest}')
print(f'build={digest.removeprefix("sha256:")}') print(f'build={digest.removeprefix("sha256:")}')
print(f'tag={tag}') print(f'tag={tag}')