Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ Every deploy follows the same order, and stops at the first step that fails:
the server (WFA-01.1, D-104). 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`. With `expect: status` it only checks that the site answers, which does not prove the new code is live.
`health.timeout_s`. A JSON answer with a `commit` field, such as a `version.json`, counts only by that commit: the
release before may carry the same version number. With `expect: status` it only checks that the site answers,
which does not prove the new code is live.
6. **deploy-finish.** Closes the deploy in Core with `success`, `failure` or, for a healthy rollback, `rolled_back`.
It runs even when an earlier step failed, so failed deploys are recorded too.

Expand Down
21 changes: 20 additions & 1 deletion actions/deploy-core/core.sh
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,28 @@ cmd_start() {
fail "Core no confirma la aprobación: $(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).
422)
reason="$(jq -r '.error // .message // "petición no válida"' "$response" 2> /dev/null || printf 'petición no válida')"
fail "Core rechaza la petición de deploy-start (422): $reason No se despliega."
;;
*) fail "Core respondió $code a deploy-start. No se despliega." ;;
esac
rm -f "$request" "$response"
}

body_has_version() {
local body="$1" commit version pattern
local body="$1" commit version pattern served
commit="$(state_get commit)"
version="$(state_get version)"
# A JSON body that names its commit (version.json) answers by the commit alone: the release before can carry the same
# version number, as when rasgo-core records the API version and the health host is a static app.
served="$(jq -r 'if type == "object" and has("commit") then .commit | tostring else empty end' "$body" 2>/dev/null || true)"
if [ -n "$served" ]; then
[ "$served" = "$commit" ] && return 0
[ "${#served}" -ge 7 ] && [ "${commit#"$served"}" != "$commit" ] && return 0
return 1
fi
grep -Fq "$commit" "$body" && return 0
grep -Fq "$(short "$commit")" "$body" && return 0
[ -n "$version" ] || return 1
Expand Down Expand Up @@ -300,6 +313,12 @@ cmd_finish() {
note "Core registra el despliegue $id: $result."
summary "- Core: despliegue $id cerrado como $result."
;;
# 409: the deploy is no longer started, as when a retry arrives after Core already closed it. Nothing stays open.
409)
echo "::warning::Core ya tenía cerrado el despliegue $id (409): no se registra otra vez como $result."
summary "- Core: el despliegue $id ya estaba cerrado."
;;
404) fail "Core no conoce el despliegue $id (404): este despliegue ($result) no queda registrado." ;;
*) fail "Core respondió $code a deploy-finish: el despliegue $id ($result) queda abierto en Core." ;;
esac
rm -f "$request" "$response"
Expand Down
50 changes: 50 additions & 0 deletions tests/deploy-core.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ check "start: calls deploy-start on Core" grep -Fxq "https://core.test/api/v1/de
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 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 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

Expand Down Expand Up @@ -269,6 +272,12 @@ export CORE_TOKEN=t
answer start 201 '{}'
expect_fail "start: a 201 without deployment_id stops the deploy" "sin deployment_id" start

setup
core contract
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

# --- health -----------------------------------------------------------------------------------------------------

setup
Expand Down Expand Up @@ -296,6 +305,32 @@ answer health 200 '{"version":"11.4.0"}'
expect_fail "health: another version is not this one (timeout)" "no sirve la versión 1.4.0" health
check "health: state failed" test "$(state health)" = failed

setup
export DEPLOY_ENVIRONMENT=production APPROVAL_ID=a GITHUB_REF_TYPE=tag GITHUB_REF_NAME=acme-v1.4.0
core contract
jq '.health_timeout = "1"' "$RASGO_STATE" > "$RASGO_STATE.tmp" && mv "$RASGO_STATE.tmp" "$RASGO_STATE"
export HEALTH_INTERVAL=1
answer health 200 '{"app":"web","version":"1.4.0","commit":"aaaaaaabbbbbbb"}'
expect_fail "health: the same version from another commit is the old release" "no sirve la versión 1.4.0" health

setup
export DEPLOY_ENVIRONMENT=production APPROVAL_ID=a GITHUB_REF_TYPE=tag GITHUB_REF_NAME=acme-v1.4.0
core contract
answer health 200 "{\"app\":\"web\",\"version\":\"2.0.0\",\"commit\":\"$SHA\"}"
expect_ok "health: with a commit field, the full commit counts whatever the version" health

setup
core contract
answer health 200 "{\"app\":\"web\",\"version\":\"2.0.0\",\"commit\":\"${SHA:0:10}\"}"
expect_ok "health: with a commit field, a prefix of the commit counts" health

setup
core contract
jq '.health_timeout = "1"' "$RASGO_STATE" > "$RASGO_STATE.tmp" && mv "$RASGO_STATE.tmp" "$RASGO_STATE"
export HEALTH_INTERVAL=1
answer health 200 "{\"app\":\"web\",\"commit\":\"${SHA:0:4}\"}"
expect_fail "health: a commit shorter than 7 characters does not count" "no sirve la versión" health

setup
sed -i 's| timeout_s: 5| expect: status\n timeout_s: 5|' rasgo.deploy.yml
core contract
Expand Down Expand Up @@ -362,6 +397,21 @@ jq '.deployment_id = "01JDEPLOY" | .health = "ok"' "$RASGO_STATE" > "$RASGO_STAT
answer finish 500 '{}'
expect_fail "finish: says when the deploy stays open in Core" "queda abierto en Core" finish

setup
core contract
export CORE_TOKEN=t
jq '.deployment_id = "42" | .health = "ok"' "$RASGO_STATE" > "$RASGO_STATE.tmp" && mv "$RASGO_STATE.tmp" "$RASGO_STATE"
answer finish 409 '{"error":"This deployment is already success."}'
expect_ok "finish: a deploy Core already closed (409) does not fail the run" finish
check "finish: warns about the 409" has "ya tenía cerrado el despliegue 42"

setup
core contract
export CORE_TOKEN=t
jq '.deployment_id = "42" | .health = "ok"' "$RASGO_STATE" > "$RASGO_STATE.tmp" && mv "$RASGO_STATE.tmp" "$RASGO_STATE"
answer finish 404 '{"error":"This deployment does not exist."}'
expect_fail "finish: a deploy Core does not know (404)" "no conoce el despliegue 42" finish

setup
core contract
export CORE_TOKEN=t VERSIONS='not json'
Expand Down
Loading