Last active
May 6, 2024 22:31
-
-
Save cubic3d/17b2c601549802501d4ca5a68dcc4db8 to your computer and use it in GitHub Desktop.
GitHub Actions workflow to create a resource diff on HelmRelease PRs
This file contains 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
name: Create diff on updated HelmReleases | |
on: | |
pull_request: | |
branches: | |
- master | |
paths: | |
- "clusters/**.yaml" | |
env: | |
conf_live_branch: master | |
conf_allow_repeating_same_comment: false | |
conf_ignore_known_labels_containing_versions: true | |
jobs: | |
changes: | |
name: Detect changes | |
runs-on: ubuntu-latest | |
outputs: | |
files: "${{ steps.extract.outputs.files }}" | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v2 | |
- name: Get changes | |
uses: dorny/paths-filter@v2 | |
id: filter | |
with: | |
list-files: shell | |
filters: | | |
changed: | |
- '**' | |
- name: Keep HelmReleases only | |
id: extract | |
run: | | |
filtered=$(grep -zl "kind: HelmRelease.*registryUrl=" ${{ steps.filter.outputs.changed_files }} \ | |
| jq -nR '[inputs | select(length>0)]') | |
echo ::set-output name=files::${filtered} | |
helm: | |
name: Template HelmReleases | |
runs-on: ubuntu-latest | |
if: ${{ needs.changes.outputs.files != '[]' }} | |
needs: | |
- changes | |
strategy: | |
matrix: | |
file: ${{ fromJson(needs.changes.outputs.files) }} | |
fail-fast: false | |
steps: | |
- name: Setup Kubernetes Tools | |
uses: yokawasa/[email protected] | |
with: | |
setup-tools: | | |
helm | |
yq | |
- name: Checkout live branch | |
uses: actions/checkout@v2 | |
with: | |
ref: ${{ env.conf_live_branch }} | |
path: live | |
- name: Checkout PR branch | |
uses: actions/checkout@v2 | |
with: | |
path: pr | |
- name: Create diff | |
id: diff | |
run: | | |
hr_live_url=$(sed -nr 's|.*registryUrl=(.+)$|\1|p' live/${{ matrix.file }}) | |
hr_live_chart=$(yq e .spec.chart.spec.chart live/${{ matrix.file }}) | |
hr_live_version=$(yq e .spec.chart.spec.version live/${{ matrix.file }}) | |
hr_live_values=$(yq e .spec.values live/${{ matrix.file }}) | |
hr_pr_url=$(sed -nr 's|.*registryUrl=(.+)$|\1|p' pr/${{ matrix.file }}) | |
hr_pr_chart=$(yq e .spec.chart.spec.chart pr/${{ matrix.file }}) | |
hr_pr_version=$(yq e .spec.chart.spec.version pr/${{ matrix.file }}) | |
hr_pr_values=$(yq e .spec.values pr/${{ matrix.file }}) | |
helm repo add live "$hr_live_url" | |
helm repo add pr "$hr_pr_url" | |
resources_live=$(echo "$hr_live_values" | \ | |
helm template "$hr_live_chart" \ | |
live/"$hr_live_chart" \ | |
--version "$hr_live_version" -f - || true) | |
echo "$resources_live" | |
echo "#####################################################" | |
resources_pr=$(echo "$hr_pr_values" | \ | |
helm template "$hr_pr_chart" \ | |
pr/"$hr_pr_chart" \ | |
--version "$hr_pr_version" -f -) | |
echo "$resources_pr" | |
echo "#####################################################" | |
if [ "$conf_ignore_known_labels_containing_versions" = "true" ]; then | |
labels='.metadata.labels."helm.sh/chart"' | |
labels+=',.metadata.labels.chart' | |
labels+=',.metadata.labels."app.kubernetes.io/version"' | |
labels+=',.spec.template.metadata.labels."helm.sh/chart"' | |
labels+=',.spec.template.metadata.labels.chart' | |
labels+=',.spec.template.metadata.labels."app.kubernetes.io/version"' | |
resources_live=$(echo "$resources_live" | yq e "del($labels)" -) | |
resources_pr=$(echo "$resources_pr" | yq e "del($labels)" -) | |
fi | |
diff=$((diff -u <(echo "$resources_live") <(echo "$resources_pr") || true) | tail +3) | |
echo "$diff" | |
message="Path: \`${{ matrix.file }}\`" | |
if [ "$hr_live_chart" != "$hr_pr_chart" ]; then | |
message="$message"$'\n'"Chart: \`$hr_live_chart\` -> \`$hr_pr_chart\`" | |
fi | |
if [ "$hr_live_version" != "$hr_pr_version" ]; then | |
message="$message"$'\n'"Version: \`$hr_live_version\` -> \`$hr_pr_version\`" | |
fi | |
if [ "$hr_live_url" != "$hr_pr_url" ]; then | |
message="$message"$'\n'"Repo: \`$hr_live_url\` -> \`$hr_pr_url\`" | |
fi | |
message="$message"$'\n'$'\n' | |
if [ -z "$diff" ]; then | |
message="$message"'```'$'\n'"No changes detected in resources"$'\n''```' | |
else | |
message="$message"'```diff'$'\n'"$diff"$'\n''```' | |
fi | |
echo "::set-output name=message::$(echo "$message" | jq --raw-input --slurp)" | |
- name: Add PR Comment | |
uses: mshick/add-pr-comment@v1 | |
with: | |
repo-token: ${{ secrets.GITHUB_TOKEN }} | |
allow-repeats: ${{ env.conf_allow_repeating_same_comment }} | |
message: "${{ fromJSON(steps.diff.outputs.message) }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Revision 7
Revision 6
Revision 5
Revision 4
Revision 3
Revision 2