-
Notifications
You must be signed in to change notification settings - Fork 1
225 lines (188 loc) · 8.45 KB
/
k8s.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
name: k8s CI
on:
pull_request:
paths:
- "k8s/**"
- ".github/workflows/k8s.yaml"
push:
paths:
- "k8s/**"
- ".github/workflows/k8s.yaml"
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.ref }}
- run: "mkdir -p ./k8s/tmp/cluster-kustomize"
shell: bash
- run: 'echo -en "apiVersion: kustomize.config.k8s.io/v1beta1\nkind: Kustomization\nresources:\n" > ./k8s/tmp/cluster-kustomize/kustomization.yaml'
shell: bash
- run: 'ls ./k8s/apps/ | while read line; do echo " - ../../apps/$line" >> ./k8s/tmp/cluster-kustomize/kustomization.yaml ; done'
shell: bash
- uses: azure/k8s-bake@v3
with:
renderEngine: "kustomize"
kustomizationPath: "./k8s/tmp/cluster-kustomize/"
kubectl-version: "latest"
id: base_bake
- run: "mv ${{ steps.base_bake.outputs.manifestsBundle }} /tmp/manifests-base.yaml"
- uses: actions/checkout@v4
- run: "ls ./k8s/"
shell: bash
- run: "mkdir -p ./k8s/tmp/cluster-kustomize"
shell: bash
- run: 'echo -en "apiVersion: kustomize.config.k8s.io/v1beta1\nkind: Kustomization\nresources:\n" > ./k8s/tmp/cluster-kustomize/kustomization.yaml'
shell: bash
- run: 'ls ./k8s/apps/ | while read line; do echo " - ../../apps/$line" >> ./k8s/tmp/cluster-kustomize/kustomization.yaml ; done'
shell: bash
- uses: azure/k8s-bake@v3
with:
renderEngine: "kustomize"
kustomizationPath: ./k8s/tmp/cluster-kustomize/
kubectl-version: "latest"
id: base_changed
- run: "mv ${{ steps.base_changed.outputs.manifestsBundle }} /tmp/manifests-head.yaml"
# Helm Diff
- name: install dependencies
run: |
# Install jq
sudo wget https://github.com/jqlang/jq/releases/download/jq-1.7.1/jq-linux-amd64 -O /usr/bin/jq
sudo chmod +x /usr/bin/jq
# Install yq
sudo wget https://github.com/mikefarah/yq/releases/download/v4.43.1/yq_linux_amd64 -O /usr/bin/yq
sudo chmod +x /usr/bin/yq
# install helm
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | sudo bash
- name: HelmReleaseとHelmRepoを抽出
run: |
cat /tmp/manifests-head.yaml |yq -j '. | select(.kind == "HelmRelease") | sort_keys(.metadata.name .metadata.namespace)' > /tmp/helmreleases-head.json
cat /tmp/manifests-head.yaml |yq '. | select(.kind == "HelmRepository") | sort_keys(.metadata.name .metadata.namespace)' > /tmp/helmrepository-head.yaml
cat /tmp/manifests-base.yaml |yq -j '. | select(.kind == "HelmRelease") | sort_keys(.metadata.name .metadata.namespace)' > /tmp/helmreleases-base.json
cat /tmp/manifests-base.yaml |yq '. | select(.kind == "HelmRepository") | sort_keys(.metadata.name .metadata.namespace)' > /tmp/helmrepository-base.yaml
- name: helm repo add
run: |
# helm repo add head-<.metadata.name-.metadata.namespace> <.spec.url>
cat /tmp/helmrepository-head.yaml | yq -r '"head-" + .metadata.name + "-" + .metadata.namespace + " " + .spec.url' | while read line; do
helm repo add $line
done
# helm repo add base-<.metadata.name-.metadata.namespace> <.spec.url>
cat /tmp/helmrepository-base.yaml | yq -r '"base-" + .metadata.name + "-" + .metadata.namespace + " " + .spec.url' | while read line; do
helm repo add $line
done
- name: helm template
run: |
length=$(cat /tmp/helmreleases-head.json | jq -s length)
for i in $( seq 0 $(($length - 1)) ); do
cat /tmp/helmreleases-head.json | jq -rs ".[$i] | .spec.values" | tee values.json >> /dev/null
command=$(cat /tmp/helmreleases-head.json | jq -rs "\"helm template \" + .[$i].metadata.name + \" --namespace \" + .[$i].metadata.namespace + \" head-\" + .[$i].spec.chart.spec.sourceRef.name + \"-\" + .[$i].metadata.namespace + \"/\" + .[$i].spec.chart.spec.chart + \" --version \" + .[$i].spec.chart.spec.version + \" -f values.json\"")
eval $command | yq "sort_keys(..)" >> /tmp/manifests-head.yaml
done
length=$(cat /tmp/helmreleases-base.json | jq -s length)
for i in $( seq 0 $(($length - 1)) ); do
cat /tmp/helmreleases-base.json | jq -rs ".[$i] | .spec.values" | tee values.json >> /dev/null
command=$(cat /tmp/helmreleases-base.json | jq -rs "\"helm template \" + .[$i].metadata.name + \" --namespace \" + .[$i].metadata.namespace + \" base-\" + .[$i].spec.chart.spec.sourceRef.name + \"-\" + .[$i].metadata.namespace + \"/\" + .[$i].spec.chart.spec.chart + \" --version \" + .[$i].spec.chart.spec.version + \" -f values.json\"")
eval $command | yq "sort_keys(..)" >> /tmp/manifests-base.yaml
done
- name: Upload file status_job02.txt as an artifact
uses: actions/upload-artifact@v4
with:
if-no-files-found: error
name: manifests
path: |
/tmp/manifests-base.yaml
/tmp/manifests-head.yaml
diff:
runs-on: ubuntu-latest
needs: build
if: github.event_name == 'pull_request'
steps:
- uses: actions/download-artifact@v4
with:
name: manifests
- name: install dyff
run: |
wget https://github.com/homeport/dyff/releases/download/v1.8.0/dyff_1.8.0_linux_amd64.tar.gz
tar -xvf dyff_1.8.0_linux_amd64.tar.gz
chmod +x dyff
- name: Build markdown comment with manifest diff
run: |
echo "# Manifest diff
<details>
<summary>Click to expand</summary>
\`\`\`diff
$(diff -u manifests-base.yaml manifests-head.yaml)
\`\`\`
</details>" | tee /tmp/diff.md
# PRへのコメントだけど、issue commentのAPIを使うらしい
- name: Comment manifest diff to GitHub PR
run: |
cat /tmp/diff.md | jq -Rs '{ "body": . }' | curl --fail \
-X POST -H 'Accept: application/vnd.github.v3+json' \
--header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.number }}/comments \
-d @-
- name: Build markdown comment with manifest dyff
run: |
echo "# Manifest dyff
<details>
<summary>Click to expand</summary>
\`\`\`diff
$(./dyff between manifests-base.yaml manifests-head.yaml)
\`\`\`
</details>" | tee /tmp/dyff.md
# PRへのコメントだけど、issue commentのAPIを使うらしい
- name: Comment manifest diff to GitHub PR
run: |
cat /tmp/dyff.md | jq -Rs '{ "body": . }' | curl --fail \
-X POST -H 'Accept: application/vnd.github.v3+json' \
--header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.number }}/comments \
-d @-
lint:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/download-artifact@v4
with:
name: manifests
- name: Lint manifest with kubeconform
uses: docker://ghcr.io/yannh/kubeconform:latest
with:
args: "-ignore-missing-schemas -strict -summary manifests-head.yaml"
failed:
runs-on: ubuntu-latest
needs: lint
if: failure()
steps:
- name: Send GitHub Action trigger data to Slack workflow
id: slack
uses: slackapi/[email protected]
with:
payload: |
{
"text": "Workflow ${{ github.workflow }} failed",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Workflow ${{ github.workflow }} failed"
}
},
{
"type": "section",
"block_id": "section789",
"fields": [
{
"type": "mrkdwn",
"text": "error: ${{ job.status }}"
}
]
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}