diff --git a/.github/workflows/deploy-laravel.yml b/.github/workflows/deploy-laravel.yml index dd44e51..da668e4 100644 --- a/.github/workflows/deploy-laravel.yml +++ b/.github/workflows/deploy-laravel.yml @@ -1,6 +1,7 @@ -# Reusable RASGO deploy, Laravel on Ploi (RINF-54, WFA-01.1, D-104): reads rasgo.deploy.yml, asks RASGO Core with +# Reusable RASGO deploy, Laravel on Ploi (RINF-54, DEV-12, D-111): reads rasgo.deploy.yml, asks RASGO Core with # deploy-start, calls the Ploi deploy webhook of the site, waits until /version answers with this commit and closes -# the deploy in Core with deploy-finish. Production only deploys with an approval_id that Core confirms. +# the deploy in Core with deploy-finish. Production deploys with Sergio's merge into main, which Core checks in +# deploy-start (D-111). # Also deploys rasgo-core (adapter monorepo-ploi): one Ploi site per environment. # Usage in each repository (.github/workflows/deploy.yml), with BWS_ACCESS_TOKEN (and the variable BWS_PROJECT_ID) in its # staging and production environments: @@ -9,10 +10,10 @@ # on: # push: # branches: [main] +# tags: ['v*'] # workflow_dispatch: # inputs: # environment: { type: choice, options: [production, staging], default: production } -# approval_id: { type: string, required: false } # rollback_of: { type: string, required: false } # task: { type: string, required: false } # run_id: { type: string, required: false } @@ -20,8 +21,7 @@ # deploy: # uses: rasgo-studio/.github/.github/workflows/deploy-laravel.yml@main # with: -# environment: ${{ inputs.environment || 'staging' }} -# approval_id: ${{ inputs.approval_id }} +# environment: ${{ inputs.environment || (github.ref_type == 'tag' && 'production') || 'staging' }} # rollback_of: ${{ inputs.rollback_of }} # task: ${{ inputs.task }} # run_id: ${{ inputs.run_id }} @@ -37,11 +37,11 @@ on: type: string required: true approval_id: - description: Approval of Core for this commit (production, and any environment with approval core) + description: Only for a Core older than RCOR-87, which still asks for an approval; goes once that Core is no longer live type: string default: "" rollback_of: - description: deployment_id of the failed deploy that this one undoes (Core allows it without a new approval) + description: deployment_id of the failed deploy that this one undoes; Core closes it as rolled_back type: string default: "" task: @@ -86,6 +86,7 @@ jobs: cancel-in-progress: false env: DEPLOY_ENVIRONMENT: ${{ inputs.environment }} + DEPLOYER: ${{ github.triggering_actor }} DEPLOY_ADAPTERS: laravel-ploi monorepo-ploi APPROVAL_ID: ${{ inputs.approval_id }} ROLLBACK_OF: ${{ inputs.rollback_of }} @@ -116,12 +117,12 @@ jobs: with: command: secrets args: RASGO_CORE_DEPLOY_TOKEN PLOI_DEPLOY_WEBHOOK - # Ploi pulls the head of its branch, not a commit: only that head can be the approved commit. + # Ploi pulls the head of its branch, not a commit: only that head can be the commit of this run. - name: Branch the server pulls uses: rasgo-studio/.github/actions/deploy-core@main with: command: branch-head - - name: Start (Core checks the approval) + - name: Start (Core checks who deploys) uses: rasgo-studio/.github/actions/deploy-core@main env: CORE_TOKEN: ${{ env.RASGO_CORE_DEPLOY_TOKEN }} diff --git a/.github/workflows/deploy-static.yml b/.github/workflows/deploy-static.yml index d85ab49..3d8717e 100644 --- a/.github/workflows/deploy-static.yml +++ b/.github/workflows/deploy-static.yml @@ -1,7 +1,7 @@ -# Reusable RASGO deploy, static site on Cloudflare Pages (RINF-54, WFA-01.1, D-104): reads rasgo.deploy.yml, builds +# Reusable RASGO deploy, static site on Cloudflare Pages (RINF-54, DEV-12, D-111): reads rasgo.deploy.yml, builds # this commit, asks RASGO Core with deploy-start, uploads the build with wrangler (Direct Upload), waits until the -# health path answers with this commit and closes the deploy in Core with deploy-finish. Production only deploys -# with an approval_id that Core confirms. The build gets a version.json (component, version, commit) unless it has one. +# health path answers with this commit and closes the deploy in Core with deploy-finish. Production deploys with Sergio's merge, which Core +# checks in deploy-start (D-111). The build gets a version.json (component, version, commit) unless it has one. # The Pages project is a Direct Upload project: its Git integration, if any, does not deploy production by itself. # Usage in each repository (.github/workflows/deploy.yml), with BWS_ACCESS_TOKEN (and the variable BWS_PROJECT_ID) in its # staging and production environments: @@ -10,18 +10,17 @@ # on: # push: # branches: [main] +# tags: ['v*'] # workflow_dispatch: # inputs: # environment: { type: choice, options: [production, staging], default: production } -# approval_id: { type: string, required: false } # rollback_of: { type: string, required: false } # task: { type: string, required: false } # jobs: # deploy: # uses: rasgo-studio/.github/.github/workflows/deploy-static.yml@main # with: -# environment: ${{ inputs.environment || 'staging' }} -# approval_id: ${{ inputs.approval_id }} +# environment: ${{ inputs.environment || (github.ref_type == 'tag' && 'production') || 'staging' }} # rollback_of: ${{ inputs.rollback_of }} # task: ${{ inputs.task }} # pages-project: example-org @@ -37,11 +36,11 @@ on: type: string required: true approval_id: - description: Approval of Core for this commit (production, and any environment with approval core) + description: Only for a Core older than RCOR-87, which still asks for an approval; goes once that Core is no longer live type: string default: "" rollback_of: - description: deployment_id of the failed deploy that this one undoes (Core allows it without a new approval) + description: deployment_id of the failed deploy that this one undoes; Core closes it as rolled_back type: string default: "" task: @@ -101,6 +100,7 @@ jobs: cancel-in-progress: false env: DEPLOY_ENVIRONMENT: ${{ inputs.environment }} + DEPLOYER: ${{ github.triggering_actor }} DEPLOY_ADAPTERS: static APPROVAL_ID: ${{ inputs.approval_id }} ROLLBACK_OF: ${{ inputs.rollback_of }} @@ -115,7 +115,7 @@ jobs: uses: rasgo-studio/.github/actions/deploy-core@main with: command: contract - # The build comes before the secrets and deploy-start, so a broken build does not spend the approval. + # The build comes before the secrets and deploy-start, so a broken build never reaches Core. - uses: actions/setup-node@v4 with: node-version: ${{ inputs.node-version }} @@ -138,7 +138,7 @@ jobs: with: command: secrets args: RASGO_CORE_DEPLOY_TOKEN CLOUDFLARE_API_TOKEN CLOUDFLARE_ACCOUNT_ID - - name: Start (Core checks the approval) + - name: Start (Core checks who deploys) uses: rasgo-studio/.github/actions/deploy-core@main env: CORE_TOKEN: ${{ env.RASGO_CORE_DEPLOY_TOKEN }} diff --git a/.github/workflows/deploy-wordpress.yml b/.github/workflows/deploy-wordpress.yml index 5b29f53..e8c8763 100644 --- a/.github/workflows/deploy-wordpress.yml +++ b/.github/workflows/deploy-wordpress.yml @@ -1,24 +1,24 @@ -# Reusable RASGO deploy, WordPress on Ploi (RINF-54, WFA-01.1, D-104): reads rasgo.deploy.yml, asks RASGO Core with +# Reusable RASGO deploy, WordPress on Ploi (RINF-54, DEV-12, D-111): reads rasgo.deploy.yml, asks RASGO Core with # deploy-start, calls the Ploi deploy webhook of the site (its deploy script pulls the repository and builds the -# theme), checks the health path and closes the deploy in Core with deploy-finish. Production only deploys with an -# approval_id that Core confirms. Since RINF-4 an Action no longer reaches the servers by SSH: Ploi does the deploy. +# theme), checks the health path and closes the deploy in Core with deploy-finish. Production deploys with Sergio's +# merge of the version PR, which publishes the tag, and Core checks it in deploy-start (D-111). Since RINF-4 an Action no longer reaches the servers by SSH: Ploi does the deploy. # Usage in each repository (.github/workflows/deploy.yml), with BWS_ACCESS_TOKEN (and the variable BWS_PROJECT_ID) in its # staging and production environments: # # name: Deploy # on: +# push: +# tags: ['v*'] # workflow_dispatch: # inputs: # environment: { type: choice, options: [production, staging], default: production } -# approval_id: { type: string, required: false } # rollback_of: { type: string, required: false } # task: { type: string, required: false } # jobs: # deploy: # uses: rasgo-studio/.github/.github/workflows/deploy-wordpress.yml@main # with: -# environment: ${{ inputs.environment }} -# approval_id: ${{ inputs.approval_id }} +# environment: ${{ inputs.environment || (github.ref_type == 'tag' && 'production') || 'staging' }} # rollback_of: ${{ inputs.rollback_of }} # task: ${{ inputs.task }} # secrets: inherit @@ -33,11 +33,11 @@ on: type: string required: true approval_id: - description: Approval of Core for this commit (production, and any environment with approval core) + description: Only for a Core older than RCOR-87, which still asks for an approval; goes once that Core is no longer live type: string default: "" rollback_of: - description: deployment_id of the failed deploy that this one undoes (Core allows it without a new approval) + description: deployment_id of the failed deploy that this one undoes; Core closes it as rolled_back type: string default: "" task: @@ -74,6 +74,7 @@ jobs: cancel-in-progress: false env: DEPLOY_ENVIRONMENT: ${{ inputs.environment }} + DEPLOYER: ${{ github.triggering_actor }} DEPLOY_ADAPTERS: wordpress APPROVAL_ID: ${{ inputs.approval_id }} ROLLBACK_OF: ${{ inputs.rollback_of }} @@ -102,12 +103,12 @@ jobs: with: command: secrets args: RASGO_CORE_DEPLOY_TOKEN PLOI_DEPLOY_WEBHOOK - # Ploi pulls the head of its branch, not a commit: only that head can be the approved commit. + # Ploi pulls the head of its branch, not a commit: only that head can be the commit of this run. - name: Branch the server pulls uses: rasgo-studio/.github/actions/deploy-core@main with: command: branch-head - - name: Start (Core checks the approval) + - name: Start (Core checks who deploys) uses: rasgo-studio/.github/actions/deploy-core@main env: CORE_TOKEN: ${{ env.RASGO_CORE_DEPLOY_TOKEN }} diff --git a/README.md b/README.md index 39d0c36..2e724e8 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ deploy workflows (`.github/workflows/deploy-*.yml`), called from each repository ## Deploy contract: `rasgo.deploy.yml` Each repository that deploys keeps a `rasgo.deploy.yml` at its root: what it is, how it deploys and how a deploy -is checked (RINF-54). The reusable deploy workflows read it, and RASGO Core shows it in each approval. Version 1 +is checked (RINF-54). The reusable deploy workflows read it, and RASGO Core keeps it with each deploy. Version 1 keeps only what exists today: the adapters are the current ways of deploying, behind one contract. Its schema is `schemas/deploy-contract.json`. @@ -18,16 +18,18 @@ keeps only what exists today: the adapters are the current ways of deploying, be | `component` | yes | Its name in the Registry of RASGO Core: the repository, or the site it publishes | | `stack` | yes | `laravel`, `wordpress`, `static`, `monorepo` or `n8n`, as the `stack` property of the repository (DEV-02.2) | | `adapter` | yes | `laravel-ploi` (Ploi deploy webhook), `wordpress` (Ploi deploy webhook too), `static` (Cloudflare Pages), `monorepo-ploi` (rasgo-core, one Ploi site per environment) or `n8n` (rasgo-motor) | -| `environments` | yes | `staging` and `production`, each with `ref` (a branch, or `tag` for the published release), `url`, `approval` and `secrets` | -| `migrations` | no | The command the deploy runs after the code; it is shown in the approval | +| `environments` | yes | `staging` and `production`, each with `ref` (a branch, or `tag` for the published release), `url`, `approval` (`merge` or `none`) and `secrets` | +| `migrations` | no | The command the deploy runs after the code; the record of the deploy in Core shows it | | `health` | yes | `path` on the environment `url` that answers with the deployed version (`expect: version`), or only its status | -| `rollback` | yes | `previous-tag` (a deploy of the tag before: Core allows it without a new approval up to 24 hours after the failed deploy) or `none` (going back is not done by the system) | +| `rollback` | yes | `previous-tag` (a deploy of the tag before, launched by Sergio, which Core closes as `rolled_back`) or `none` (going back is not done by the system) | Rules of the contract: -- **Production always deploys with an approval of Core** (`approval: core`): the deploy workflow asks Core - (`deploy-start`) whether the `approval_id` exists, has not expired and is for the same commit, and stops - before touching the server if not (WFA-01.1, level 3; D-104). Staging may be automatic (`approval: none`, level 2). +- **Production deploys with Sergio's merge, which is the approval** (`approval: merge`, D-111): the deploy workflow + sends Core (`deploy-start`) the GitHub person who merged or launched the run, and Core stops it before it touches + the server unless that person is Sergio (DEV-12). There is no approval in the Hub for a deploy. Staging may be + automatic (`approval: none`, level 2). `approval: core`, the name before D-111, still reads as `merge` until + rasgo-wp-base and the sites made from it change their contract. - **Names, never values** (DEV-07): `secrets` lists the names of the environment secrets the adapter reads, such as the Ploi deploy webhook. No host, path with a secret, token or key goes in the contract. - This repository is public (DEV-02.2): the examples here carry no client names; each repository writes its own. @@ -48,7 +50,7 @@ environments: production: ref: tag url: https://example.com - approval: core + approval: merge secrets: [PLOI_DEPLOY_WEBHOOK] migrations: php artisan migrate --force health: @@ -68,7 +70,7 @@ environments: production: ref: tag url: https://example.com - approval: core + approval: merge secrets: [PLOI_DEPLOY_WEBHOOK] health: path: / @@ -93,7 +95,7 @@ environments: production: ref: tag url: https://example.org - approval: core + approval: merge secrets: [CLOUDFLARE_API_TOKEN, CLOUDFLARE_ACCOUNT_ID] health: path: /version.json @@ -114,12 +116,13 @@ the one of its `adapter` from its own `.github/workflows/deploy.yml`; the header Every deploy follows the same order, and stops at the first step that fails: 1. **Contract.** Reads `rasgo.deploy.yml` and checks it against the run: the adapter is the one of the workflow, the - environment exists, production has `approval: core`, an environment with `approval: core` got an `approval_id`, - and the run comes from what the environment deploys (its branch, or a tag when `ref: tag`). + environment exists, production has `approval: merge` and a GitHub person who merged or launched the run, and the + run comes from what the environment deploys (its branch, or a tag when `ref: tag`). 2. **Secrets.** The secrets the adapter reads come from Bitwarden Secrets Manager only (see Secrets below). In `deploy-static.yml` this step runs after the build. -3. **deploy-start.** Asks RASGO Core with the repository, app, environment, commit and `approval_id`. Core answers with - a `deployment_id`, or with 403 and the reason (`pending`, `expired`, `used`, `mismatch`…), and then nothing touches - the server (WFA-01.1, D-104). If Core does not answer, nothing deploys: there is no emergency exit. +3. **deploy-start.** Asks RASGO Core with the repository, app, environment, commit and the GitHub person + (`deployed_by`: `github.triggering_actor`). Core answers with a `deployment_id`, or with 403 and the reason + (`not_allowed` when that person is not Sergio), and then nothing touches the server (D-111, DEV-12). If Core does + not answer, nothing deploys: there is no emergency exit. 4. **Deploy.** The adapter: the Ploi webhook, or the upload to Cloudflare Pages. 5. **Health.** Asks `url` + `health.path` until it answers with this commit (full or short) or this version, up to `health.timeout_s`. A JSON answer with a `commit` field, such as a `version.json`, counts only by that commit: the @@ -130,10 +133,10 @@ Every deploy follows the same order, and stops at the first step that fails: Calling them: -- **Inputs.** `environment` (required), `approval_id`, `rollback_of` (the `deployment_id` of the failed deploy that - this one undoes) and `task` (the Plane code, so Core can report on it), plus `app`, `version` and, for a monorepo, +- **Inputs.** `environment` (required), `rollback_of` (the `deployment_id` of the failed deploy that 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`. + `build-command`, `output-dir`, `production-branch` and `staging-branch`. `approval_id` is only for a Core older + than RCOR-87, which still asks for an approval; it goes once that Core is no longer live. - **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 @@ -157,14 +160,14 @@ Calling them: `BWS_PROJECT_ID` (read only that project) and `BWS_SERVER_URL` (another region). - **Calling.** With `secrets: inherit`, or passing `BWS_ACCESS_TOKEN` explicitly under `secrets:`. - The repository variable `RASGO_CORE_URL` points to another Core, for tests. -- **Production** runs from `workflow_dispatch` once Sergio has approved it in the Hub, on the tag it deploys: - `gh workflow run deploy.yml --ref acme-v1.4.0 -f environment=production -f approval_id=`. Each approval serves - one deploy of one commit and lasts 24 hours (spec 003 of rasgo-core). Staging with `approval: none` can deploy on - each push to its branch. +- **Production** runs on Sergio's merge: the push to the branch the contract deploys, or the tag that the merge of + the version PR publishes (DEV-12 12.3). To relaunch or go back, Sergio runs `workflow_dispatch` on what he wants + live: `gh workflow run deploy.yml --ref acme-v1.4.0 -f environment=production`. Staging with `approval: none` can + deploy on each push to its branch. - **Ploi deploys the head of its branch**, not a given commit. So both Ploi workflows stop before deploy-start when that head (`ref`, or `ploi-branch` for a tag, `main` by default) is not the commit of the run: with release-please, deploy the tag right after merging the release. For the same reason a Ploi rollback to an older tag cannot pass; - going back there is a revert merged into the branch, deployed with a new approval, or the rollback of the Ploi panel + going back there is a revert merged into the branch, which deploys like any merge, or the rollback of the Ploi panel (Sergio); their contracts say `rollback: none`. `deploy-static.yml` builds the commit it runs on, so `rollback: previous-tag` works there. - **Static sites** get `version.json` (`component`, `version`, `commit`) in the published folder unless the build diff --git a/actions/deploy-core/core.sh b/actions/deploy-core/core.sh index d1e9609..a320f4f 100755 --- a/actions/deploy-core/core.sh +++ b/actions/deploy-core/core.sh @@ -1,16 +1,18 @@ #!/usr/bin/env bash -# Steps shared by the reusable deploy workflows of rasgo-studio (RINF-54, WFA-01.1, D-104). +# Steps shared by the reusable deploy workflows of rasgo-studio (RINF-54, RINF-91, DEV-12, D-111). # # core.sh require NAME... the named environment variables (the secrets the adapter reads) are set # core.sh secrets NAME... the same, read from Bitwarden Secrets Manager with BWS_ACCESS_TOKEN (D-109) # core.sh contract reads rasgo.deploy.yml, checks it against this run and saves the deploy state # core.sh branch-head the branch the Ploi site pulls is still at the commit of this run # core.sh version-file [DIR] writes DIR (or OUTPUT_DIR)/version.json with component, version and commit -# core.sh start deploy-start in RASGO Core: it only goes on with an approval Core confirms +# core.sh start deploy-start in RASGO Core: production only goes on when Core accepts who deploys # core.sh health waits until the environment answers with this commit or version # core.sh finish deploy-finish in RASGO Core with the result # # Core is the barrier: without an answer from Core nothing deploys, and there is no emergency exit (RINF-54). +# Production deploys with Sergio's merge, which is the approval (D-111): deploy-start carries the GitHub person who +# merged or launched the run (DEPLOYER, else GITHUB_TRIGGERING_ACTOR, else GITHUB_ACTOR) and Core refuses anyone else. # 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). @@ -172,7 +174,7 @@ cmd_secrets() { 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 run_id + local adapter component ref url approval path expect timeout version app branch public tag run_id deployer case "$env" in staging | production) ;; @@ -197,12 +199,21 @@ cmd_contract() { [ -n "$ref" ] && [ -n "$url" ] || fail "Al entorno $env le falta ref o url." case "$url" in https://*) ;; *) fail "La url de $env no es https." ;; esac - # Production always deploys with an approval of Core (WFA-01.1, level 3), whatever the contract says. - if [ "$env" = production ] && [ "$approval" != core ]; then - fail "Producción solo despliega con aprobación de Core (approval: core, WFA-01.1); el contrato dice «$approval»." + # Production deploys with Sergio's merge (approval: merge, D-111). «core» is the name it had before D-111: it still + # reads as merge until rasgo-wp-base and the sites made from it change their contract. + case "$approval" in + merge | none) ;; + core) approval=merge ;; + *) fail "approval «$approval» desconocida: el contrato v1 tiene merge o none." ;; + esac + if [ "$env" = production ] && [ "$approval" != merge ]; then + fail "Producción solo despliega con el merge de Sergio (approval: merge, D-111); el contrato dice «$approval»." fi - if [ "$approval" = core ] && [ -z "${APPROVAL_ID:-}" ] && [ -z "${ROLLBACK_OF:-}" ]; then - fail "Falta approval_id: $env solo despliega con una aprobación de Core para este commit (WFA-01.1). Pídela con approval.request y lanza el workflow con ella." + + # Who merged or launched this run. Core checks it in deploy-start; without it production does not deploy. + deployer="${DEPLOYER:-${GITHUB_TRIGGERING_ACTOR:-${GITHUB_ACTOR:-}}}" + if [ "$approval" = merge ] && [ -z "$deployer" ]; then + fail "No sé qué persona de GitHub lanzó esta ejecución: $env solo despliega con el merge de Sergio (D-111)." fi # What runs is what the contract says deploys: the published tag, or that branch. @@ -244,13 +255,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:-}" --arg run_id "$run_id" --argjson contract "$public" \ + --arg rollback_of "${ROLLBACK_OF:-}" --arg task "${TASK:-}" --arg run_id "$run_id" --arg deployed_by "$deployer" \ + --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, run_id: $run_id, contract: $contract}' > "$STATE" + rollback_of: $rollback_of, task: $task, run_id: $run_id, deployed_by: $deployed_by, contract: $contract}' > "$STATE" - note "Contrato correcto: $component ($adapter) a $env, $url, versión $version, commit $(short "$GITHUB_SHA"), aprobación: $approval." + note "Contrato correcto: $component ($adapter) a $env, $url, versión $version, commit $(short "$GITHUB_SHA"), aprobación: $approval${deployer:+, lo despliega $deployer}." 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")." } @@ -262,7 +274,7 @@ cmd_branch_head() { head="$(git ls-remote --exit-code origin "refs/heads/$branch" | cut -f1)" \ || fail "No encuentro la rama $branch en el repositorio." [ "$head" = "$GITHUB_SHA" ] \ - || fail "Ploi despliega la cabeza de $branch ($(short "$head")) y esta ejecución es de $(short "$GITHUB_SHA"): el servidor no recibiría el commit aprobado. No se despliega." + || fail "Ploi despliega la cabeza de $branch ($(short "$head")) y esta ejecución es de $(short "$GITHUB_SHA"): el servidor no recibiría el commit de esta ejecución. No se despliega." note "La rama $branch está en $(short "$head"), el commit de esta ejecución." } @@ -278,8 +290,11 @@ cmd_version_file() { note "Escrito $dir/version.json con la versión $(state_get version)." } +# Why Core refuses. not_allowed is the answer since D-111; the others are the approval of Core before RCOR-87, which +# reads approval_id, and go with the input approval_id once that Core is no longer live. reason_text() { case "$1" in + not_allowed) printf 'solo despliega a producción el merge o la ejecución de Sergio' ;; missing) printf 'falta approval_id' ;; not_found) printf 'esa aprobación no existe' ;; pending) printf 'la aprobación sigue pendiente: apruébala en el Hub y vuelve a lanzarlo' ;; @@ -301,6 +316,7 @@ cmd_start() { jq --arg repository "${GITHUB_REPOSITORY:?}" \ --arg run_url "${GITHUB_SERVER_URL:-https://github.com}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID:-0}" \ '{repository: $repository, app, environment, commit_sha: .commit, run_url: $run_url, contract} + + (if .deployed_by != "" then {deployed_by} else {} end) + (if .approval_id != "" then {approval_id} else {} end) + (if .rollback_of != "" then {rollback_of} else {} end) + (if .task != "" then {task} else {} end)' "$STATE" > "$request" @@ -317,8 +333,8 @@ cmd_start() { ;; 403) reason="$(jq -r '.reason // "desconocido"' "$response" 2> /dev/null || printf 'desconocido')" - rule="$(jq -r '.rule // "WFA-01.1"' "$response" 2> /dev/null || printf 'WFA-01.1')" - fail "Core no confirma la aprobación: $(reason_text "$reason") ($rule). No se toca el servidor." + rule="$(jq -r '.rule // "D-111"' "$response" 2> /dev/null || printf 'D-111')" + fail "Core no deja desplegar: $(reason_text "$reason") ($rule). No se toca el servidor." ;; 401) fail "Core no acepta el token de despliegue (401): revisa RASGO_CORE_DEPLOY_TOKEN. No se despliega." ;; # 422: the request does not validate, such as a contract key that looks like a secret (PLN-11). diff --git a/schemas/deploy-contract.json b/schemas/deploy-contract.json index ccb7093..d2af8a1 100644 --- a/schemas/deploy-contract.json +++ b/schemas/deploy-contract.json @@ -31,7 +31,7 @@ } }, "migrations": { - "description": "Command the deploy runs after the code, or none. Shown in the approval.", + "description": "Command the deploy runs after the code, or none. The record of the deploy in Core shows it.", "type": "string" }, "health": { @@ -70,8 +70,8 @@ }, "url": { "type": "string", "pattern": "^https://" }, "approval": { - "description": "core: only with an approval_id that Core confirms (level 3, WFA-01.1). none: automatic (level 2), staging only.", - "enum": ["core", "none"] + "description": "merge: deploys with Sergio's merge or run, which Core checks in deploy-start (D-111). none: automatic (level 2), staging only. core: the name of merge before D-111, read as merge until rasgo-wp-base and the sites made from it change their contract.", + "enum": ["merge", "none", "core"] }, "secrets": { "description": "Names of the environment secrets the adapter reads (for example the Ploi deploy webhook). Never values.", @@ -84,9 +84,9 @@ }, "allOf": [ { - "description": "Production always deploys with an approval of Core.", + "description": "Production always deploys with Sergio's merge (D-111).", "if": { "required": ["environments"], "properties": { "environments": { "required": ["production"] } } }, - "then": { "properties": { "environments": { "properties": { "production": { "required": ["approval"], "properties": { "approval": { "const": "core" } } } } } } } + "then": { "properties": { "environments": { "properties": { "production": { "required": ["approval"], "properties": { "approval": { "enum": ["merge", "core"] } } } } } } } } ] } diff --git a/tests/deploy-core.test.sh b/tests/deploy-core.test.sh index 7a166a7..d49b341 100644 --- a/tests/deploy-core.test.sh +++ b/tests/deploy-core.test.sh @@ -80,7 +80,7 @@ environments: production: ref: tag url: https://example.com/ - approval: core + approval: merge secrets: [PLOI_DEPLOY_WEBHOOK] migrations: php artisan migrate --force health: @@ -88,7 +88,8 @@ health: timeout_s: 5 rollback: previous-tag EOF - unset APPROVAL_ID ROLLBACK_OF TASK RUN_ID 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 DEPLOYER + export GITHUB_ACTOR=sergio-cortinas GITHUB_TRIGGERING_ACTOR=sergio-cortinas 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" @@ -140,16 +141,19 @@ check "contract: app defaults to the component" test "$(state app)" = acme-porta setup export DEPLOY_ENVIRONMENT=production GITHUB_REF_TYPE=tag GITHUB_REF_NAME=acme-v1.4.0 -expect_fail "contract: production needs an approval_id" "Falta approval_id" contract -check "contract: the message cites WFA-01.1" has "WFA-01.1" +unset GITHUB_ACTOR GITHUB_TRIGGERING_ACTOR +expect_fail "contract: production needs the person who launched the run" "solo despliega con el merge de Sergio" contract +check "contract: the message cites D-111" has "D-111" setup -export DEPLOY_ENVIRONMENT=production APPROVAL_ID=01JAPPROVAL +export DEPLOY_ENVIRONMENT=production expect_fail "contract: production deploys a tag, not a branch" "etiqueta publicada" contract setup -export DEPLOY_ENVIRONMENT=production APPROVAL_ID=01JAPPROVAL GITHUB_REF_TYPE=tag GITHUB_REF_NAME=acme-v1.4.0 -expect_ok "contract: production on a tag with an approval" contract +export DEPLOY_ENVIRONMENT=production GITHUB_REF_TYPE=tag GITHUB_REF_NAME=acme-v1.4.0 GITHUB_ACTOR=release-bot +expect_ok "contract: production on a tag, merged by a person, needs no approval_id" contract +check "contract: the person is the one who launched the run" test "$(state deployed_by)" = sergio-cortinas +check "contract: approval is merge" test "$(state approval)" = merge check "contract: version is the number of the tag" test "$(state version)" = "1.4.0" check "contract: a tag deploy follows the Ploi branch (main)" test "$(state branch)" = main check "contract: the trailing slash of the url goes" test "$(state url)" = "https://example.com" @@ -160,9 +164,25 @@ expect_ok "contract: a rollback goes on without approval_id (Core decides)" cont check "contract: PLOI_BRANCH for a tag" test "$(state branch)" = release setup -sed -i 's/approval: core/approval: none/' rasgo.deploy.yml -export DEPLOY_ENVIRONMENT=production APPROVAL_ID=01JAPPROVAL GITHUB_REF_TYPE=tag GITHUB_REF_NAME=v1.4.0 -expect_fail "contract: production without approval core is refused" "Producción solo despliega" contract +export DEPLOY_ENVIRONMENT=production DEPLOYER=other-person GITHUB_REF_TYPE=tag GITHUB_REF_NAME=v1.4.0 +expect_ok "contract: DEPLOYER names the person when it is set" contract +check "contract: DEPLOYER wins" test "$(state deployed_by)" = other-person + +setup +sed -i 's/approval: merge/approval: core/' rasgo.deploy.yml +export DEPLOY_ENVIRONMENT=production GITHUB_REF_TYPE=tag GITHUB_REF_NAME=v1.4.0 +expect_ok "contract: approval core, the name before D-111, reads as merge" contract +check "contract: core is recorded as merge" test "$(state approval)" = merge + +setup +sed -i 's/approval: merge/approval: none/' rasgo.deploy.yml +export DEPLOY_ENVIRONMENT=production GITHUB_REF_TYPE=tag GITHUB_REF_NAME=v1.4.0 +expect_fail "contract: production without approval merge is refused" "Producción solo despliega con el merge de Sergio" contract + +setup +sed -i 's/approval: merge/approval: hub/' rasgo.deploy.yml +export DEPLOY_ENVIRONMENT=production GITHUB_REF_TYPE=tag GITHUB_REF_NAME=v1.4.0 +expect_fail "contract: an unknown approval is refused" "approval «hub» desconocida" contract setup export DEPLOY_ADAPTERS=static @@ -283,40 +303,45 @@ core contract printf '%s' "$SHA" > "$FAKE/head" expect_ok "branch-head: the branch is at the commit of the run" branch-head printf '%s' "fedcba9876543210fedcba9876543210fedcba98" > "$FAKE/head" -expect_fail "branch-head: a branch that moved stops the deploy" "no recibiría el commit aprobado" branch-head +expect_fail "branch-head: a branch that moved stops the deploy" "no recibiría el commit de esta ejecución" branch-head # --- start ------------------------------------------------------------------------------------------------------ setup -export DEPLOY_ENVIRONMENT=production APPROVAL_ID=01JAPPROVAL TASK=ACME01WEB-23 GITHUB_REF_TYPE=tag GITHUB_REF_NAME=v1.4.0 +export DEPLOY_ENVIRONMENT=production TASK=ACME01WEB-23 GITHUB_REF_TYPE=tag GITHUB_REF_NAME=v1.4.0 core contract export CORE_TOKEN=token-secret-value answer start 201 '{"deployment_id":"01JDEPLOY"}' -expect_ok "start: Core confirms the approval" start +expect_ok "start: Core accepts the person who deploys" start check "start: the deployment_id is saved" test "$(state deployment_id)" = 01JDEPLOY check "start: calls deploy-start on Core" grep -Fxq "https://core.test/api/v1/deploy-start" "$FAKE/urls" -check "start: the body carries the approval tuple" test "$(jq -r '[.repository, .app, .environment, .commit_sha, .approval_id, .task] | join(" ")' "$FAKE/request.start.json")" = "rasgo-studio/acme-portal acme-portal production $SHA 01JAPPROVAL ACME01WEB-23" +check "start: the body carries what deploys and who" test "$(jq -r '[.repository, .app, .environment, .commit_sha, .deployed_by, .task] | join(" ")' "$FAKE/request.start.json")" = "rasgo-studio/acme-portal acme-portal production $SHA sergio-cortinas ACME01WEB-23" +check "start: no approval_id when there is none" test "$(jq 'has("approval_id")' "$FAKE/request.start.json")" = false check "start: the body carries the run url" test "$(jq -r .run_url "$FAKE/request.start.json")" = "https://github.com/rasgo-studio/acme-portal/actions/runs/42" check "start: no rollback_of when there is none" test "$(jq 'has("rollback_of")' "$FAKE/request.start.json")" = false # Core answers 422 when any key of the contract looks like a secret (DeployContractCheck of rasgo-core, PLN-11). check "start: the contract goes without keys that look like secrets" test "$(jq '[.contract | paths | .[] | strings | select(test("secret|token|password|passwd|private[_-]?key|api[_-]?key|credential"; "i"))] | length' "$FAKE/request.start.json")" = 0 -check "start: the contract keeps the rest" test "$(jq -r '.contract.environments.production.approval' "$FAKE/request.start.json")" = core +check "start: the contract keeps the rest" test "$(jq -r '.contract.environments.production.approval' "$FAKE/request.start.json")" = merge check "start: the token goes in a header" grep -Fq "Authorization: Bearer token-secret-value" "$FAKE/headers" check "start: the token is not on the command line" test "$(grep -c token-secret-value "$FAKE/args")" = 0 setup +export DEPLOY_ENVIRONMENT=production GITHUB_REF_TYPE=tag GITHUB_REF_NAME=v1.4.0 GITHUB_TRIGGERING_ACTOR=someone-else core contract export CORE_TOKEN=t -answer start 403 '{"error":"Approval already used","reason":"used","rule":"WFA-01.1"}' -expect_fail "start: a used approval stops the deploy" "ya se usó" start -check "start: the rule is shown" has "(WFA-01.1)" +answer start 403 '{"error":"Only Sergio deploys to production.","reason":"not_allowed","rule":"D-111"}' +expect_fail "start: Core refuses anyone but Sergio" "solo despliega a producción el merge o la ejecución de Sergio" start +check "start: the rule is shown" has "(D-111)" check "start: no deployment_id after a 403" test -z "$(state deployment_id)" +# The Core before RCOR-87 still asks for approval_id: the input goes through while that Core is live. setup +export DEPLOY_ENVIRONMENT=production APPROVAL_ID=01JAPPROVAL GITHUB_REF_TYPE=tag GITHUB_REF_NAME=v1.4.0 core contract export CORE_TOKEN=t -answer start 403 '{"reason":"mismatch","rule":"WFA-01.1"}' -expect_fail "start: an approval for another commit stops the deploy" "otro repositorio, app, entorno o commit" start +answer start 403 '{"reason":"used","rule":"WFA-01.1"}' +expect_fail "start: the Core before RCOR-87 still explains its approval" "ya se usó" start +check "start: approval_id goes to that Core" test "$(jq -r .approval_id "$FAKE/request.start.json")" = 01JAPPROVAL setup core contract