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
102 changes: 63 additions & 39 deletions .github/workflows/deploy-gate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
# job, so the reviewer approves the job that holds the credentials, after this
# gate has run in the same workflow run, and a workflow edit cannot remove it.
#
# The range starts at the newest successful deployment of that environment,
# which GitHub records for every job that uses it. A first deploy has none:
# pass base_sha. The claims check always enforces here; there is no report mode.
# The range starts at the newest successful deployment of that environment
# whose commit is an ancestor of, or equal to, the commit being deployed. GitHub
# records a deployment for every job that uses an environment. base_sha is used
# only when no such deployment exists (a first deploy). The claims check always
# enforces here; there is no report mode.
name: deploy gate

on:
Expand All @@ -35,9 +37,10 @@ on:
required: true
base_sha:
description: >-
Full 40-character SHA to check from. Leave empty to use the newest
successful deployment of the environment. Set it only for a first
deploy, when no deployment exists.
Full 40-character SHA to check from, for a first deploy only. It is
used only when no successful deployment of the environment is an
ancestor of the commit being deployed, and it must be a strict
ancestor of that commit. Otherwise it is ignored with a warning.
type: string
default: ''
runner:
Expand Down Expand Up @@ -141,42 +144,61 @@ jobs:
esac
HEAD_SHA=$(git rev-parse --verify -q 'HEAD^{commit}') || fail "no checked-out commit"

BASE=""
# base_sha is a fallback for a first deploy, never an override.
if [ -n "${BASE_INPUT:-}" ]; then
[[ "$BASE_INPUT" =~ ^[0-9a-f]{40}$ ]] || fail "input base_sha must be a full 40-character lower-case commit SHA"
BASE="$BASE_INPUT"
SOURCE="input base_sha"
fi

WORK=$(mktemp -d "${RUNNER_TEMP:-/tmp}/gate.XXXXXX")
trap 'rm -rf -- "$WORK"' EXIT
api() {
curl -sS --max-time 20 -o "$WORK/body.json" -w '%{http_code}' \
-H "Authorization: Bearer ${GATE_TOKEN}" \
-H 'Accept: application/vnd.github+json' "${API_URL}$1" 2> /dev/null
}
# Deployments come newest first. The base is the first one whose latest
# status is success and whose commit is an ancestor of, or equal to, the
# commit being deployed. A newer deployment that is not an ancestor (the
# deploy is a rollback) is skipped, so a commit that was never deployed
# is checked from the deployed commit before it. Three pages of 30.
BASE=""
SOURCE=""
SKIPPED=0
for page in 1 2 3; do
code=$(api "/repos/${REPO}/deployments?environment=${ENVIRONMENT}&per_page=30&page=${page}") || code=000
[ "$code" = 200 ] || fail "could not list the deployments of ${ENVIRONMENT} (HTTP ${code}); the caller must grant permissions: deployments: read"
jq -r '.[] | "\(.id) \(.sha)"' "$WORK/body.json" > "$WORK/page.txt" 2> /dev/null \
|| fail "unreadable deployments list"
[ -s "$WORK/page.txt" ] || break
while read -r id sha; do
[[ "$id" =~ ^[0-9]+$ ]] || fail "malformed deployment id"
[[ "$sha" =~ ^[0-9a-f]{40}$ ]] || fail "malformed SHA in deployment ${id}"
code=$(api "/repos/${REPO}/deployments/${id}/statuses?per_page=1") || code=000
[ "$code" = 200 ] || fail "could not read the statuses of deployment ${id} (HTTP ${code})"
state=$(jq -r '.[0].state // ""' "$WORK/body.json" 2> /dev/null) || fail "unreadable statuses of deployment ${id}"
[ "$state" = success ] || continue
if git merge-base --is-ancestor "$sha" "$HEAD_SHA" 2> /dev/null; then
BASE="$sha"
SOURCE="deployment ${id}"
break
fi
SKIPPED=$((SKIPPED + 1))
done < "$WORK/page.txt"
if [ -n "$BASE" ]; then break; fi
done

