|
| 1 | +name: verify-stages |
| 2 | + |
| 3 | +# Runs the BYOX test suite on every push to main, hashes the canonical |
| 4 | +# files, mints a GitHub OIDC token bound to audience=karnstack, and |
| 5 | +# reports the result to karnstack.com so verified stages light up in |
| 6 | +# your karnstack dashboard. |
| 7 | +# |
| 8 | +# You should not edit this workflow or anything in .karnstack/ - those |
| 9 | +# files are part of the karnstack canonical template and their hashes |
| 10 | +# are checked server-side. Modifying them causes verification to be |
| 11 | +# rejected with `files_modified`. Re-fetch them from the template if |
| 12 | +# you do edit by accident: |
| 13 | +# |
| 14 | +# git checkout origin/template -- .github/workflows/verify-stages.yml .karnstack/ |
| 15 | + |
| 16 | +on: |
| 17 | + push: |
| 18 | + branches: [main] |
| 19 | + workflow_dispatch: |
| 20 | + |
| 21 | +permissions: |
| 22 | + contents: read |
| 23 | + id-token: write # required to mint the OIDC token |
| 24 | + |
| 25 | +env: |
| 26 | + PROJECT_SLUG: rate-limiter |
| 27 | + LANGUAGE: python |
| 28 | + KARNSTACK_URL: https://karnstack.com |
| 29 | + |
| 30 | +jobs: |
| 31 | + verify: |
| 32 | + # Skip the workflow on the karnstack canonical template itself. |
| 33 | + # A template-marked repo cannot produce a valid `template_repository` |
| 34 | + # claim, so karnstack's verify endpoint would reject it anyway with |
| 35 | + # `not_from_template`. Skipping here saves the action minutes and |
| 36 | + # keeps the template's CI history clean. |
| 37 | + if: ${{ !github.event.repository.is_template }} |
| 38 | + runs-on: ubuntu-latest |
| 39 | + steps: |
| 40 | + - name: Checkout |
| 41 | + uses: actions/checkout@v6 |
| 42 | + |
| 43 | + - name: Install mise toolchain |
| 44 | + uses: jdx/mise-action@v4 |
| 45 | + |
| 46 | + - name: Run all stages |
| 47 | + id: tests |
| 48 | + continue-on-error: true |
| 49 | + run: mise run all 2>&1 | tee test-output.txt |
| 50 | + |
| 51 | + - name: Compute canonical file hashes |
| 52 | + run: bash .karnstack/compute-hashes.sh > hashes.json |
| 53 | + |
| 54 | + - name: Parse passing stages from test output |
| 55 | + run: bash .karnstack/parse-stages.sh test-output.txt > stages.json |
| 56 | + |
| 57 | + - name: Capture template metadata |
| 58 | + id: tmpl |
| 59 | + # Workflow-attested template lineage. karnstack's verify endpoint |
| 60 | + # prefers this over hitting the unauthenticated GitHub API, which |
| 61 | + # is required for private forks (the API call 404s on those). |
| 62 | + # |
| 63 | + # The push event payload does NOT include |
| 64 | + # repository.template_repository (it's a stripped-down version of |
| 65 | + # the repo object), so we ask the REST API directly with the |
| 66 | + # auto-issued GITHUB_TOKEN. This call is authenticated and works |
| 67 | + # on private forks. |
| 68 | + # |
| 69 | + # Trust is gated server-side by the hash check above: editing this |
| 70 | + # workflow to lie about template_repo busts the canonical hash and |
| 71 | + # the verification is rejected with `files_modified`. |
| 72 | + env: |
| 73 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 74 | + run: | |
| 75 | + set -euo pipefail |
| 76 | + TMPL=$(gh api "repos/${GITHUB_REPOSITORY}" --jq '.template_repository.full_name // ""') |
| 77 | + echo "full_name=${TMPL}" >> "$GITHUB_OUTPUT" |
| 78 | +
|
| 79 | + - name: Mint OIDC token and post to karnstack |
| 80 | + env: |
| 81 | + TEMPLATE_REPO: ${{ steps.tmpl.outputs.full_name }} |
| 82 | + run: | |
| 83 | + set -euo pipefail |
| 84 | +
|
| 85 | + OIDC_TOKEN=$(curl -sLS \ |
| 86 | + -H "Authorization: Bearer ${ACTIONS_ID_TOKEN_REQUEST_TOKEN}" \ |
| 87 | + "${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=karnstack" \ |
| 88 | + | jq -r '.value') |
| 89 | +
|
| 90 | + PAYLOAD=$(jq -n \ |
| 91 | + --arg t "$OIDC_TOKEN" \ |
| 92 | + --arg p "$PROJECT_SLUG" \ |
| 93 | + --arg l "$LANGUAGE" \ |
| 94 | + --arg tr "$TEMPLATE_REPO" \ |
| 95 | + --slurpfile s stages.json \ |
| 96 | + --slurpfile h hashes.json \ |
| 97 | + '{ oidc_token: $t, project: $p, language: $l, template_repo: $tr, stages: $s[0], hashes: $h[0] }') |
| 98 | +
|
| 99 | + RESPONSE=$(curl -sS -X POST "${KARNSTACK_URL}/api/v1/byox/verify" \ |
| 100 | + -H "Content-Type: application/json" \ |
| 101 | + -d "$PAYLOAD") |
| 102 | +
|
| 103 | + echo "karnstack response:" |
| 104 | + echo "$RESPONSE" | jq . |
| 105 | +
|
| 106 | + OK=$(echo "$RESPONSE" | jq -r '.ok // false') |
| 107 | + if [ "$OK" != "true" ]; then |
| 108 | + REASON=$(echo "$RESPONSE" | jq -r '.reason // "unknown"') |
| 109 | + if [ "$REASON" = "files_modified" ]; then |
| 110 | + echo "::error title=karnstack verify::your starter is out of sync with karnstack. run 'mise run sync' (or bash .karnstack/sync.sh) to pull the latest canonical files, then commit and push." |
| 111 | + echo "::error::if .karnstack/sync.sh does not yet exist in your fork, bootstrap it first:" |
| 112 | + echo "::error:: curl -fsSL https://raw.githubusercontent.com/karnstack/byox-rate-limiter-python/main/.karnstack/sync.sh -o .karnstack/sync.sh && chmod +x .karnstack/sync.sh && bash .karnstack/sync.sh" |
| 113 | + else |
| 114 | + echo "::warning title=karnstack verify::verification not accepted: ${REASON}" |
| 115 | + fi |
| 116 | + else |
| 117 | + STAGES=$(echo "$RESPONSE" | jq -r '.verified_stages | join(", ")') |
| 118 | + echo "::notice title=karnstack verify::verified stages: ${STAGES}" |
| 119 | + fi |
0 commit comments