Skip to content

Instantly share code, notes, and snippets.

@zewelor
Last active December 1, 2023 12:57
Show Gist options
  • Select an option

  • Save zewelor/0749ad8653d111b8b719901fdcd7d9cc to your computer and use it in GitHub Desktop.

Select an option

Save zewelor/0749ad8653d111b8b719901fdcd7d9cc to your computer and use it in GitHub Desktop.
Github workflow to render helm diff between pull request branch and main. Adjust path with *.yaml if doesn't work. Also take a look at awk, and adjust depth there, to base dir for helm app. It will add comment with diff. I'm using this with argocd
name: Helm Chart Comparison
on:
pull_request:
paths:
- 'gitops/charts/**/*.yaml'
jobs:
find-changes:
runs-on: ubuntu-latest
outputs:
modified_charts: ${{ steps.detect-modified-charts.outputs.modified_charts }}
steps:
# Checkout the PR code
- name: Checkout PR
uses: actions/checkout@v4
with:
path: pr
fetch-depth: 0
# Install Helm
- name: Install Helm
uses: azure/setup-helm@v3
# Detect Modified Helm Charts
- name: Detect Modified Charts
id: detect-modified-charts
run: |
#!/bin/bash
cd "${GITHUB_WORKSPACE}/pr/"
modified_chart_files=$(git diff --name-only origin/${{ github.base_ref }} ${{ github.sha }} -- 'gitops/charts/*.yaml')
echo "Modified yaml files:"
echo $modified_chart_files
modified_charts=$(echo "$modified_chart_files" | awk -F'/' '{print $1"/"$2"/"$3"/"$4;}' | sort -u | jq -nRMc '[inputs | select(length>0)]')
echo "Modified charts:"
echo $modified_charts
echo "modified_charts=$modified_charts" >> $GITHUB_OUTPUT
working-directory: ${{ github.workspace }}/pr
- uses: izhangzhihao/delete-comment@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
delete_user_name: github-actions[bot]
issue_number: ${{ github.event.pull_request.number }}
helm-diff:
runs-on: ubuntu-latest
needs:
- find-changes
strategy:
matrix:
chart: ${{ fromJson(needs.find-changes.outputs.modified_charts) }}
fail-fast: false
steps:
# Checkout the PR code
- name: Checkout PR
uses: actions/checkout@v4
with:
path: pr
# Checkout the main branch code
- name: Checkout main branch
uses: actions/checkout@v4
with:
ref: 'main'
path: main
# Render and Compare Helm Charts
- name: Render and Compare Helm Charts
id: render-diff
run: |
chart_diff=""
chart="${{ matrix.chart }}"
pr_chart_path="${GITHUB_WORKSPACE}/pr/$chart"
main_chart_path="${GITHUB_WORKSPACE}/main/$chart"
chart_name="$(helm show chart $pr_chart_path | grep -E '^name:' | awk '{print $2}')"
echo "Processing chart: $chart - $chart_name"
helm dependency update $pr_chart_path
helm template $chart_name $pr_chart_path > pr_rendered.yaml
helm dependency update $main_chart_path
helm template $chart_name $main_chart_path > main_rendered.yaml
raw_diff_output=$(diff --unified main_rendered.yaml pr_rendered.yaml|| true)
# Filter out the unwanted fields
diff_output=$(echo "$raw_diff_output" | grep -Ev "^(--- |\+\+\+ )")
diff_output=$(echo "$diff_output" | awk '
/^@@/ {
if (print_section && !skip_section) {
print section
}
section="" # Reset section
print_section=0 # Reset print_section
skip_section=0 # Reset skip_section
skip_line=0 # Reset skip_line
}
/^[-+]/ {
if (/app\.kubernetes\.io\/version: |helm\.sh\/chart: /) {
skip_line++
} else {
print_section=1
}
}
{
section = section $0 "\n" # Append current line to section
}
/^@@/,/^$/ {
if (/^@@/ && skip_line == 2) { # If only version/chart lines are found
skip_section=1
}
}
END {
if (print_section && !skip_section) {
print section
}
}
' )
if [ -n "$diff_output" ]; then
echo "Differences detected in chart: $chart"
echo "::group::Chart Diff - $chart"
echo "$diff_output"
echo "::endgroup::"
chart_diff="$diff_output"
echo "diff_exists=true" >> $GITHUB_OUTPUT
else
echo "No differences in chart: $chart"
echo "diff_exists=false" >> $GITHUB_OUTPUT
fi
echo "chart_diff=$(echo "$chart_diff" | jq -Rs .)" >> $GITHUB_OUTPUT
- name: Post Comment with Diff
if: steps.render-diff.outputs.diff_exists == 'true'
uses: peter-evans/create-or-update-comment@v3
with:
issue-number: ${{ github.event.pull_request.number }}
body: |
# ${{ matrix.chart }}
```diff
${{ fromJSON(steps.render-diff.outputs.chart_diff) }}
```
- name: Post Comment - Diff Empty
if: steps.render-diff.outputs.diff_exists == 'false'
uses: peter-evans/create-or-update-comment@v3
with:
issue-number: ${{ github.event.pull_request.number }}
body: |
# ${{ matrix.chart }}
:white_check_mark: No differences found.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment