diff --git a/.github/workflows/deploy-laravel.yml b/.github/workflows/deploy-laravel.yml index 8ec7263..57d9436 100644 --- a/.github/workflows/deploy-laravel.yml +++ b/.github/workflows/deploy-laravel.yml @@ -14,6 +14,7 @@ # approval_id: { type: string, required: false } # rollback_of: { type: string, required: false } # task: { type: string, required: false } +# run_id: { type: string, required: false } # jobs: # deploy: # uses: rasgo-studio/.github/.github/workflows/deploy-laravel.yml@main @@ -22,6 +23,7 @@ # approval_id: ${{ inputs.approval_id }} # rollback_of: ${{ inputs.rollback_of }} # task: ${{ inputs.task }} +# run_id: ${{ inputs.run_id }} # secrets: inherit # name: Deploy @@ -45,6 +47,10 @@ on: description: Plane task code (ACME01WEB-23), so Core can report the result on it type: string default: "" + run_id: + description: Number of the RASGO Core run this deploy belongs to; sent as X-Rasgo-Run to deploy-start and deploy-finish + type: string + default: "" app: description: App name in Core, when it is not the component of the contract type: string @@ -86,6 +92,7 @@ jobs: APPROVAL_ID: ${{ inputs.approval_id }} ROLLBACK_OF: ${{ inputs.rollback_of }} TASK: ${{ inputs.task }} + RUN_ID: ${{ inputs.run_id }} APP: ${{ inputs.app }} VERSION: ${{ inputs.version }} VERSIONS: ${{ inputs.versions }} diff --git a/README.md b/README.md index ea458ce..e914e64 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,11 @@ Calling them: this one undoes) and `task` (the Plane code, so Core can report on it), plus `app`, `version` and, for a monorepo, `versions` (a JSON object with the version of each app). `deploy-static.yml` also takes `pages-project`, `build-command`, `output-dir`, `production-branch` and `staging-branch`. +- **Tying a deploy to its run.** `deploy-laravel.yml` takes `run_id`, the number of the RASGO Core run the deploy + belongs to. With it, deploy-start and deploy-finish carry the header `X-Rasgo-Run: ` and Core keeps the + deploy in `deployments.run_id`, as an artifact of that run (RCOR-45). Only digits are accepted, and the Contract + step stops on anything else. Without it, neither request carries the header and the deploy works as before. Core + ignores a number that is not one of its runs. - **Secrets** live in the `staging` and `production` environments of the calling repository, with the names its contract lists (DEV-07): `RASGO_CORE_DEPLOY_TOKEN` always (the Core token of the deploy actor, ops 1), then `PLOI_DEPLOY_WEBHOOK`, or `CLOUDFLARE_API_TOKEN` and `CLOUDFLARE_ACCOUNT_ID`. The repository variable diff --git a/actions/deploy-core/core.sh b/actions/deploy-core/core.sh index 6f9b4a5..8553366 100755 --- a/actions/deploy-core/core.sh +++ b/actions/deploy-core/core.sh @@ -11,6 +11,8 @@ # # Core is the barrier: without an answer from Core nothing deploys, and there is no emergency exit (RINF-54). # The state of one deploy lives in a JSON file between steps; it holds names and ids, never a secret. +# With RUN_ID (the input run_id), deploy-start and deploy-finish carry X-Rasgo-Run, so Core ties the deploy to the run +# that asked for it (RCOR-45, deployments.run_id). Without it, both requests are the same as before (RINF-71). # Messages are in Spanish, for the person who reads the run (AGT-03). set -euo pipefail @@ -53,11 +55,13 @@ yaml_to_json() { # POST to Core: path, request file, response file. Prints the HTTP code; fails only when Core cannot be reached. # The token goes in a header file, so it never shows in the command line of the runner. core_post() { - local path="$1" request="$2" response="$3" headers code rc=0 + local path="$1" request="$2" response="$3" headers run code rc=0 shift 3 headers="$(mktemp)" chmod 600 "$headers" printf 'Authorization: Bearer %s\n' "${CORE_TOKEN:-}" > "$headers" + run="$(state_get run_id 2> /dev/null || true)" + if [ -n "$run" ]; then printf 'X-Rasgo-Run: %s\n' "$run" >> "$headers"; fi code="$(curl -sS --max-time 30 "$@" -X POST -H @"$headers" -H 'Content-Type: application/json' \ -H 'Accept: application/json' --data-binary @"$request" -o "$response" -w '%{http_code}' \ "$CORE_URL/api/v1/$path")" || rc=$? @@ -77,7 +81,7 @@ cmd_require() { cmd_contract() { local file="${CONTRACT_FILE:-rasgo.deploy.yml}" env="${DEPLOY_ENVIRONMENT:-}" json - local adapter component ref url approval path expect timeout version app branch public tag + local adapter component ref url approval path expect timeout version app branch public tag run_id case "$env" in staging | production) ;; @@ -136,6 +140,12 @@ cmd_contract() { [ -n "$version" ] || version="$(short "$GITHUB_SHA")" app="${APP:-$component}" + # The run of Core this deploy belongs to: its number, or nothing (RINF-71). Core ignores a number that is no run. + run_id="${RUN_ID:-}" + case "$run_id" in + *[!0-9]*) fail "run_id «$run_id» no es el número de un run de Core: solo cifras, o vacío." ;; + esac + # Core keeps the contract it receives (spec 003, Clarify 7), without the names of the secrets. public="$(jq -c 'del(.environments[]?.secrets)' <<< "$json")" @@ -143,13 +153,14 @@ cmd_contract() { --arg ref "$ref" --arg tag "$tag" --arg branch "$branch" --arg url "${url%/}" --arg approval "$approval" \ --arg health_path "$path" --arg health_expect "$expect" --arg health_timeout "$timeout" \ --arg version "$version" --arg commit "${GITHUB_SHA:?}" --arg approval_id "${APPROVAL_ID:-}" \ - --arg rollback_of "${ROLLBACK_OF:-}" --arg task "${TASK:-}" --argjson contract "$public" \ + --arg rollback_of "${ROLLBACK_OF:-}" --arg task "${TASK:-}" --arg run_id "$run_id" --argjson contract "$public" \ '{component: $component, app: $app, adapter: $adapter, environment: $environment, ref: $ref, tag: $tag, branch: $branch, url: $url, approval: $approval, health_path: $health_path, health_expect: $health_expect, health_timeout: $health_timeout, version: $version, commit: $commit, approval_id: $approval_id, - rollback_of: $rollback_of, task: $task, contract: $contract}' > "$STATE" + rollback_of: $rollback_of, task: $task, run_id: $run_id, contract: $contract}' > "$STATE" note "Contrato correcto: $component ($adapter) a $env, $url, versión $version, commit $(short "$GITHUB_SHA"), aprobación: $approval." + if [ -n "$run_id" ]; then note "El despliegue queda unido al run $run_id de Core (X-Rasgo-Run)."; fi summary "- Contrato: $component ($adapter) a $env, versión $version, commit $(short "$GITHUB_SHA")." } diff --git a/tests/deploy-core.test.sh b/tests/deploy-core.test.sh index 533e1ca..f1ae18f 100644 --- a/tests/deploy-core.test.sh +++ b/tests/deploy-core.test.sh @@ -88,7 +88,7 @@ health: timeout_s: 5 rollback: previous-tag EOF - unset APPROVAL_ID ROLLBACK_OF TASK APP VERSION VERSIONS PLOI_BRANCH CORE_TOKEN OUTPUT_DIR CONTRACT_FILE + unset APPROVAL_ID ROLLBACK_OF TASK RUN_ID APP VERSION VERSIONS PLOI_BRANCH CORE_TOKEN OUTPUT_DIR CONTRACT_FILE export FAKE_DIR="$FAKE" PATH="$FAKE/bin:$PATH" RASGO_STATE="$WORK/state.json" HEALTH_INTERVAL=0 export GITHUB_SHA="$SHA" GITHUB_REPOSITORY="rasgo-studio/acme-portal" GITHUB_SERVER_URL="https://github.com" export GITHUB_RUN_ID=42 GITHUB_REF_TYPE=branch GITHUB_REF_NAME=main GITHUB_STEP_SUMMARY="$WORK/summary" @@ -278,6 +278,41 @@ export CORE_TOKEN=t answer start 422 '{"error":"The deploy contract must not carry secrets: environments.staging.secrets.","rule":"PLN-11"}' expect_fail "start: a 422 says why Core refuses the request" "must not carry secrets" start +# --- run_id: X-Rasgo-Run (RINF-71) ------------------------------------------------------------------------------ + +setup +export RUN_ID=314 +expect_ok "run_id: the contract accepts the number of a run" contract +check "run_id: saved in the state" test "$(state run_id)" = 314 +check "run_id: the contract step says it" has "unido al run 314" +export CORE_TOKEN=token-secret-value +answer start 201 '{"deployment_id":"01JDEPLOY"}' +expect_ok "run_id: start" start +jq '.health = "ok"' "$RASGO_STATE" > "$RASGO_STATE.tmp" && mv "$RASGO_STATE.tmp" "$RASGO_STATE" +answer finish 200 '{}' +expect_ok "run_id: finish" finish +check "run_id: deploy-start and deploy-finish both carry X-Rasgo-Run" test "$(grep -cFx "X-Rasgo-Run: 314" "$FAKE/headers")" = 2 +check "run_id: the header goes with the token, in the header file" test "$(grep -c "X-Rasgo-Run" "$FAKE/args")" = 0 +check "run_id: the bodies stay the same" test "$(jq 'has("run_id")' "$FAKE/request.start.json") $(jq 'has("run_id")' "$FAKE/request.finish.json")" = "false false" + +setup +expect_ok "no run_id: the contract" contract +check "no run_id: empty in the state" test -z "$(state run_id)" +export CORE_TOKEN=t +answer start 201 '{"deployment_id":"01JDEPLOY"}' +expect_ok "no run_id: start" start +jq '.health = "ok"' "$RASGO_STATE" > "$RASGO_STATE.tmp" && mv "$RASGO_STATE.tmp" "$RASGO_STATE" +answer finish 200 '{}' +expect_ok "no run_id: finish" finish +check "no run_id: no request carries X-Rasgo-Run" test "$(grep -c "X-Rasgo-Run" "$FAKE/headers")" = 0 +check "no run_id: the token still goes in both" test "$(grep -cFx "Authorization: Bearer t" "$FAKE/headers")" = 2 +check "no run_id: the contract step says nothing about a run" test "$(grep -c "unido al run" "$OUT")" = 0 + +setup +export RUN_ID="12; rm" +expect_fail "run_id: anything but digits stops at the contract" "no es el número de un run" contract +check "run_id: nothing saved after the refusal" test ! -f "$RASGO_STATE" + # --- health ----------------------------------------------------------------------------------------------------- setup