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
29 changes: 29 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: lint

on:
pull_request:

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
cancel-in-progress: true

permissions: {}

jobs:
# The job name must stay `lint`: the Default ruleset requires a check with that exact name.
lint:
name: lint
runs-on: ubuntu-latest
permissions:
actions: read # for trunk-action
checks: write # for trunk-action
contents: read # for checkout + trunk-action
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
# Trunk needs the merge-base to lint only the PR diff.
fetch-depth: 0
persist-credentials: false

- name: Trunk Check
uses: trunk-io/trunk-action@04ba50e7658c81db7356da96657e6e77f220bfa3 # v1.3.1
80 changes: 80 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: test

on:
pull_request:
workflow_dispatch:

permissions:
contents: write

env:
BRANCH: test/gitops-${{ github.run_id }}-${{ github.run_attempt }}
FIXTURE: test/fixtures/values.yaml
Comment thread
whyvez marked this conversation as resolved.
Resolved

jobs:
setup:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

- name: Create scratch branch
run: git push origin "HEAD:refs/heads/${BRANCH}"

update:
needs: setup
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
job: [a, b]
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

- name: GitOps Update
id: gitops
uses: ./
with:
new_value: ${{ github.run_id }}-${{ matrix.job }}
yaml_key_paths: .${{ matrix.job }}.tag
yaml_file_path: ${{ env.FIXTURE }}
commit_message: 'test ${{ matrix.job }} "quoted" $(echo pwned)'
target_branch: ${{ env.BRANCH }}

- name: Check outputs
env:
SHA: ${{ steps.gitops.outputs.sha }}
BASE_SHA: ${{ steps.gitops.outputs.base_sha }}
EXPECTED_MESSAGE: 'ci(gitops): test ${{ matrix.job }} "quoted" $(echo pwned)'
run: |
git fetch --depth=2 origin "$SHA"
test "$(git rev-parse "${SHA}~1")" = "$BASE_SHA"
test "$(git log -1 --format=%s "$SHA")" = "$EXPECTED_MESSAGE"

verify:
needs: update
runs-on: ubuntu-latest
env:
RUN_ID: ${{ github.run_id }}
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

- name: Check both commits landed
run: |
git fetch origin "$BRANCH"
git log --format=%s FETCH_HEAD
for job in a b; do
git log --format=%s FETCH_HEAD | grep -qxF "ci(gitops): test ${job} \"quoted\" \$(echo pwned)"
test "$(git show "FETCH_HEAD:${FIXTURE}" | yq ".${job}.tag")" = "${RUN_ID}-${job}"
done
# The blank line between keys must survive the yq edit.
test "$(git show "FETCH_HEAD:${FIXTURE}" | wc -l)" = "$(wc -l < "$FIXTURE")"

cleanup:
needs: [setup, verify]
if: always() && needs.setup.result == 'success'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

- name: Delete scratch branch
run: git push origin --delete "$BRANCH"
9 changes: 9 additions & 0 deletions .trunk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*out
*logs
*actions
*notifications
*tools
plugins
user_trunk.yaml
user.yaml
tmp
2 changes: 2 additions & 0 deletions .trunk/configs/.markdownlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Prettier friendly markdownlint config (all formatting rules disabled)
extends: markdownlint/style/prettier
7 changes: 7 additions & 0 deletions .trunk/configs/.shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
enable=all
source-path=SCRIPTDIR
disable=SC2154

# If you're having issues with shellcheck following source, disable the errors via:
# disable=SC1090
# disable=SC1091
7 changes: 7 additions & 0 deletions .trunk/configs/.yamllint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rules:
quoted-strings:
required: only-when-needed
extra-allowed: ["{|}"]
key-duplicates: {}
octal-values:
forbid-implicit-octal: true
36 changes: 36 additions & 0 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This file controls the behavior of Trunk: https://docs.trunk.io/cli
# To learn more about the format of this file, see https://docs.trunk.io/reference/trunk-yaml
version: 0.1
cli:
version: 1.29.0
# Trunk provides extensibility via plugins. (https://docs.trunk.io/plugins)
plugins:
sources:
- id: trunk
ref: v1.11.0
uri: https://github.com/trunk-io/plugins
# Many linters and tools depend on runtimes - configure them here. (https://docs.trunk.io/runtimes)
runtimes:
enabled:
- [email protected]
- [email protected]
# This is the section where you manage your linters. (https://docs.trunk.io/check/configuration)
lint:
enabled:
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- git-diff-check
- [email protected]
- [email protected]
- [email protected]
- [email protected]
actions:
disabled:
- trunk-announce
- trunk-check-pre-push
- trunk-fmt-pre-commit
enabled:
- trunk-upgrade-available
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,57 @@
# github-action-gitops-update
Reusable GitHub Action to update a YAML file and commit the result

Reusable GitHub Action to update a YAML file and commit the result.

It sets one or more YAML keys to a new value, commits the change as `ci(gitops): <commit_message>`, and pushes it to `target_branch`. If another run pushed to the branch first, it fetches the latest branch, re-applies the edit and tries again.

## Usage

```yaml
permissions:
contents: write

jobs:
gitops-update:
runs-on: ubuntu-latest
outputs:
sha: ${{ steps.gitops.outputs.sha }}
steps:
- name: GitOps Update and Commit
id: gitops
uses: masterpointio/github-action-gitops-update@<commit-sha> # vX.Y.Z
with:
new_value: ${{ inputs.image_tag }}
yaml_key_paths: .components.terraform.my-app.vars.image_tag
yaml_file_path: stacks/org/dev/us-east-1.yaml
commit_message: "update my-app image_tag to ${{ inputs.image_tag }}"
```

## Inputs