if [ -n "$BASE" ]; then
if [ -n "${BASE_INPUT:-}" ]; then
echo "::warning::deploy gate: input base_sha ignored; ${SOURCE} at ${BASE} is the base"
SOURCE="${SOURCE}; input base_sha ignored"
fi
else
WORK=$(mktemp -d "${RUNNER_TEMP:-/tmp}/gate.XXXXXX")
trap 'rm -rf -- "$WORK"' EXIT
api() {
curl -sS --max-time 20 -o "$WORK/body.json" -w '%{http_code}' \
-H "Authorization: Bearer ${GATE_TOKEN}" \
-H 'Accept: application/vnd.github+json' "${API_URL}$1" 2> /dev/null
}
# Deployments come newest first. The first one whose latest status
# is success is the last deploy. Three pages of 30 bound the walk.
for page in 1 2 3; do
code=$(api "/repos/${REPO}/deployments?environment=${ENVIRONMENT}&per_page=30&page=${page}") || code=000
[ "$code" = 200 ] || fail "could not list the deployments of ${ENVIRONMENT} (HTTP ${code}); the caller must grant permissions: deployments: read"
jq -r '.[] | "\(.id) \(.sha)"' "$WORK/body.json" > "$WORK/page.txt" 2> /dev/null \
|| fail "unreadable deployments list"
[ -s "$WORK/page.txt" ] || break
while read -r id sha; do
[[ "$id" =~ ^[0-9]+$ ]] || fail "malformed deployment id"
code=$(api "/repos/${REPO}/deployments/${id}/statuses?per_page=1") || code=000
[ "$code" = 200 ] || fail "could not read the statuses of deployment ${id} (HTTP ${code})"
state=$(jq -r '.[0].state // ""' "$WORK/body.json" 2> /dev/null) || fail "unreadable statuses of deployment ${id}"
if [ "$state" = success ]; then
BASE="$sha"
SOURCE="deployment ${id}"
break
fi
done < "$WORK/page.txt"
if [ -n "$BASE" ]; then break; fi
done
[ -n "$BASE" ] || fail "no successful deployment of ${ENVIRONMENT} in the newest 90; for a first deploy set input base_sha"
[[ "$BASE" =~ ^[0-9a-f]{40}$ ]] || fail "the deployment SHA is malformed"
[ -n "${BASE_INPUT:-}" ] || fail "no successful deployment of ${ENVIRONMENT} at an ancestor of ${HEAD_SHA} in the newest 90; for a first deploy set input base_sha"
[ "$BASE_INPUT" != "$HEAD_SHA" ] || fail "input base_sha is the commit being deployed; it must be an earlier commit"
git merge-base --is-ancestor "$BASE_INPUT" "$HEAD_SHA" 2> /dev/null \
|| fail "input base_sha ${BASE_INPUT} is not an ancestor of ${HEAD_SHA}"
BASE="$BASE_INPUT"
SOURCE="input base_sha, no deployed ancestor"
fi
git cat-file -e "${BASE}^{commit}" 2> /dev/null || fail "base ${BASE} is not in this repository's history"

