diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 0000000..40badbb --- /dev/null +++ b/.github/workflows/lint.yaml @@ -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 diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..08c1115 --- /dev/null +++ b/.github/workflows/test.yaml @@ -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 + +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" diff --git a/.trunk/.gitignore b/.trunk/.gitignore new file mode 100644 index 0000000..15966d0 --- /dev/null +++ b/.trunk/.gitignore @@ -0,0 +1,9 @@ +*out +*logs +*actions +*notifications +*tools +plugins +user_trunk.yaml +user.yaml +tmp diff --git a/.trunk/configs/.markdownlint.yaml b/.trunk/configs/.markdownlint.yaml new file mode 100644 index 0000000..b40ee9d --- /dev/null +++ b/.trunk/configs/.markdownlint.yaml @@ -0,0 +1,2 @@ +# Prettier friendly markdownlint config (all formatting rules disabled) +extends: markdownlint/style/prettier diff --git a/.trunk/configs/.shellcheckrc b/.trunk/configs/.shellcheckrc new file mode 100644 index 0000000..8c7b1ad --- /dev/null +++ b/.trunk/configs/.shellcheckrc @@ -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 diff --git a/.trunk/configs/.yamllint.yaml b/.trunk/configs/.yamllint.yaml new file mode 100644 index 0000000..184e251 --- /dev/null +++ b/.trunk/configs/.yamllint.yaml @@ -0,0 +1,7 @@ +rules: + quoted-strings: + required: only-when-needed + extra-allowed: ["{|}"] + key-duplicates: {} + octal-values: + forbid-implicit-octal: true diff --git a/.trunk/trunk.yaml b/.trunk/trunk.yaml new file mode 100644 index 0000000..16920c9 --- /dev/null +++ b/.trunk/trunk.yaml @@ -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: + - node@22.16.0 + - python@3.14.4 +# This is the section where you manage your linters. (https://docs.trunk.io/check/configuration) +lint: + enabled: + - actionlint@1.7.12 + - gitleaks@8.30.1 + - shellcheck@0.11.0 + - shfmt@3.14.1 + - checkov@3.4.5 + - git-diff-check + - markdownlint@0.50.0 + - prettier@3.10.1 + - trufflehog@3.99.9 + - yamllint@1.38.0 +actions: + disabled: + - trunk-announce + - trunk-check-pre-push + - trunk-fmt-pre-commit + enabled: + - trunk-upgrade-available diff --git a/README.md b/README.md index 522f2af..f740586 100644 --- a/README.md +++ b/README.md @@ -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): `, 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@ # 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 | `github-actions@github.com` | 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. diff --git a/action.yaml b/action.yaml index 444c8c3..ecaec46 100644 --- a/action.yaml +++ b/action.yaml @@ -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 @@ -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 diff --git a/test/fixtures/values.yaml b/test/fixtures/values.yaml new file mode 100644 index 0000000..afd3e9b --- /dev/null +++ b/test/fixtures/values.yaml @@ -0,0 +1,7 @@ +a: + tag: initial + name: a + +b: + name: b + tag: initial