|
| 1 | +name: Promote Tested Image to Release Tag |
| 2 | + |
| 3 | +# Promoting does not rebuild anything. The image CI already built, tested and scanned |
| 4 | +# for this commit is promoted to the release tag by copying its manifest, so the |
| 5 | +# promoted artifact is byte for byte the one that passed the pipeline. A rebuild would |
| 6 | +# not be equivalent: the Dockerfile runs `apt-get upgrade`, so the same source produces |
| 7 | +# a different image whenever Ubuntu has moved on. |
| 8 | +# |
| 9 | +# The caller is expected to trigger this workflow from a push of a `vX.Y.Z` tag, or of |
| 10 | +# a component tag such as `component-vX.Y.Z` when release-please is configured to include |
| 11 | +# the component name, and to pass the registry credentials. The tag is validated against |
| 12 | +# the version release-please recorded for that component in .release-please-manifest.json. |
| 13 | + |
| 14 | +on: |
| 15 | + workflow_call: |
| 16 | + inputs: |
| 17 | + ci-workflow: |
| 18 | + description: "Workflow file of the CI that built and pushed the tested image (used to look up its run for the commit)" |
| 19 | + type: string |
| 20 | + default: "ci.yaml" |
| 21 | + image-name: |
| 22 | + description: "OCI image name (registry/repository) to promote, e.g. docker-regis.iex.ec/iexec-result-proxy" |
| 23 | + type: string |
| 24 | + required: true |
| 25 | + registry: |
| 26 | + description: "Registry the image lives in" |
| 27 | + type: string |
| 28 | + default: "docker.io" |
| 29 | + source-tag-prefix: |
| 30 | + description: "Prefix of the tag the CI tags production images with, before the short commit sha (e.g. `dev-`)" |
| 31 | + type: string |
| 32 | + default: "dev-" |
| 33 | + security-scan: |
| 34 | + description: "Enable Trivy Security Scan" |
| 35 | + type: boolean |
| 36 | + default: true |
| 37 | + trivy-version: |
| 38 | + description: "Trivy security scanner version" |
| 39 | + type: string |
| 40 | + default: "v0.71.0" |
| 41 | + secrets: |
| 42 | + username: |
| 43 | + description: "Registry username" |
| 44 | + required: true |
| 45 | + password: |
| 46 | + description: "Registry password (read and write access to the registry)" |
| 47 | + required: true |
| 48 | + |
| 49 | +permissions: |
| 50 | + contents: read |
| 51 | + # required to look up the CI run that built the tagged commit |
| 52 | + actions: read |
| 53 | + |
| 54 | +jobs: |
| 55 | + promote: |
| 56 | + name: Promote the tested image to the release tag |
| 57 | + runs-on: ubuntu-latest |
| 58 | + |
| 59 | + # avoid shell injection through string interpolation |
| 60 | + env: |
| 61 | + CI_WORKFLOW: ${{ inputs.ci-workflow }} |
| 62 | + IMAGE: ${{ inputs.image-name }} |
| 63 | + REGISTRY: ${{ inputs.registry }} |
| 64 | + SOURCE_TAG_PREFIX: ${{ inputs.source-tag-prefix }} |
| 65 | + |
| 66 | + steps: |
| 67 | + - name: Checkout |
| 68 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 69 | + with: |
| 70 | + fetch-depth: 0 |
| 71 | + |
| 72 | + - name: Check the tag and the project version agree |
| 73 | + id: check |
| 74 | + env: |
| 75 | + TAG: ${{ github.ref_name }} |
| 76 | + run: | |
| 77 | + if [ -z "$(git branch -r --contains "$GITHUB_SHA" 'origin/main')" ]; then |
| 78 | + echo "❌ Tag $TAG is not on main. Release tags must be created on main." |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | +
|
| 82 | + # release-please records the released version of every component in |
| 83 | + # .release-please-manifest.json, committed in the same PR as the version bump, |
| 84 | + # so at the tagged commit it holds exactly the version being released. |
| 85 | + version="${TAG##*-v}" |
| 86 | + if [ "$version" = "$TAG" ]; then |
| 87 | + version="${TAG#v}" |
| 88 | + component="." |
| 89 | + else |
| 90 | + component="${TAG%-v$version}" |
| 91 | + fi |
| 92 | +
|
| 93 | + if [ ! -f ".release-please-manifest.json" ]; then |
| 94 | + echo "❌ No .release-please-manifest.json at the repository root." |
| 95 | + echo " The project has to be released by release-please in manifest mode." |
| 96 | + exit 1 |
| 97 | + fi |
| 98 | +
|
| 99 | + declared=$(COMPONENT="$component" python3 - <<'PY' |
| 100 | + import json |
| 101 | + import os |
| 102 | + from pathlib import Path |
| 103 | +
|
| 104 | + component = os.environ["COMPONENT"] |
| 105 | + manifest = json.loads(Path(".release-please-manifest.json").read_text()) |
| 106 | + if component != ".": |
| 107 | + print(next((v for path, v in manifest.items() if path == component or path.rsplit("/", 1)[-1] == component), "")) |
| 108 | + else: |
| 109 | + keys = list(manifest) |
| 110 | + print(manifest["."] if "." in manifest else manifest[keys[0]] if len(keys) == 1 else "") |
| 111 | + PY |
| 112 | + ) |
| 113 | +
|
| 114 | + if [ -z "$declared" ]; then |
| 115 | + echo "❌ No version recorded for component '$component' in .release-please-manifest.json" |
| 116 | + exit 1 |
| 117 | + fi |
| 118 | +
|
| 119 | + if [ "$version" != "$declared" ]; then |
| 120 | + echo "❌ Tag version ($version) does not match the version release-please recorded for '$component' ($declared)" |
| 121 | + exit 1 |
| 122 | + fi |
| 123 | +
|
| 124 | + echo "✅ Tag $TAG matches $component version $declared" |
| 125 | +
|
| 126 | + # The CI tags images built from a production branch as |
| 127 | + # <source-tag-prefix><short sha>, and the release tag points at that commit. |
| 128 | + echo "version=$version" | tee -a "$GITHUB_OUTPUT" |
| 129 | + echo "source_tag=${SOURCE_TAG_PREFIX}$(echo "$GITHUB_SHA" | cut -c1-8)" | tee -a "$GITHUB_OUTPUT" |
| 130 | +
|
| 131 | + - name: Wait for the CI run that built this commit |
| 132 | + env: |
| 133 | + GH_TOKEN: ${{ github.token }} |
| 134 | + run: | |
| 135 | + # Both workflows start from the same push to main: release-please creates this |
| 136 | + # tag within a minute, while CI needs several more to build and push the image |
| 137 | + # being promoted. So the image is normally still missing when this job starts. |
| 138 | + for _ in $(seq 1 60); do |
| 139 | + run=$(gh run list --commit "$GITHUB_SHA" --workflow "$CI_WORKFLOW" --limit 1 \ |
| 140 | + --json status,conclusion \ |
| 141 | + --jq '.[] | "\(.status) \(.conclusion)"' 2>/dev/null || true) |
| 142 | + status=${run%% *} |
| 143 | + conclusion=${run##* } |
| 144 | +
|
| 145 | + case "$status" in |
| 146 | + completed) |
| 147 | + if [ "$conclusion" = "success" ]; then |
| 148 | + echo "✅ CI succeeded for $GITHUB_SHA" |
| 149 | + exit 0 |
| 150 | + fi |
| 151 | + echo "❌ CI for $GITHUB_SHA concluded '$conclusion'; refusing to release" |
| 152 | + exit 1 |
| 153 | + ;; |
| 154 | + "") |
| 155 | + echo "No CI run found yet for $GITHUB_SHA, waiting…" |
| 156 | + ;; |
| 157 | + *) |
| 158 | + echo "CI is $status, waiting…" |
| 159 | + ;; |
| 160 | + esac |
| 161 | + sleep 30 |
| 162 | + done |
| 163 | +
|
| 164 | + echo "❌ Timed out after 30 minutes waiting for the CI run on $GITHUB_SHA" |
| 165 | + exit 1 |
| 166 | +
|
| 167 | + - name: Login to Docker Registry |
| 168 | + uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0 |
| 169 | + with: |
| 170 | + registry: ${{ inputs.registry }} |
| 171 | + username: ${{ secrets.username }} |
| 172 | + password: ${{ secrets.password }} |
| 173 | + |
| 174 | + - name: Set up Docker Buildx |
| 175 | + # imagetools needs no builder, but this guarantees a buildx recent enough to support --prefer-index |
| 176 | + uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 |
| 177 | + |
| 178 | + - name: Resolve the digest CI built for this commit |
| 179 | + id: tested |
| 180 | + env: |
| 181 | + SOURCE_TAG: ${{ steps.check.outputs.source_tag }} |
| 182 | + run: | |
| 183 | + digest=$(docker buildx imagetools inspect "$IMAGE:$SOURCE_TAG" \ |
| 184 | + --format '{{.Manifest.Digest}}') |
| 185 | + echo " $SOURCE_TAG -> $digest" |
| 186 | + echo "digest=$digest" >> "$GITHUB_OUTPUT" |
| 187 | +
|
| 188 | + # Scanning before the promotion, not after: the release tag would land on this |
| 189 | + # very digest, so a vulnerability found here must stop the release rather than be |
| 190 | + # reported against an image already tagged as released. The digest is unchanged by |
| 191 | + # the promotion, so this is a scan of exactly what gets released. |
| 192 | + - name: Re-scan the tested image for newly disclosed vulnerabilities |
| 193 | + if: ${{ inputs.security-scan }} |
| 194 | + uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0 |
| 195 | + with: |
| 196 | + image-ref: ${{ env.IMAGE }}@${{ steps.tested.outputs.digest }} |
| 197 | + format: table |
| 198 | + ignore-unfixed: true |
| 199 | + vuln-type: 'os,library' |
| 200 | + severity: 'CRITICAL,HIGH' |
| 201 | + hide-progress: true |
| 202 | + exit-code: 1 |
| 203 | + # same scanner version as the CI build, so a finding here means the |
| 204 | + # vulnerability database moved, not the tool |
| 205 | + version: ${{ inputs.trivy-version }} |
| 206 | + |
| 207 | + - name: Promote the tested image to the release tag |
| 208 | + env: |
| 209 | + SOURCE_TAG: ${{ steps.check.outputs.source_tag }} |
| 210 | + VERSION: ${{ steps.check.outputs.version }} |
| 211 | + DIGEST: ${{ steps.tested.outputs.digest }} |
| 212 | + run: | |
| 213 | + # --prefer-index=false copies the manifest verbatim. Without it buildx wraps a |
| 214 | + # single-source manifest in an OCI index, which gives the release tag a new |
| 215 | + # digest and defeats the point of promoting the exact image that was tested. |
| 216 | + docker buildx imagetools create --prefer-index=false \ |
| 217 | + -t "$IMAGE:$VERSION" "$IMAGE:$SOURCE_TAG" |
| 218 | +
|
| 219 | + released=$(docker buildx imagetools inspect "$IMAGE:$VERSION" \ |
| 220 | + --format '{{.Manifest.Digest}}') |
| 221 | + echo " $SOURCE_TAG -> $DIGEST" |
| 222 | + echo " $VERSION -> $released" |
| 223 | +
|
| 224 | + if [ "$released" != "$DIGEST" ]; then |
| 225 | + echo "❌ $IMAGE:$VERSION does not point at the tested image." |
| 226 | + echo " A buildx too old to honour --prefer-index wraps the manifest in an index." |
| 227 | + exit 1 |
| 228 | + fi |
| 229 | +
|
| 230 | + echo "✅ $IMAGE:$VERSION is the image CI built and tested for $GITHUB_SHA" |
| 231 | +
|
| 232 | + - name: Summarise the release |
| 233 | + env: |
| 234 | + SOURCE_TAG: ${{ steps.check.outputs.source_tag }} |
| 235 | + VERSION: ${{ steps.check.outputs.version }} |
| 236 | + DIGEST: ${{ steps.tested.outputs.digest }} |
| 237 | + run: | |
| 238 | + { |
| 239 | + echo "### Released \`$IMAGE:$VERSION\`" |
| 240 | + echo "" |
| 241 | + echo "Promoted from \`$SOURCE_TAG\` without rebuilding." |
| 242 | + echo "" |
| 243 | + echo "| | |" |
| 244 | + echo "| --- | --- |" |
| 245 | + echo "| Commit | \`$GITHUB_SHA\` |" |
| 246 | + echo "| Digest | \`$DIGEST\` |" |
| 247 | + } >> "$GITHUB_STEP_SUMMARY" |
0 commit comments