Expand All @@ -188,12 +210,14 @@ jobs:
echo "| Last deployed | \`${BASE}\` ($(cell "$SOURCE")) |"
echo "| Deploying | \`${HEAD_SHA}\` |"
echo "| Commits in range | ${COMMITS} |"
echo "| Deployed non-ancestors skipped | ${SKIPPED} |"
echo "| Claims mode | \`enforce\` |"
echo "| Claims paths | $( [ -n "${CLAIMS_PATHS:-}" ] && echo "\`$(cell "$CLAIMS_PATHS")\`" || echo "default scope" ) |"
echo "| Evidence paths | \`$(cell "${CLAIMS_EVIDENCE_PATHS:-}")\` |"
echo "| URL prefixes | $( [ -n "${CLAIMS_URL_PREFIXES:-}" ] && echo "\`$(cell "$CLAIMS_URL_PREFIXES")\`" || echo "none" ) |"
echo ""
} >> "$SUMMARY"
echo "deploy gate: range ${BASE}..${HEAD_SHA}, ${COMMITS} commit(s), base from ${SOURCE}"
{ echo "base=${BASE}"; echo "sha=${HEAD_SHA}"; } >> "$OUT"

- name: Check claims in the deploy range
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added

- Deploy gate base rules (fix forward from the assurance re-check of #23). The base is the newest successful deployment whose commit is an ancestor of, or equal to, the commit being deployed, so a rollback to a commit that was never deployed is checked. `base_sha` is used only when no such deployment exists, must be a strict ancestor, and is otherwise ignored with a warning. The summary and log show the range and the number of deployments skipped. README limits gain the `deployments: write` residual, the `if: always()` warning and the `.jury-tools` path.
- Reusable `deploy-gate.yml` (CER-4034): the claims check, enforce only, on the range from the newest successful deployment of the caller's environment to the commit being deployed; outputs `sha` and `base`. The approval is the environment on the caller's deploy job. The claims step now lives in `scripts/claims_job.sh`, which `agent-jury.yml` and `deploy-gate.yml` both run.
- Agent Jury `claims check` job and `scripts/claims_check.py`: flags uncited guarantees, certifications, absolute claims, metrics and legal liabilities in added public text. The model classifies uncited hits. New input `claims_mode` (`enforce` default, or `report`). The script is checked out at `job.workflow_sha`. A `claims: proven <evidence>` marker cites one claim, and its evidence must resolve: a repo path at the PR head, a commit SHA in the repo, or an https URL on an allowlisted host. New input `claims_url_hosts` (default `github.com`). The job never fetches a URL. Added lines are normalised (HTML entities, NFKC, zero-width and soft-hyphen characters, look-alike letters) before matching. New categories `unreadable` and `obfuscation`. Scope is case-insensitive and adds `.htm`, `.markdown`, `.rst`, `README*`, `pricing/` and `landing/`. The job fails closed with exit code 2 on a malformed model answer, under `bash -e` as on the runner.

Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ The job is a new check, `claims check`. Add it to branch protection only after a

## Deploy gate

`deploy-gate.yml` runs the claims check on everything a deploy ships. The range starts at the newest successful deployment of the caller's environment and ends at the commit being deployed. GitHub records a deployment for every job that uses an environment, so the gate keeps no state of its own. The check always enforces; the gate has no `claims_mode` input.
`deploy-gate.yml` runs the claims check on everything a deploy ships. The range starts at the newest successful deployment of the caller's environment whose commit is an ancestor of, or equal to, the commit being deployed, and ends at that commit. A newer deployment that is not an ancestor, as in a rollback, is skipped, so a commit that was never deployed is checked from the deployed commit before it. GitHub records a deployment for every job that uses an environment, so the gate keeps no state of its own. The check always enforces; the gate has no `claims_mode` input.

The approval is the environment on the caller's deploy job (TASK-0823, option A). The reviewer approves the job that holds the deploy credentials, after the gate has run in the same workflow run. The gate's step summary shows the environment, the last deployed commit, the commit being deployed, the number of commits in the range and the effective claims settings.

Expand Down Expand Up @@ -126,8 +126,8 @@ Set up the environment `<unit>-prod` like `cortex-prod`:
- deployment branches limited to protected branches;
- the deploy credentials stored as environment secrets, not repo secrets.

A first deploy has no deployment to start from. Pass `base_sha` once, as a full 40-character SHA, for example the last commit that is already live. The gate fails closed in each of these cases:
- no successful deployment is found and no `base_sha` is given;
A first deploy has no deployment to start from. Pass `base_sha` once, as a full 40-character SHA, for example the last commit that is already live. It must be a strict ancestor of the commit being deployed. The gate uses it only when no successful deployment is an ancestor; otherwise it ignores it with a warning, so wiring `base_sha` to a dispatch input cannot shorten the range. The gate fails closed in each of these cases:
- no successful deployment at an ancestor is found and no usable `base_sha` is given;
- the deployments API refuses the request, for example when the caller did not grant `deployments: read`;
- the base commit is not in the repository's history;
- the environment name has characters other than letters, digits, `.`, `_` and `-`.
Expand All @@ -138,7 +138,10 @@ Limits:
- A caller can drop `needs: gate` only by landing a workflow change on `main`. The approval still applies, because it belongs to the environment, but the claims check would not run. A reviewer should see the `gate / claims check` job in the run before approving.
- GitHub lets a required reviewer's token approve through the API, and agents on ceres hold that token. The approval stops accidents, not a rogue agent. It moves to a reflex RED card once reflexd is reachable from CI runners (TASK-0823).
- Every successful deployment of the environment moves the base, whichever workflow made it.
- The range is `git diff base...sha`. A rollback to an older commit has an empty range and passes.
- A rollback to a commit that was itself deployed has an empty range and passes.
- Anyone holding a token with `deployments: write` in the repository, including a workflow on a branch that asks for it, can create a deployment with a success status and so move the base. GitHub's documentation does not say that environment protection applies to deployments created through the API. A tamper-evident record of each gate pass replaces deployment statuses when the approval moves to a reflex RED card (TASK-0823).
- Do not give the deploy job `if: always()` or `if: ${{ !cancelled() }}`: with either, it runs after a failed or skipped gate.
- The gate checks this repository out at `.jury-tools` in the workspace. A caller that tracks a path of that name gets a checkout error.

## Pinning and releases

Expand Down
48 changes: 45 additions & 3 deletions tests/test_deploy_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ def tearDownClass(cls):
cls.api.server.shutdown()
cls.tmp.cleanup()

def run_range(self, environment='web-prod', base_sha='', deployments=None, status=200):
def run_range(self, environment='web-prod', base_sha='', deployments=None, status=200, head=None):
self.api.deployments, self.api.status, self.api.seen = deployments or {}, status, []
git(self.root, 'checkout', '-q', '--detach', head or self.shas[-1])
out = Path(self.tmp.name) / 'out'
out.write_text('')
env = {**GIT_ENV, 'API_URL': self.api.url, 'REPO': 'o/r', 'GATE_TOKEN': 'fixture',
Expand All @@ -171,11 +172,52 @@ def test_no_successful_deployment_fails_closed_and_names_base_sha(self):
self.assertIn('base_sha', log)
self.assertNotIn('base', out)

def test_base_sha_input_is_used_without_the_api(self):
rc, out, log = self.run_range(base_sha=self.shas[0], status=500)
def test_base_sha_is_used_only_when_no_deployment_exists(self):
rc, out, log = self.run_range(base_sha=self.shas[0])
self.assertEqual(rc, 0, log)
self.assertEqual(out.get('base'), self.shas[0])

def test_base_sha_never_skips_the_api(self):
rc, out, _ = self.run_range(base_sha=self.shas[0], status=500)
self.assertNotEqual(rc, 0)
self.assertNotIn('base', out)

def test_base_sha_is_ignored_when_a_deployment_exists(self):
# Assurance finding 1: an input must not override the history.
a, b, c = self.shas
rc, out, log = self.run_range(base_sha=c, deployments={'web-prod': [(2, a, 'success')]})
self.assertEqual(rc, 0, log)
self.assertEqual(out.get('base'), a)
self.assertIn('base_sha ignored', log)

def test_base_sha_must_be_a_strict_ancestor(self):
a, b, c = self.shas
for head, base in ((c, c), (b, c)):
with self.subTest(head=head[:7], base=base[:7]):
rc, out, _ = self.run_range(base_sha=base, head=head)
self.assertNotEqual(rc, 0)
self.assertNotIn('base', out)

def test_rollback_starts_at_the_newest_deployed_ancestor(self):
# A re-run of an old commit after a newer deploy: b was never deployed.
a, b, c = self.shas
rc, out, log = self.run_range(head=b, deployments={'web-prod': [(3, c, 'success'), (1, a, 'success')]})
self.assertEqual(rc, 0, log)
self.assertEqual((out.get('base'), out.get('sha')), (a, b))

def test_rollback_to_a_deployed_commit_has_an_empty_range(self):
a, b, c = self.shas
rc, out, log = self.run_range(head=b, deployments={'web-prod': [(3, c, 'success'), (2, b, 'success')]})
self.assertEqual(rc, 0, log)
self.assertEqual(out.get('base'), b)

def test_no_deployed_ancestor_and_no_base_sha_fails_closed(self):
a, b, c = self.shas
rc, out, log = self.run_range(head=b, deployments={'web-prod': [(3, c, 'success')]})
self.assertNotEqual(rc, 0)
self.assertIn('base_sha', log)
self.assertNotIn('base', out)

def test_malformed_base_sha_fails_closed(self):
for bad in ('main', self.shas[0][:12], self.shas[0] + '; true', 'HEAD~1'):
with self.subTest(bad=bad):
Expand Down
Loading