Skip to content

Commit c7df507

Browse files
authored
feat(helm): Use conftest to validate "safe to automerge" diffs (#143)
## Description This PR uses conftest to validate "safe to automerge" diffs <!-- Please do not leave this blank This PR [adds/removes/fixes/replaces] the [feature/bug/etc]. --> ## Related Tickets & Documents * MZCLD-2112 * mozilla/helm-charts#263 <!-- Please reference related Jira tickets, GitHub issues or Bugzilla. This repo has been configured to automatically insert hyperlinks for SVCSE and MZCLD, OPST, and other tickets. See https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/managing-repository-settings/configuring-autolinks-to-reference-external-resources -->
1 parent 626b4f6 commit c7df507

1 file changed

Lines changed: 124 additions & 3 deletions

File tree

.github/workflows/diff-rendered-charts.yml

Lines changed: 124 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
name: render and diff helm charts
99
on:
1010
workflow_call:
11+
inputs:
12+
automerge_test:
13+
description: Run confest against helm diff output to evaluate if PR is safe to automerge
14+
default: false
15+
required: false
16+
type: boolean
1117

1218
jobs:
1319
get_changed_helm_charts:
@@ -102,9 +108,6 @@ jobs:
102108
- get_changed_helm_charts
103109
- render_charts
104110
steps:
105-
- name: setup helm
106-
uses: azure/setup-helm@1a275c3b69536ee54be43f2070a358922e12c8d4 #v4.3.1
107-
108111
- name: download artifacts
109112
uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 #v8.0.0
110113
with:
@@ -123,6 +126,7 @@ jobs:
123126
done
124127
env:
125128
CHARTS: ${{ needs.get_changed_helm_charts.outputs.charts }}
129+
126130
- name: post diff as comment on pull request
127131
if: needs.get_changed_helm_charts.outputs.charts != ''
128132
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8
@@ -178,3 +182,120 @@ jobs:
178182
body: comment
179183
})
180184
}
185+
186+
evaulate_helm_chart_automerge:
187+
if: inputs.automerge_test
188+
runs-on: ubuntu-latest
189+
needs:
190+
- get_changed_helm_charts
191+
- render_charts
192+
steps:
193+
- name: set commit status
194+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 #v9.0.0
195+
with:
196+
script: |
197+
github.rest.repos.createCommitStatus({
198+
owner: context.repo.owner,
199+
repo: context.repo.repo,
200+
sha: context.payload.pull_request.head.sha,
201+
state: 'pending',
202+
context: 'conftest test',
203+
description: '',
204+
target_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`
205+
})
206+
207+
- name: download artifacts
208+
uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 #v8.0.0
209+
with:
210+
pattern: shared-*
211+
merge-multiple: true
212+
path: "shared"
213+
214+
# TODO Move this step into a separate action with tool cache support
215+
- name: install conftest
216+
env:
217+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
218+
run: |
219+
set -euo pipefail
220+
221+
gh release download v0.68.2 -R open-policy-agent/conftest -p "*_Linux_x86_64.tar.gz" -O - | tar xzf -
222+
mv conftest /usr/local/bin
223+
224+
# TODO Move this step into a separate action with tool cache support
225+
- name: install diffnest
226+
env:
227+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
228+
run: |
229+
set -euo pipefail
230+
231+
gh release download v1.7.0 -R sters/diffnest -p "*_linux-amd64.tar.gz" -O - | tar xzf -
232+
mv diffnest /usr/local/bin
233+
234+
- name: structured diff
235+
env:
236+
CHARTS: ${{ needs.get_changed_helm_charts.outputs.charts }}
237+
run: |
238+
set -euo pipefail
239+
240+
shopt -s nullglob
241+
242+
for chart in ${CHARTS}; do
243+
CONFIGS=`find "shared/base-charts/${chart}" "shared/head-charts/${chart}" -mindepth 1 -maxdepth 1 -type d -print0 | xargs --null basename -a | sort | uniq`
244+
245+
mkdir -p diff/${chart}
246+
247+
for config in ${CONFIGS}; do
248+
for dir in "shared/base-charts/${chart}/${config}" "shared/head-charts/${chart}/${config}"; do
249+
OUTPUT_FILE="${dir}.yaml"
250+
251+
touch "$OUTPUT_FILE"
252+
253+
# Recursively find and concatenate all files
254+
find "$dir" -type f | sort | while read -r file; do
255+
cat "$file" >> "$OUTPUT_FILE"
256+
echo "" >> "$OUTPUT_FILE" # ensure newline between files
257+
done
258+
done
259+
260+
diffnest --format json-patch "shared/base-charts/${chart}/${config}.yaml" "shared/head-charts/${chart}/${config}.yaml" > "diff/${chart}/${config}.json" || true
261+
done
262+
done
263+
264+
- name: conftest test
265+
id: conftest
266+
env:
267+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
268+
run: |
269+
set -uo pipefail
270+
271+
CONFTEST_OUTPUT=$(conftest test --no-color --no-fail --strict --update https://raw.githubusercontent.com/mozilla/helm-charts/main/policy/helm-automerge.rego diff)
272+
CONFTEST_EXIT_CODE=$?
273+
STATUS_DESCRIPTION=$(echo "$CONFTEST_OUTPUT" | tail -1)
274+
275+
echo "STATUS_DESCRIPTION=${STATUS_DESCRIPTION}" >> "$GITHUB_OUTPUT"
276+
echo "${STATUS_DESCRIPTION}" >> $GITHUB_STEP_SUMMARY
277+
if [ ${CONFTEST_EXIT_CODE} -eq 0 ]; then
278+
echo "STATUS_STATE=success" >> "$GITHUB_OUTPUT"
279+
else
280+
echo "STATUS_STATE=failure" >> "$GITHUB_OUTPUT"
281+
fi
282+
283+
- name: set commit status
284+
env:
285+
STATUS_DESCRIPTION: ${{ steps.conftest.outputs.STATUS_DESCRIPTION }}
286+
STATUS_STATE: ${{ steps.conftest.outputs.STATUS_STATE }}
287+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 #v9.0.0
288+
with:
289+
script: |
290+
const description = process.env.STATUS_DESCRIPTION
291+
const state = process.env.STATUS_STATE
292+
293+
github.rest.repos.createCommitStatus({
294+
owner: context.repo.owner,
295+
repo: context.repo.repo,
296+
sha: context.payload.pull_request.head.sha,
297+
state: state,
298+
context: 'conftest test',
299+
description: description,
300+
target_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`
301+
})

0 commit comments

Comments
 (0)