Repository navigation
Fix concurrent GitOps image publication on main #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #!/usr/bin/env bash | ||
| # Publish a scanned image to the current main manifest without rebasing stale work. | ||
| set -euo pipefail | ||
|
|
||
| source_sha=$1 | ||
| registry_namespace=$2 | ||
| [[ "$source_sha" =~ ^[0-9a-f]{40}$ ]] || { echo "Invalid source commit" >&2; exit 2; } | ||
| [[ "$registry_namespace" =~ ^[a-z0-9][a-z0-9_-]*$ ]] || { echo "Invalid registry namespace" >&2; exit 2; } | ||
| cd "$(git rev-parse --show-toplevel)" | ||
| [[ -z "$(git status --porcelain)" ]] || { echo "GitOps publisher needs a clean checkout" >&2; exit 1; } | ||
|
|
||
| refresh_main() { | ||
| git fetch --no-tags origin '+refs/heads/main:refs/remotes/origin/main' >&2 && | ||
| git rev-parse --verify refs/remotes/origin/main | ||
| } | ||
|
|
||
| remote_tip=$(refresh_main) | ||
| if [[ "$remote_tip" != "$source_sha" ]]; then | ||
| echo "Superseded: main is $remote_tip; not deploying $source_sha" | ||
| exit 0 | ||
| fi | ||
|
|
||
| git switch --detach "$source_sha" | ||
| manifest=manifests/deployment.yaml | ||
| pattern='^[[:space:]]+image:[[:space:]]+[^[:space:]]+/governor:[^[:space:]]+[[:space:]]*$' | ||
| image_count=$(awk -v image_pattern="$pattern" '$0 ~ image_pattern {count++} END {print count+0}' "$manifest") | ||
| [[ "$image_count" == 1 ]] || { echo "Expected exactly one Governor image in $manifest; found $image_count" >&2; exit 1; } | ||
| new_image="$registry_namespace/governor:$source_sha" | ||
| sed -E "s|^([[:space:]]+image:[[:space:]]+)[^[:space:]]+/governor:[^[:space:]]+[[:space:]]*$|\\1$new_image|" "$manifest" > "$manifest.tmp" | ||
| mv "$manifest.tmp" "$manifest" | ||
| grep -Eq "$pattern" "$manifest" | ||
| if git diff --quiet -- "$manifest"; then | ||
| echo "Already current: $new_image" | ||
| exit 0 | ||
| fi | ||
|
|
||
| git add -- "$manifest" | ||
| git -c user.name='GitHub Actions Bot' -c user.email='actions@github.com' commit -m "Deploy: Update Governor image to $source_sha" | ||
|
|
||
| # Only a regular fast-forward push is allowed. If someone moved main while this | ||
| # job prepared its commit, never rebase the old manifest onto their newer work. | ||
| if git push origin HEAD:refs/heads/main; then | ||
| echo "Published $new_image" | ||
| exit 0 | ||
| fi | ||
| remote_tip=$(refresh_main) | ||
| if [[ "$remote_tip" != "$source_sha" ]]; then | ||
| echo "Superseded during publication: main is $remote_tip; not overwriting it" | ||
| exit 0 | ||
| fi | ||
| echo "Failed to publish $new_image although main is unchanged" >&2 | ||
| exit 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| root=$(cd "$(dirname "$0")/.." && pwd) | ||
| publisher="$root/scripts/publish-gitops-image.sh" | ||
| work=$(mktemp -d) | ||
| trap 'rm -rf "$work"' EXIT | ||
| export GIT_CONFIG_NOSYSTEM=1 | ||
| export GIT_CONFIG_GLOBAL=/dev/null | ||
| export GIT_AUTHOR_NAME='Governor test' GIT_AUTHOR_EMAIL='test@example.invalid' | ||
| export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME" GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL" | ||
| remote="$work/remote.git" | ||
| alpha="$work/alpha" | ||
| beta="$work/beta" | ||
| git init -q --initial-branch=main --bare "$remote" | ||
| git clone -q "$remote" "$alpha" | ||
| mkdir -p "$alpha/manifests" | ||
| cat > "$alpha/manifests/deployment.yaml" <<'YAML' | ||
| spec: | ||
| template: | ||
| spec: | ||
| containers: | ||
| - name: governor | ||
| image: test-image/governor:prior | ||
| - name: metrics | ||
| image: test-image/metrics:unchanged | ||
| YAML | ||
| git -C "$alpha" add . | ||
| git -C "$alpha" commit -qm 'Initial app source' | ||
| git -C "$alpha" push -q origin main | ||
| source_sha=$(git -C "$alpha" rev-parse HEAD) | ||
| push_candidate() { (cd "$alpha" && bash "$publisher" "$1" test-image); } | ||
| remote_main() { git --git-dir="$remote" rev-parse main; } | ||
| remote_manifest() { git --git-dir="$remote" show main:manifests/deployment.yaml; } | ||
|
|
||
| # Deploy exactly the scanned SHA and preserve the unrelated image. | ||
| push_candidate "$source_sha" | ||
| published=$(remote_main) | ||
| [[ "$published" != "$source_sha" ]] | ||
| [[ "$(git --git-dir="$remote" rev-parse main^)" == "$source_sha" ]] | ||
| grep -q "image: test-image/governor:$source_sha" <<<"$(remote_manifest)" | ||
| grep -q 'image: test-image/metrics:unchanged' <<<"$(remote_manifest)" | ||
| push_candidate "$source_sha" | ||
| [[ "$(remote_main)" == "$published" ]] | ||
|
|
||
| # A run whose source is already superseded cannot roll the manifest back. | ||
| git clone -q "$remote" "$beta" | ||
| git -C "$beta" commit -q --allow-empty -m 'Newer source' | ||
| git -C "$beta" push -q origin main | ||
| newer=$(remote_main) | ||
| push_candidate "$source_sha" | ||
| [[ "$(remote_main)" == "$newer" ]] | ||
| grep -q "image: test-image/governor:$source_sha" <<<"$(remote_manifest)" | ||
|
|
||
| # Simulate main moving between fetch/check and push under the exact same | ||
| # non-fast-forward condition as the real GitOps failure. | ||
| git -C "$alpha" fetch -q origin main | ||
| git -C "$alpha" switch -q --detach "$newer" | ||
| git -C "$beta" commit -q --allow-empty -m 'Concurrent winner' | ||
| winner=$(git -C "$beta" rev-parse HEAD) | ||
| export GOVERNOR_RACE_REPO="$beta" | ||
| cat > "$alpha/.git/hooks/pre-push" <<'HOOK' | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
| git -C "$GOVERNOR_RACE_REPO" push -q origin main | ||
| HOOK | ||
| chmod +x "$alpha/.git/hooks/pre-push" | ||
| push_candidate "$newer" | ||
| [[ "$(remote_main)" == "$winner" ]] | ||
| grep -q "image: test-image/governor:$source_sha" <<<"$(remote_manifest)" | ||
|
|
||
| # If the remote did not move, do not disguise other push failures as races. | ||
| cat > "$alpha/.git/hooks/pre-push" <<'HOOK' | ||
| #!/usr/bin/env bash | ||
| exit 1 | ||
| HOOK | ||
| if push_candidate "$winner"; then | ||
| echo "Unrelated push failure was not propagated" >&2 | ||
| exit 1 | ||
| fi | ||
| [[ "$(remote_main)" == "$winner" ]] | ||
| echo "GitOps publication tests passed" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change introduces a new GitOps publication policy and a disposable integration test, but does not update any README or documentation. Document that superseded source commits now skip deployment successfully, and include
bash scripts/test-gitops-publication.shas the local verification command so maintainers can interpret skipped publications and reproduce the CI check. The repository'sAGENTS.mdexplicitly requires relevant documentation updates in the same change when commands, tests, or boundaries change.AGENTS.md reference: AGENTS.md:L110-L110
Useful? React with 👍 / 👎.