| Name | Required | Default | Description |
| ----------------- | -------- | ----------------------------------------------- | ---------------------------------------------------------------- |
| `new_value` | yes | | New value for the given YAML keys |
| `yaml_key_paths` | yes | | Comma-separated list of yq paths to update, e.g. `.a.tag,.b.tag` |
| `yaml_file_path` | yes | | Path to the YAML file to update |
| `commit_message` | yes | | Commit message, prefixed with `ci(gitops):` |
| `commit_username` | no | `github-actions` | Commit author name |
| `commit_email` | no | `[email protected]` | Commit author email |
| `target_branch` | no | `${{ github.event.repository.default_branch }}` | Branch to push to |
| `github_token` | no | `${{ github.token }}` | Token used to push (needed when the target branch is protected) |
| `max_attempts` | no | `5` | Number of times to retry the push when the target branch moved |

## Outputs

| Name | Description |
| ---------- | --------------------------------------------------------------------- |
| `sha` | SHA of the pushed commit |
| `base_sha` | SHA of the pushed commit's parent (`target_branch` before the update) |

## Concurrency

Runs that target the same branch at the same time are safe: each push that is rejected is retried on top of the latest branch, with a short random delay. Don't put callers in a shared `concurrency` group to serialize them, because GitHub cancels older pending runs in a group and those updates are lost.

## Notes

- yq removes blank lines when it writes a file, so the action applies yq's output through `diff -B | patch` to keep them. A blank line directly next to an edited key can still be removed.
- If the value is unchanged, the action still pushes an empty commit.
67 changes: 49 additions & 18 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ inputs:
description: The GitHub token used to commit and push the gitops changes (unnecessary if you don't have branch protections enabled on your target branch)
default: ${{ github.token }}
required: false
max_attempts:
description: Number of times to retry the push when the target branch moved
default: "5"
required: false

outputs:
sha:
description: SHA of the pushed commit
value: ${{ steps.commit.outputs.sha }}
base_sha:
description: SHA of the pushed commit's parent (target_branch before this update)
value: ${{ steps.commit.outputs.base_sha }}

runs:
using: composite
Expand All @@ -42,25 +54,44 @@ runs:

- name: 🏗 Set up yq
uses: frenck/action-setup-yq@c4b5be8b4a215c536a41d436757d9feb92836d4f # v1.0.2
with:
# A fixed version skips the unauthenticated GitHub API lookup for "latest", which runners often hit the rate limit on.
version: v4.54.1

- name: Update YAML file + Commit + Push
- name: Update, Commit and Push
id: commit
shell: bash
env:
NEW_VALUE: ${{ inputs.new_value }}
YAML_KEY_PATHS: ${{ inputs.yaml_key_paths }}
YAML_FILE_PATH: ${{ inputs.yaml_file_path }}
COMMIT_MESSAGE: ${{ inputs.commit_message }}
COMMIT_USERNAME: ${{ inputs.commit_username }}
COMMIT_EMAIL: ${{ inputs.commit_email }}
TARGET_BRANCH: ${{ inputs.target_branch }}
MAX_ATTEMPTS: ${{ inputs.max_attempts }}
run: |
IFS=',' read -r -a key_paths <<< "${{ inputs.yaml_key_paths }}"
for key_path in "${key_paths[@]}"
do
echo "Updating path: $key_path"
# Have to include a goofy diff / patch workaround to deal yq removing all whitespace newlines
# See https://github.com/mikefarah/yq/issues/515#issuecomment-1113957629
# Ugh, I need to add the "File updated successfully" to get the patch to work...
yq "($key_path) = \"${{ inputs.new_value }}\"" "${{ inputs.yaml_file_path }}" | diff -B "${{ inputs.yaml_file_path }}" - | patch --silent "${{ inputs.yaml_file_path }}" - && echo "File updated successfully at $key_path"
done

echo "Git config setup"
git config user.name ${{ inputs.commit_username }}
git config user.email ${{ inputs.commit_email }}
git config user.name "$COMMIT_USERNAME"
git config user.email "$COMMIT_EMAIL"
IFS=',' read -r -a key_paths <<< "$YAML_KEY_PATHS"

echo "Committing and pushing changes"
git add ${{ inputs.yaml_file_path }}
git commit --allow-empty -m "ci(gitops): ${{ inputs.commit_message }}"
git push -u origin ${{ inputs.target_branch }}
# Re-apply the edit on a fresh target branch each attempt, so a concurrent push never needs a merge.
for ((attempt = 1; attempt <= MAX_ATTEMPTS; attempt++)); do
git fetch --depth=1 origin "$TARGET_BRANCH"
git reset --hard FETCH_HEAD
for key_path in "${key_paths[@]}"; do
# diff/patch keeps the blank lines that yq strips: https://github.com/mikefarah/yq/issues/515
yq "(${key_path}) = strenv(NEW_VALUE)" "$YAML_FILE_PATH" | diff -B "$YAML_FILE_PATH" - | patch --silent "$YAML_FILE_PATH" - && echo "Updated $key_path"
done
git add "$YAML_FILE_PATH"
git commit --allow-empty -m "ci(gitops): ${COMMIT_MESSAGE}"
if git push origin "HEAD:${TARGET_BRANCH}"; then
echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
echo "base_sha=$(git rev-parse HEAD~1)" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "Push rejected (attempt ${attempt}/${MAX_ATTEMPTS}); retrying on the latest ${TARGET_BRANCH}"
sleep $((RANDOM % 10 + 5))
done
echo "::error::Push to ${TARGET_BRANCH} rejected ${MAX_ATTEMPTS} times"
exit 1
7 changes: 7 additions & 0 deletions test/fixtures/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
a:
tag: initial
name: a

b:
name: b
tag: initial