|
| 1 | +name: 'Sign Container Image' |
| 2 | +description: >- |
| 3 | + Sign a container image and attest SLSA v1 build provenance using cosign |
| 4 | + keyless (Sigstore) with GitHub Actions OIDC. Callers must check out the |
| 5 | + source tree (e.g. `actions/checkout`) before invoking this action — the |
| 6 | + built commit is resolved with `git rev-parse HEAD` so the attestation |
| 7 | + records the revision that was actually built rather than `github.sha`, |
| 8 | + which on a tag push reflects the tag-pointed commit and may differ from |
| 9 | + the branch tip the release workflow checks out. The calling job must |
| 10 | + have `id-token: write`. |
| 11 | +
|
| 12 | +inputs: |
| 13 | + image: |
| 14 | + description: 'Full image reference (registry/repo:tag) to sign by digest.' |
| 15 | + required: true |
| 16 | + workflow-path: |
| 17 | + description: 'Path to the workflow file (repo-relative) that triggered the build.' |
| 18 | + required: true |
| 19 | + run-started-at: |
| 20 | + description: >- |
| 21 | + ISO-8601 workflow run start time (typically |
| 22 | + `github.run_started_at` from the caller workflow). Recorded as |
| 23 | + `runDetails.metadata.startedOn` in the SLSA provenance predicate. |
| 24 | + If empty, the action falls back to the time at which it began |
| 25 | + executing — `github.run_started_at` is reported empty in some |
| 26 | + edge cases and `required: true` on a composite-action input |
| 27 | + does not reject empty strings. |
| 28 | + required: true |
| 29 | + |
| 30 | +runs: |
| 31 | + using: "composite" |
| 32 | + steps: |
| 33 | + - name: Capture fallback start timestamp |
| 34 | + id: start |
| 35 | + shell: bash |
| 36 | + run: | |
| 37 | + echo "STARTED_ON=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> "$GITHUB_OUTPUT" |
| 38 | +
|
| 39 | + - name: Install cosign |
| 40 | + uses: sigstore/cosign-installer@v3 |
| 41 | + |
| 42 | + - name: Install crane |
| 43 | + uses: imjasonh/setup-crane@v0.5 |
| 44 | + |
| 45 | + - name: Resolve image digest |
| 46 | + id: digest |
| 47 | + shell: bash |
| 48 | + env: |
| 49 | + IMAGE: ${{ inputs.image }} |
| 50 | + run: | |
| 51 | + set -euo pipefail |
| 52 | + DIGEST="$(crane digest "${IMAGE}")" |
| 53 | + REPO="${IMAGE%:*}" |
| 54 | + REF="${REPO}@${DIGEST}" |
| 55 | + echo "Resolved ${IMAGE} -> ${REF}" |
| 56 | + echo "REF=${REF}" >> "$GITHUB_OUTPUT" |
| 57 | + echo "DIGEST=${DIGEST}" >> "$GITHUB_OUTPUT" |
| 58 | +
|
| 59 | + - name: Sign image with cosign (keyless) |
| 60 | + shell: bash |
| 61 | + env: |
| 62 | + REF: ${{ steps.digest.outputs.REF }} |
| 63 | + run: | |
| 64 | + set -euo pipefail |
| 65 | + cosign sign --yes "${REF}" |
| 66 | +
|
| 67 | + - name: Generate SLSA v1 provenance predicate |
| 68 | + shell: bash |
| 69 | + env: |
| 70 | + WORKFLOW_PATH: ${{ inputs.workflow-path }} |
| 71 | + STARTED_ON: ${{ inputs.run-started-at }} |
| 72 | + FALLBACK_STARTED_ON: ${{ steps.start.outputs.STARTED_ON }} |
| 73 | + run: | |
| 74 | + set -euo pipefail |
| 75 | + if [[ -z "${STARTED_ON:-}" ]]; then |
| 76 | + STARTED_ON="$FALLBACK_STARTED_ON" |
| 77 | + fi |
| 78 | + RESOLVED_GIT_COMMIT="$(git rev-parse HEAD)" |
| 79 | + export RESOLVED_GIT_COMMIT STARTED_ON |
| 80 | + python3 - <<'PY' > predicate.json |
| 81 | + import json |
| 82 | + import os |
| 83 | +
|
| 84 | + repo = os.environ["GITHUB_REPOSITORY"] |
| 85 | + commit = os.environ["RESOLVED_GIT_COMMIT"] |
| 86 | + run_id = os.environ["GITHUB_RUN_ID"] |
| 87 | + run_attempt = os.environ["GITHUB_RUN_ATTEMPT"] |
| 88 | +
|
| 89 | + predicate = { |
| 90 | + "buildDefinition": { |
| 91 | + "buildType": "https://github.com/opencost/opencost/build/workflow@v1", |
| 92 | + "externalParameters": { |
| 93 | + "workflow": { |
| 94 | + "ref": os.environ["GITHUB_REF"], |
| 95 | + "repository": f"https://github.com/{repo}", |
| 96 | + "path": os.environ["WORKFLOW_PATH"], |
| 97 | + } |
| 98 | + }, |
| 99 | + "internalParameters": {}, |
| 100 | + "resolvedDependencies": [ |
| 101 | + { |
| 102 | + "uri": f"git+https://github.com/{repo}@{commit}", |
| 103 | + "digest": {"gitCommit": commit}, |
| 104 | + } |
| 105 | + ], |
| 106 | + }, |
| 107 | + "runDetails": { |
| 108 | + "builder": { |
| 109 | + "id": f"https://github.com/{repo}/actions/runs/{run_id}", |
| 110 | + }, |
| 111 | + "metadata": { |
| 112 | + "invocationId": f"https://github.com/{repo}/actions/runs/{run_id}/attempts/{run_attempt}", |
| 113 | + "startedOn": os.environ["STARTED_ON"], |
| 114 | + }, |
| 115 | + }, |
| 116 | + } |
| 117 | + print(json.dumps(predicate, indent=2)) |
| 118 | + PY |
| 119 | +
|
| 120 | + - name: Attest SLSA provenance with cosign |
| 121 | + shell: bash |
| 122 | + env: |
| 123 | + REF: ${{ steps.digest.outputs.REF }} |
| 124 | + run: | |
| 125 | + set -euo pipefail |
| 126 | + cosign attest --yes \ |
| 127 | + --predicate predicate.json \ |
| 128 | + --type slsaprovenance1 \ |
| 129 | + "${REF}" |
0 commit comments