Skip to content

feat: write-env-state records a deploy in argocd-state - #79

Merged
hamishfagg merged 8 commits into
mainfrom
feat/write-env-state
Nov 13, 2026
Merged

hamishfagg merged 8 commits into
mainfrom
feat/write-env-state

Conversation

@hamishfagg

@hamishfagg hamishfagg commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

The deploy half of the Argo CD move (KFS 208, argocd-state 1): a composite action that sets tag and revision in argocd-state <env>/<service>.yaml and pushes. Argo CD polls that repository and syncs the service, so the calling job needs no cluster credential.

  • scripts/write_env_state.py edits the two lines in place, so the keys set when the service was added keep their order and comments. Values are checked against the same shapes argocd-state's validator accepts. A missing file is refused, not created: adding a service is a reviewed change there.
  • Each attempt fetches and resets to the remote branch before editing, so two deploys landing at once both get in; a push rejected because the branch moved is retried, up to 5 times.
  • The action checks out argocd-state with the mindsdb-release-train installation token (minted by the caller with actions/create-github-app-token), which that repo's ruleset lets push to main.

Inputs are sha, the commit Argo CD renders the chart from, and tag, the image build-push-ecr pushed for it. On a pull_request run callers pass the PR head sha and its <build environment>-head-<sha> tag, not $GITHUB_SHA: that is the synthetic merge commit, which GitHub drops once the PR closes.

When it pushed a commit, the action sets commit status argocd/<env> to pending on sha in the calling repository, linking to the Argo CD app. Argo CD's notifier (KFS 208) only ever sets that status to success or failure, so without this a sync that never started left the commit with no status at all; now it is a pending that never clears. A rerun that finds the file already up to date sets nothing, since nothing will sync. The calling job needs statuses: write; status-token defaults to github.token, argocd-url to newdev's.

build-push-ecr gains remote-builder: "false" skips the in-cluster BuildKit agent, which only a runner inside newdev can reach, and builds on the builder setup-buildx-action made on the runner. The default keeps every existing caller as it is.

Tests in tests/test_write_env_state.py run the script against a local bare repository: the edit, the no-op, the missing file, a stale clone, a push rejected mid-attempt, a push a pre-receive hook refuses, and giving up. actionlint and zizmor report nothing on the action.

🤖 Generated with Claude Code

Sets tag and revision in <env>/<service>.yaml and pushes with the
release-train App's token, retrying when another deploy lands first.
The file has to exist already.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@hamishfagg
hamishfagg requested a review from a team as a code owner November 12, 2026 20:04
hamishfagg and others added 4 commits November 13, 2026 09:17
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Argo CD's notifier only ever sets that status to success or failure, so
a sync that never starts left the commit with no status at all. Set
pending first and a stuck sync shows as a pending that never clears;
a failed push sets it to error with the run as its link.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
lucas-koontz
lucas-koontz previously approved these changes Nov 13, 2026

@lucas-koontz lucas-koontz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed write-env-state, its script and tests, the new pending status, and the build-push-ecr change. The state edit, the input checks, and the retry loop hold up, including on the shallow checkout and after a force-push on the remote.

Three fixes are worth making, and none blocks: the gha cache flags never take effect from a run: step, a push the server refuses gets retried as if it were a race, and the pending status sticks when the state file already matches. The rest is optional or polish.

Comment thread build-push-ecr/action.yml Outdated
if [ "$REMOTE_BUILDER" = "false" ]; then
# setup-buildx-action gave this runner a builder of its own; the
# GitHub Actions cache stands in for the agent's layer cache.
CACHE_ARGS="--cache-from type=gha --cache-to type=gha,mode=max"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: These flags do nothing. buildx reads the cache's token and URL from ACTIONS_RUNTIME_TOKEN and ACTIONS_RESULTS_URL, a run: step never gets them, and buildx then drops type=gha without a warning. A hosted build on this branch logged no cache import or export. The gain would be small, so I'd drop CACHE_ARGS and the cache wording on line 32. To keep it, add crazy-max/ghaction-github-runtime and a per-image scope=.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped in 260d84e: CACHE_ARGS and the cache wording are gone, and remote-builder: "false" now only skips the remote builder. The input is read from env:.

