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
registry = os.environ['HARBOR_REGISTRY']
project = os.environ['HARBOR_PROJECT']
repository = os.environ['HARBOR_REPOSITORY']
path = urllib.parse.quote(f'{project}/{repository}', safe='')
params = urllib.parse.urlencode({'with_tag': 'true', 'page': 1, 'page_size': 100, 'sort': '-push_time'})
url = f'https://{registry}/api/v2.0/projects/{urllib.parse.quote(project, safe="")}/repositories/{urllib.parse.quote(repository, safe="")}/artifacts?{params}'
token = base64.b64encode(f"{os.environ['HARBOR_USERNAME']}:{os.environ['HARBOR_PASSWORD']}".encode()).decode()
request = urllib.request.Request(url, headers={'Authorization': f'Basic {token}'})
repository = f"{os.environ['HARBOR_PROJECT']}/{os.environ['HARBOR_REPOSITORY']}"
encoded_repo = urllib.parse.quote(repository, safe='/')
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:
artifacts = json.load(response)
tag_re = re.compile(r'^sha-([0-9a-f]{40})-run-[0-9]+-[0-9]+$')
tags = json.load(response).get('tags') or []
tag_re = re.compile(r'^sha-([0-9a-f]{40})-run-([0-9]+)-([0-9]+)$')
candidates = []
for artifact in artifacts:
digest = artifact.get('digest', '')
for tag in artifact.get('tags') or []:
match = tag_re.fullmatch(tag.get('name', ''))
if match and re.fullmatch(r'sha256:[0-9a-f]{64}', digest):
candidates.append((artifact.get('push_time', ''), digest, tag['name'], match.group(1)))
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 Harbor')
candidates.sort(reverse=True)
_, digest, tag, commit = candidates[0]
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,
'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'build={digest.removeprefix("sha256:")}')
print(f'tag={tag}')
+31 -17
View File
@@ -42,26 +42,40 @@ jobs:
import base64, json, os, re, urllib.parse, urllib.request
registry = os.environ['HARBOR_REGISTRY']
project = os.environ['HARBOR_PROJECT']
repository = os.environ['HARBOR_REPOSITORY']
params = urllib.parse.urlencode({'with_tag': 'true', 'page': 1, 'page_size': 100, 'sort': '-push_time'})
url = f'https://{registry}/api/v2.0/projects/{urllib.parse.quote(project, safe="")}/repositories/{urllib.parse.quote(repository, safe="")}/artifacts?{params}'
token = base64.b64encode(f"{os.environ['HARBOR_USERNAME']}:{os.environ['HARBOR_PASSWORD']}".encode()).decode()
request = urllib.request.Request(url, headers={'Authorization': f'Basic {token}'})
repository = f"{os.environ['HARBOR_PROJECT']}/{os.environ['HARBOR_REPOSITORY']}"
encoded_repo = urllib.parse.quote(repository, safe='/')
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:
artifacts = json.load(response)
tag_re = re.compile(r'^sha-([0-9a-f]{40})-run-[0-9]+-[0-9]+$')
tags = json.load(response).get('tags') or []
tag_re = re.compile(r'^sha-([0-9a-f]{40})-run-([0-9]+)-([0-9]+)$')
candidates = []
for artifact in artifacts:
digest = artifact.get('digest', '')
for tag in artifact.get('tags') or []:
match = tag_re.fullmatch(tag.get('name', ''))
if match and re.fullmatch(r'sha256:[0-9a-f]{64}', digest):
candidates.append((artifact.get('push_time', ''), digest, tag['name'], match.group(1)))
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 Harbor')
candidates.sort(reverse=True)
_, digest, tag, commit = candidates[0]
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,
'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'build={digest.removeprefix("sha256:")}')
print(f'tag={tag}')