/
action.yaml
66 lines (60 loc) · 2.66 KB
/
action.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
name: gitops-update
description: Reusable GitHub Action to update a YAML file and commit the result
author: hello@masterpoint.io
inputs:
new_value:
description: New value that should replace the existing value at the given YAML path
required: true
yaml_key_paths:
description: Comma-separated list of paths to the YAML keys that should be updated within the given YAML file
required: true
yaml_file_path:
description: Path to the YAML file that should be updated
required: true
commit_message:
description: Commit message that should be used when committing the changes
required: true
commit_username:
description: The username of the user that will execute the commit.
default: github-actions
required: false
commit_email:
description: The email of the user that will execute the commit.
default: github-actions@github.com
required: false
target_branch:
description: The branch that should be pushed to
default: ${{ github.event.repository.default_branch }}
required: false
github_token:
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
runs:
using: composite
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.3
with:
token: ${{ inputs.github_token }}
- name: 🏗 Set up yq
uses: frenck/action-setup-yq@c4b5be8b4a215c536a41d436757d9feb92836d4f # v1.0.2
- name: Update YAML file + Commit + Push
shell: bash
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 }}
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 }}