Comment thread scripts/write_env_state.py Outdated
Comment on lines +94 to +95
if "rejected" not in push.stderr and "fetch first" not in push.stderr and "non-fast-forward" not in push.stderr:
raise StateError(f"push to {branch} failed: {push.stderr.strip()}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: "rejected" also matches ! [remote rejected] HEAD -> main (push declined due to repository rule violations). A ruleset or hook refusal then gets pushed five times and reported as "main kept moving", and git's reason is lost. These markers match only a race, client side or server side. A test with a failing hooks/pre-receive in the bare fixture would pin it:

Suggested change
if "rejected" not in push.stderr and "fetch first" not in push.stderr and "non-fast-forward" not in push.stderr:
raise StateError(f"push to {branch} failed: {push.stderr.strip()}")
if not any(m in push.stderr for m in ("(fetch first)", "(non-fast-forward)", "(incorrect old value provided)", "cannot lock ref")):
raise StateError(f"push to {branch} failed: {push.stderr.strip()}")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken in 936ad10, with the pre-receive test: a refused push raises with git's own text after one attempt; only the four race markers are retried.

Comment thread scripts/write_env_state.py Outdated
Comment on lines +69 to +70
def git(repo_dir: Path, *args: str, check: bool = True) -> subprocess.CompletedProcess:
return subprocess.run(["git", "-C", str(repo_dir), *args], capture_output=True, text=True, check=check)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional: A failed fetch, reset, add, or commit raises CalledProcessError, and capture_output swallows git's message, so the log shows a traceback with only the exit status. Raising StateError gives the ::error:: line with the reason:

Suggested change
def git(repo_dir: Path, *args: str, check: bool = True) -> subprocess.CompletedProcess:
return subprocess.run(["git", "-C", str(repo_dir), *args], capture_output=True, text=True, check=check)
def git(repo_dir: Path, *args: str, check: bool = True) -> subprocess.CompletedProcess:
result = subprocess.run(["git", "-C", str(repo_dir), *args], capture_output=True, text=True)
if check and result.returncode != 0:
raise StateError(f"git {args[0]} failed: {result.stderr.strip()}")
return result

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken in 936ad10.

Comment thread write-env-state/action.yml Outdated
path: argocd-state
persist-credentials: true

- name: Mark the commit as waiting for Argo CD

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: When the state file already records this sha, the script returns without a commit, so Argo CD never syncs and nothing replaces this pending. A rerun, or any new label on an already-deployed PR when the caller reruns on labeled, does exactly that, and the commit's earlier success turns into a pending that never clears. Setting pending only after the script reports a commit avoids it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved in 8dadb04: the script writes pushed=true|false to GITHUB_OUTPUT and the pending step runs after it, only on true. The error step went with the reordering; a failed push has the red job on the commit.

Comment thread write-env-state/action.yml Outdated
runs:
using: composite
steps:
- uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5.0.1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Three small ones. The pin comment says v5.0.1, but fbc6f39 is v5.1.0. build-push-ecr line 63 puts ${{ inputs.remote-builder }} straight into the script, which zizmor flags; an env: entry avoids it. updated_text drops a trailing # comment on the tag: and revision: lines, where the state repository's README template puts them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All three: the pin comment in 8dadb04, the env: read in 260d84e, and updated_text keeping a trailing comment in 936ad10, with a test.

hamishfagg and others added 3 commits November 13, 2026 16:47
buildx takes the Actions cache token and URL from variables a run: step
never has, so --cache-to type=gha was dropped without a word: the hosted
build on this branch logged no cache import or export. remote-builder
"false" now only skips the remote builder; "true", the default, is the
same command as before.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
"rejected" also matched a push a ruleset or hook refused, so that was
tried five times and reported as the branch moving. Only git's race
messages are retried now; a refusal raises with git's own text after one
attempt, and so does any other failed git command. A comment after the
tag or revision value stays. record() says whether it pushed, and main
writes pushed=true|false to --github-output for the action.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
A rerun whose file already matched set the pending before the script
returned without a commit, and nothing ever cleared it. The pending step
now follows the script and runs on its pushed output; the error step
went with the reordering. Checkout pin comment corrected to v5.1.0.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@hamishfagg
hamishfagg merged commit 1c883f9 into main Nov 13, 2026
6 checks passed
@hamishfagg
hamishfagg deleted the feat/write-env-state branch November 13, 2026 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants