-
Notifications
You must be signed in to change notification settings - Fork 118
205 lines (180 loc) · 8.63 KB
/
templates-dyff.yml
File metadata and controls
205 lines (180 loc) · 8.63 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Copyright 2025 New Vector Ltd
# Copyright 2025-2026 Element Creations Ltd
#
# SPDX-License-Identifier: AGPL-3.0-only
name: dyff of rendered templates
on:
pull_request:
permissions:
contents: read
jobs:
generate-dyff:
runs-on: ubuntu-latest
steps:
# This checks out the merge of the PR branch and the base branch.
# If you check this to be just the PR branch then the ref for the subsequent checkout
# needs to be changed too or you end up with unexpected changes being shown.
- name: Checkout PR
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
fetch-depth: 0
- uses: ./.github/actions/asdf-cache
with:
tool-versions: |
dyff 1.10.1
# helm template doesn't reliably order manifests within the same kind, so use yq to do it for us
- name: Generate manifests for PR
id: generate-manifests
run: |
mkdir -p "$RUNNER_TEMP/new"
for values in charts/matrix-stack/ci*/*values.yaml; do
echo "Generating new templates with $values";
mkdir -p "$RUNNER_TEMP/new/$(basename "$values" ".yaml")"
helm template \
-n ess-ci \
-a monitoring.coreos.com/v1/ServiceMonitor \
-a cert-manager.io/v1/Certificate \
-f "$values" charts/matrix-stack | \
yq ea '[.] | .[] | splitDoc' | \
yq -s "\"$RUNNER_TEMP/new/$(basename "$values" ".yaml")/\""' + ([.kind, .metadata.name] | join("-") | downcase) + ".yaml"'
done
echo "output_dir=$RUNNER_TEMP/new" | tee -a "$GITHUB_OUTPUT"
# https://github.com/orgs/community/discussions/59677 says that github.event.pull_request.base.sha
# is only calculated on creation of the PR, so we reference the target branch by ref.
- name: Checkout target
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
ref: ${{ github.event.pull_request.base.ref }}
# We dont clean as it would remove the .tool-versions file
clean: false
- name: Generate manifests for base
run: |
mkdir -p "$RUNNER_TEMP/old"
for values in charts/matrix-stack/ci*/*values.yaml; do
echo "Generating old templates with $values";
mkdir -p "$RUNNER_TEMP/old/$(basename "$values" ".yaml")"
helm template \
-n ess-ci \
-a monitoring.coreos.com/v1/ServiceMonitor \
-a cert-manager.io/v1/Certificate \
-f "$values" charts/matrix-stack | \
yq ea '[.] | .[] | splitDoc' | \
yq -s "\"$RUNNER_TEMP/old/$(basename "$values" ".yaml")/\""' + ([.kind, .metadata.name] | join("-") | downcase) + ".yaml"'
done
- name: Upload new manifests
id: upload-new
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: new-manifests
path: ${{ steps.generate-manifests.outputs.output_dir }}
retention-days: 1
- name: dyff old and new manifests
id: dyff
shell: bash
env:
ARTIFACT_URL: ${{ steps.upload-new.outputs.artifact-url }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
echo "output_dir=$RUNNER_TEMP" | tee -a "$GITHUB_OUTPUT"
values_directories=$(find "$RUNNER_TEMP/old" "$RUNNER_TEMP/new" -maxdepth 1 -type d | sed -E 's|'"$RUNNER_TEMP"'/(old\|new)||' | sed -E 's|^/||' | sort | uniq)
header="# dyff of changes in rendered templates of CI manifests\n\n"
comment_body=""
while read -r values_dir; do
if [ -z "$values_dir" ]; then
continue
fi
templates_files=$(find "$RUNNER_TEMP/old" "$RUNNER_TEMP/new" -maxdepth 2 -name '*.yaml' | grep "/$values_dir/" | sed -E 's|'"$RUNNER_TEMP"'/(old\|new)/||' | sort | uniq)
comment_templates_body=""
values_file_suffix=""
on_both_branches="true"
if [ ! -d "$RUNNER_TEMP/old/$values_dir" ]; then
values_file_suffix=" (added)"
on_both_branches="false"
elif [ ! -d "$RUNNER_TEMP/new/$values_dir" ]; then
values_file_suffix=" (removed)"
on_both_branches="false"
fi
while read -r templates_file; do
current_file="$(basename "$templates_file")"
if [[ "$current_file" == ".yaml" ]] && [ ! -s "$template_file" ]; then
continue
fi
if [ ! -f "$RUNNER_TEMP/old/$templates_file" ]; then
kind=$(yq '.kind' "$RUNNER_TEMP/new/$templates_file")
name=$(yq '.metadata.name' "$RUNNER_TEMP/new/$templates_file")
namespace=$(yq '.metadata.namespace' "$RUNNER_TEMP/new/$templates_file")
if [ "$on_both_branches" == "true" ]; then
comment_templates_body+="@@ $kind/$namespace/$name @@\n"
while IFS= read -r line; do
comment_templates_body+="+ $line\n"
done < "$RUNNER_TEMP/new/$templates_file"
comment_templates_body+="\n\n"
else
comment_templates_body+="$kind/$namespace/$name added\n"
fi
continue
fi
if [ ! -f "$RUNNER_TEMP/new/$templates_file" ]; then
kind=$(yq '.kind' "$RUNNER_TEMP/old/$templates_file")
name=$(yq '.metadata.name' "$RUNNER_TEMP/old/$templates_file")
namespace=$(yq '.metadata.namespace' "$RUNNER_TEMP/old/$templates_file")
if [ "$on_both_branches" == "true" ]; then
comment_templates_body+="@@ $kind/$namespace/$name @@\n"
while IFS= read -r line; do
comment_templates_body+="- $line\n"
done < "$RUNNER_TEMP/old/$templates_file"
comment_templates_body+="\n\n"
else
comment_templates_body+="$kind/$namespace/$name removed\n"
fi
continue
fi
excludes=()
# We remove hash labels as we have tests confirming they're set correctly and they just provide noise in dyff
excludes+=('--exclude-regexp' 'metadata.labels.k8s.element.io/.*-hash')
excludes+=('--exclude-regexp' 'spec.template.metadata.labels.k8s.element.io/.*-hash')
# We remove version label as we have tests for it and it just provides noise in dyff
excludes+=('--exclude-regexp' 'metadata.labels.app.kubernetes.io/version')
excludes+=('--exclude-regexp' 'spec.template.metadata.labels.app.kubernetes.io/version')
exit_code=0
dyff_detail=$(dyff between --set-exit-code --omit-header "${excludes[@]}" --output=github "$RUNNER_TEMP/old/$templates_file" "$RUNNER_TEMP/new/$templates_file" 2>&1) || exit_code=$?
if [ $exit_code -ne 0 ]; then
if [[ "$dyff_detail" == *"failed to compare input files"* ]]; then
echo "failed with file $templates_file"
exit 1
fi
kind=$(yq '.kind' "$RUNNER_TEMP/new/$templates_file")
name=$(yq '.metadata.name' "$RUNNER_TEMP/new/$templates_file")
namespace=$(yq '.metadata.namespace' "$RUNNER_TEMP/new/$templates_file")
comment_templates_body+=$(sed -e '/^!/d' -e "s|^@@\(.*\)@@|@@ $kind/$namespace/$name -\1@@|" <<< "$dyff_detail")
comment_templates_body+="\n\n\n"
fi
done <<< "$templates_files"
if [[ -n "$comment_templates_body" ]]; then
comment_body+="<details><summary><b>$values_dir.yaml$values_file_suffix</b></summary>\n"
comment_body+='\n```diff\n'
comment_body+="$comment_templates_body"
comment_body+='```\n'
comment_body+="\n</details>\n"
fi
done <<< "$values_directories"
if [ -z "$comment_body" ]; then
comment_body="No changes in rendered templates"
else
header+="Full contents of manifests and dyffs are available in ${ARTIFACT_URL}\n\n"
fi
echo -e "$header$comment_body" | tee "$RUNNER_TEMP/dyff-output.md"
echo "$PR_NUMBER" | tee "$RUNNER_TEMP/pr-number.txt"
- name: Upload generated manifests
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: dyff-templates
path: ${{ steps.dyff.outputs.output_dir }}
retention-days: 1
# Restore the current PR git state for post-step actions
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false