#!/usr/bin/env bash set -euo pipefail : "${REGISTRY_USER:?Dedicated registry user is required}" : "${REGISTRY_TOKEN:?Dedicated registry token is required}" : "${RELEASE_TOKEN:?Dedicated Gitea release token is required}" : "${RELEASE_SIGNING_KEY_B64:?Publisher ECDSA P-256 private key is required}" : "${BUILD_SERVER_URL:?Gitea URL is required}" : "${BUILD_REPOSITORY:?Repository is required}" [[ "$BUILD_SERVER_URL" == https://sources.tordais.cc ]] || { echo 'Unexpected publication host' >&2; exit 1; } [[ "$BUILD_REPOSITORY" == arnaud.dartois/Heimdall ]] || { echo 'Unexpected publication repository' >&2; exit 1; } sha=$(git rev-parse HEAD) umask 077 signing_key=$(mktemp) trap 'rm -f "$signing_key"' EXIT printf '%s' "$RELEASE_SIGNING_KEY_B64" | base64 -d > "$signing_key" openssl pkey -in "$signing_key" -noout >/dev/null openssl ec -in "$signing_key" -noout -text 2>/dev/null | grep -q 'ASN1 OID: prime256v1' || { echo 'Release signing key must use ECDSA P-256' >&2 exit 1 } tag=${BUILD_REF#refs/tags/} [[ "$BUILD_REF" == refs/tags/v* ]] || exit 1 python3 - "$sha" "$tag" <<'PY' import json,sys with open('artifacts/packages/provenance.json') as f:p=json.load(f) if p['commit'] != sys.argv[1] or 'v'+p['version'] != sys.argv[2]: raise SystemExit('Artifact provenance does not match checkout/tag') PY (cd artifacts/packages && sha256sum --check SHA256SUMS) printf '%s' "${tag#v}" > artifacts/version bash scripts/ci/images.sh # No mutation is attempted until all credentials, provenance and local builds pass. export DOCKER_CONFIG DOCKER_CONFIG=$(mktemp -d) trap 'rm -rf "$DOCKER_CONFIG"; rm -f "$signing_key"' EXIT printf '%s' "$REGISTRY_TOKEN" | docker login sources.tordais.cc -u "$REGISTRY_USER" --password-stdin agent_image=$(python3 -c 'import json; print(json.load(open("docker/agent-image.json"))["image"])') [[ "$agent_image" =~ ^[a-zA-Z0-9][a-zA-Z0-9._:/-]*$ && "${agent_image##*/}" != *:* ]] || { echo 'Invalid agent image repository in docker/agent-image.json' >&2; exit 1; } agent_registry=${agent_image%%/*} if [[ "$agent_registry" == "$agent_image" || ( "$agent_registry" != *.* && "$agent_registry" != *:* && "$agent_registry" != localhost* ) ]]; then agent_registry=docker.io fi if [[ "$agent_registry" != sources.tordais.cc ]]; then : "${AGENT_REGISTRY_USER:?Agent registry user is required for $agent_registry}" : "${AGENT_REGISTRY_TOKEN:?Agent registry token is required for $agent_registry}" printf '%s' "$AGENT_REGISTRY_TOKEN" | docker login "$agent_registry" -u "$AGENT_REGISTRY_USER" --password-stdin fi : > artifacts/image-digests.txt for component in server agent; do if [[ "$component" == agent ]]; then image=$agent_image else image="sources.tordais.cc/arnaud.dartois/heimdall-server" fi for image_tag in "sha-$sha" "${tag#v}"; do if docker manifest inspect "$image:$image_tag" >/dev/null 2>&1; then docker pull "$image:$image_tag" revision=$(docker image inspect "$image:$image_tag" --format '{{index .Config.Labels "org.opencontainers.image.revision"}}') [[ "$revision" == "$sha" ]] || { echo 'Refusing to overwrite an image from another commit' >&2; exit 1; } else docker tag "heimdall-$component:ci-$sha" "$image:$image_tag" docker push "$image:$image_tag" fi docker image inspect "$image:$image_tag" --format '{{range .RepoDigests}}{{println .}}{{end}}' >> artifacts/image-digests.txt done done cp artifacts/image-digests.txt artifacts/packages/image-digests.txt (cd artifacts/packages && sha256sum image-digests.txt >> SHA256SUMS) python3 - "$(python3 -c 'import json; print(json.load(open("docker/agent-image.json"))["image"])')" <<'PY' import json,sys repository=sys.argv[1] matches=set() for line in open('artifacts/image-digests.txt'): ref=line.strip() if '@' not in ref: continue image,digest=ref.rsplit('@',1) if image == repository and digest.startswith('sha256:') and len(digest) == 71: matches.add(digest) if len(matches) != 1: raise SystemExit('Expected one immutable agent image digest for configured repository') with open('artifacts/packages/agent-image.json','w') as f: json.dump({'image':repository,'digest':matches.pop()},f,sort_keys=True,separators=(',',':')) f.write('\n') PY (cd artifacts/packages && sha256sum agent-image.json >> SHA256SUMS) python3 - <<'PY' import hashlib,json,re from pathlib import Path root=Path('artifacts/packages') image=json.loads((root/'agent-image.json').read_text()) package_version=Path('artifacts/version').read_text().strip() version='v'+package_version assets={} for path in root.glob('heimdall-agent-*.tar.gz'): suffix=f'-{package_version}.tar.gz' if not path.name.endswith(suffix): continue platform=path.name[len('heimdall-agent-'):-len(suffix)] if not re.fullmatch(r'[a-z0-9-]+',platform): raise SystemExit('Invalid agent archive platform') assets[platform]=hashlib.sha256(path.read_bytes()).hexdigest() if not assets: raise SystemExit('No agent archive to sign') payload={'version':version,'assets':assets,'containerImage':image['image'], 'containerImageDigest':image['digest']} (root/'agent-update-manifest.json').write_text( json.dumps(payload,sort_keys=True,separators=(',',':'))+'\n') PY openssl dgst -sha256 -sign "$signing_key" \ -out artifacts/packages/agent-update-manifest.sig artifacts/packages/agent-update-manifest.json (cd artifacts/packages && sha256sum agent-update-manifest.json agent-update-manifest.sig >> SHA256SUMS) python3 scripts/ci/release-gitea.py