generated from onedr0p/cluster-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
250 lines (218 loc) · 7.62 KB
/
pre-pull-images.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
---
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: "Pre-pull Images"
on:
pull_request:
branches: ["main"]
paths: ["kubernetes/**"]
concurrency:
group: ${{ github.workflow }}-${{ github.event.number || github.ref }}
cancel-in-progress: true
jobs:
changed-clusters:
name: Changed Clusters
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.changed-clusters.outputs.all_changed_and_modified_files }}
steps:
- name: Configure 1password
uses: 1password/load-secrets-action/configure@v2
with:
service-account-token: ${{ secrets.ONEPASS_SA_TOKEN }}
- name: Get Secrets
uses: 1password/load-secrets-action@v2
with:
export-env: true
env:
BOT_APP_ID: op://Kubernetes/github-bot/BOT_APP_ID
BOT_APP_PRIVATE_KEY: op://Kubernetes/github-bot/BOT_APP_PRIVATE_KEY
- name: Generate Token
uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ env.BOT_APP_ID }}
private-key: ${{ env.BOT_APP_PRIVATE_KEY }}
- name: Checkout
uses: actions/checkout@v4
with:
token: ${{ steps.app-token.outputs.token }}
fetch-depth: 0
- name: Get Changed Clusters
id: changed-clusters
uses: tj-actions/changed-files@v45
with:
files: kubernetes/**
files_ignore: kubernetes/shared/**
dir_names: true
dir_names_max_depth: 2
matrix: true
- name: List All Changed Clusters
run: echo ${{ steps.changed-clusters.outputs.all_changed_and_modified_files }}
extract-images:
name: Extract Images
runs-on: ubuntu-latest
needs: ["changed-clusters"]
permissions:
pull-requests: write
strategy:
matrix:
paths: ${{ fromJSON(needs.changed-clusters.outputs.matrix) }}
max-parallel: 4
fail-fast: false
outputs:
matrix: ${{ steps.extract-images.outputs.images }}
steps:
- name: Configure 1password
uses: 1password/load-secrets-action/configure@v2
with:
service-account-token: ${{ secrets.ONEPASS_SA_TOKEN }}
- name: Get Secrets
uses: 1password/load-secrets-action@v2
with:
export-env: true
env:
BOT_APP_ID: op://Kubernetes/github-bot/BOT_APP_ID
BOT_APP_PRIVATE_KEY: op://Kubernetes/github-bot/BOT_APP_PRIVATE_KEY
- name: Generate Token
uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ env.BOT_APP_ID }}
private-key: ${{ env.BOT_APP_PRIVATE_KEY }}
- name: Setup mise
uses: jdx/mise-action@v2
with:
mise_toml: |
[tools]
"aqua:mikefarah/yq" = "latest"
- name: Checkout Default Branch
uses: actions/checkout@v4
with:
token: ${{ steps.app-token.outputs.token }}
ref: ${{ github.event.repository.default_branch }}
path: default
- name: Checkout Pull Request Branch
uses: actions/checkout@v4
with:
token: ${{ steps.app-token.outputs.token }}
path: pull
- name: Gather Images in Default Branch
uses: docker://ghcr.io/allenporter/flux-local:v6.1.1
with:
args: >-
get cluster
--path /github/workspace/default/${{ matrix.paths }}/flux
--enable-images
--output yaml
--output-file default.yaml
- name: Gather Images in Pull Request Branch
uses: docker://ghcr.io/allenporter/flux-local:v6.1.1
with:
args: >-
get cluster
--path /github/workspace/pull/${{ matrix.paths }}/flux
--enable-images
--output yaml
--output-file pull.yaml
- name: Filter Default Branch Results
shell: bash
run: |
yq -r '[.. | .images? | select(. != null)] | flatten | sort | unique | .[]' \
default.yaml > default.txt
- name: Filter Pull Request Branch Results
shell: bash
run: |
yq -r '[.. | .images? | select(. != null)] | flatten | sort | unique | .[]' \
pull.yaml > pull.txt
- name: Compare Default and Pull Request Images
id: extract-images
shell: bash
run: |
images=$(jq --compact-output --raw-input --null-input '[inputs]' < <(grep -vf default.txt pull.txt))
echo "images=${images}" >> $GITHUB_OUTPUT
echo "${images}"
echo "### Images" >> $GITHUB_STEP_SUMMARY
echo "${images}" | jq --raw-output 'to_entries[] | "* \(.value)"' >> $GITHUB_STEP_SUMMARY
generate-matrix:
name: Generate Matrix
runs-on: ubuntu-latest
needs: ["changed-clusters", "extract-images"]
outputs:
matrix: ${{ steps.generate-matrix.outputs.matrix }}
steps:
- name: Setup mise
uses: jdx/mise-action@v2
with:
mise_toml: |
[tools]
"aqua:mikefarah/yq" = "latest"
- name: Generate Combined Matrix
id: generate-matrix
run: |
images=${{ needs.extract-images.outputs.matrix }}
clusters=${{ needs.changed-clusters.outputs.matrix }}
# Define the talosconfig mapping as a valid JSON object
configs='{
"kubernetes/main": "op://Kubernetes/kubernetes/TALOSCONFIG_MAIN",
"kubernetes/utility": "op://Kubernetes/kubernetes/TALOSCONFIG_UTILITY"
}'
# Debugging outputs to check if they are correctly defined
echo "Images: $images"
echo "Clusters: $clusters"
echo "Configs: $configs"
# Use jq with proper variable passing
combined=$(jq -n --argjson images "$images" --argjson clusters "$clusters" --argjson configs "$configs" '
[
$images[] as $image |
$clusters[] as $cluster |
{
image: $image,
cluster: $cluster,
talosconfig: $configs[$cluster]
}
]
')
# Output the combined result
echo "Combined: $combined"
# Save to GITHUB_OUTPUT
echo "matrix=$combined" >> $GITHUB_OUTPUT
- name: Debug Combined Matrix
run: echo "${{ steps.generate-matrix.outputs.matrix }}"
pre-pull-images:
if: ${{ needs.extract-images.outputs.matrix != '[]' }}
name: Pre-pull Images
runs-on: ["gha-runner-scale-set"]
needs: ["generate-matrix"]
strategy:
matrix: ${{ fromJSON(needs.generate-matrix.outputs.matrix) }}
max-parallel: 4
fail-fast: false
steps:
- name: Configure 1password
uses: 1password/load-secrets-action/configure@v2
with:
service-account-token: ${{ secrets.ONEPASS_SA_TOKEN }}
- name: Get Secrets
uses: 1password/load-secrets-action@v2
with:
export-env: true
env:
TALOSCONFIG: ${{ matrix.talosconfig }}
- name: Setup mise
uses: jdx/mise-action@v2
with:
mise_toml: |
[tools]
"aqua:siderolabs/talos" = "latest"
- name: Pre-pull Image
run: talosctl -n $NODE_IP image pull ${{ matrix.images }}
# Summarize matrix https://github.community/t/status-check-for-a-matrix-jobs/127354/7
pre-pull-images-success:
if: ${{ always() }}
needs: ["pre-pull-images"]
name: Pre-pull Images Successful
runs-on: ubuntu-latest
steps:
- if: ${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') }}
name: Check matrix status
run: exit 1