-
Notifications
You must be signed in to change notification settings - Fork 155
214 lines (183 loc) · 7.83 KB
/
presubmit-build.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
on:
pull_request: # TODO: pull_request_target
branches:
- main
permissions:
contents: read
actions: read
env:
TF_VAR_target_repository: registry.local:5000/testing
TF_APKO_DISABLE_VERSION_TAGS: true # Disable version tags.
TF_COSIGN_DISABLE: true # Don't sign in presubmit.
concurrency:
group: presubmit-build-${{ github.head_ref }}
cancel-in-progress: true
jobs:
shard:
runs-on: ubuntu-latest
steps:
- uses: step-security/harden-runner@a4aa98b93cab29d9b1101a6143fb8bce00e2eac4 # v2.7.1
with:
egress-policy: audit
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
with:
ref: ${{ github.event.pull_request.head.sha || github.sha }}
- name: Ensure no diff on tfgen
run: |
set -x
make tfgen
if ! git diff-index --quiet HEAD --; then
echo "Error: Please re-run \"make tfgen\" and commit the result."
exit 1
fi
- run: make init-upgrade # Initialize modules.
- run: terraform fmt -check -recursive -diff
- name: Enforce YAML formatting
uses: docker://ghcr.io/wolfi-dev/wolfictl:latest@sha256:afad17b9d533bba348407f9022d5a6c27ebcd3af7e516039191d3c901b0b899c
with:
entrypoint: wolfictl
args: lint yam images/
- id: changed
uses: tj-actions/changed-files@0874344d6ebbaa00a27da73276ae7162fadcaf69 # v44.3.0
with:
dir_names: "true"
# Truncates (not ignores) the path of changed files to 2 (ie: images/flux/tests/main.tf -> images/flux)
dir_names_max_depth: "2"
files_separator: "\n" # This is the default, but ensure we keep it such so the below discover steps work
files_yaml: |
base:
- .github/actions/**
- ./tflib/**
images:
- ./images/**
- '!./images/**.md'
- id: discover-base
name: Discovered modified base files
shell: bash # bash math foo required
if: steps.changed.outputs.base_any_changed == 'true'
run: |
images=($(find ./images -maxdepth 1 -type d -not -path "./images/TEMPLATE" | awk -F'/' '{print $3}' | sort -u))
n=50 # buckets to shard into
total=${#images[@]}
base_size=$((total / n))
remainder=$((total % n))
declare -a bins
# Sequentially fill up each bin, and append any remainders to the last bin
for ((i = 0; i < total; i++)); do
idx=$((i < (total - remainder) ? i / base_size : n - 1))
bins[$idx]+="${images[$i]} "
done
matrix=$(printf "%s\n" "${bins[@]}" | jq -cRnjr '[inputs] | [ range(0; length) as $i | { "index": $i | tostring, "images": .[$i] } ]')
echo "matrix=${matrix}" >> $GITHUB_ENV
- id: discover-images
name: Discover modified images
if: steps.changed.outputs.base_any_changed != 'true' && steps.changed.outputs.images_any_changed == 'true'
run: |
# Randomize and shard into bins of 5 images each
n=5
bins=$(echo "${{ steps.changed.outputs.images_all_changed_files }}" | sed 's/images\///g' | sed 's/ /\n/g' | sort -uR | tr '\n' ' ' | xargs -n $n | jq -cRnjr '[inputs] | [ range(0; length) as $i | { "index": $i | tostring, "images": .[$i] } ]')
echo "matrix=${bins}" >> $GITHUB_ENV
- id: set-matrix
run: |
out='${{ env.matrix }}'
echo "shard_matrix=${out}" >> $GITHUB_OUTPUT
- name: Shard Results
run: echo '${{ steps.set-matrix.outputs.shard_matrix }}' | jq .
outputs:
# This is of the format [{"index": 0, "images": "a b c"}, {"index": 1, "images": "d e f"}, ...], or empty ([]) when nothing relevant has changed
shard_matrix: ${{ steps.set-matrix.outputs.shard_matrix }}
build-the-world:
runs-on: ubuntu-latest
needs: shard
if: ${{ needs.shard.outputs.shard_matrix != '' }}
strategy:
fail-fast: false
matrix:
shard: ${{ fromJson(needs.shard.outputs.shard_matrix) }}
steps:
- uses: step-security/harden-runner@a4aa98b93cab29d9b1101a6143fb8bce00e2eac4 # v2.7.1
with:
egress-policy: audit
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
with:
ref: ${{ github.event.pull_request.head.sha || github.sha }}
# In some cases, we run out of disk space during tests, so this hack frees up approx 25G.
# See the following issue for more info: https://github.com/actions/runner-images/issues/2840#issuecomment-1284059930
- name: Free up runner disk space
uses: ublue-os/remove-unwanted-software@e3843c85f5f9b73626845de0f5d44fb78ce22e12 # v6
- uses: hashicorp/setup-terraform@97f030cf6dc0b4f5e0da352c7bca9cca34579800 # v3
with:
terraform_version: '1.8.*'
terraform_wrapper: false
# Make cosign/crane CLI available to the tests
- uses: sigstore/cosign-installer@59acb6260d9c0ba8f4a2f9d9b48431a222b68e20 # v3.5.0
- uses: imjasonh/setup-crane@00c9e93efa4e1138c9a7a5c594acd6c75a2fbf0c # v0.3
- uses: chainguard-dev/actions/setup-k3d@538d1927b846546b620784754c33e2a1db86e217 # main
with:
k3s-image: cgr.dev/chainguard/k3s:latest@sha256:2cce22ae3d776f2b924de7735bb10647320b1563dbca4ab1082a1eedae3b81c5
- name: Configure imagetest provider
run: |
# Run a presubmit scoped global imagetest provider override that
# configures the k3s harnesses for the presubmit scoped local registry.
# This _could_ be in the `main.tf` with some conditionals, but since
# this is striclty for presubmit we take this approach to keep things
# simpler.
cat >> main_override.tf <<EOF
provider "imagetest" {
harnesses = {
k3s = {
networks = {
// wire in k3d's default network where the registry lives
"k3d-default" = { name = "k3d-k3s-default" }
}
registries = {
# Mirror the var.target_repository host registry to the local registry.
# This ensures the images that are pushed from the host registry are
# mirrored to the internal hostname:port registry.
"registry.local:5000" = {
mirror = { endpoints = ["http://registry.local:5000"] }
}
}
}
}
}
EOF
- name: Build
timeout-minutes: 60
run: |
set -eo pipefail
make init-upgrade
targets=""
for image in ${{ matrix.shard.images }}; do
targets+=' -target='module."${image}"''
done
terraform apply ${targets} -auto-approve --parallelism=$(nproc) -json | tee /tmp/mega-module.tf.json | jq -r '.["@message"]'
- name: Collect TF diagnostics
if: ${{ always() }}
uses: chainguard-dev/actions/terraform-diag@538d1927b846546b620784754c33e2a1db86e217 # main
with:
json-file: /tmp/mega-module.tf.json
- name: Upload terraform logs
if: always()
uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v3
with:
name: "mega-module-${{ matrix.shard.index }}.tf.json"
path: /tmp/mega-module.tf.json
- name: Collect diagnostics and upload
if: ${{ failure() }}
uses: chainguard-dev/actions/k8s-diag@538d1927b846546b620784754c33e2a1db86e217 # main
with:
cluster-type: k3d
namespace-resources: deploy,ds,sts,pods
presubmit-roundup:
needs: build-the-world
runs-on: ubuntu-latest
if: always()
steps:
- uses: step-security/harden-runner@a4aa98b93cab29d9b1101a6143fb8bce00e2eac4 # v2.7.1
with:
egress-policy: audit
- name: Fail if any previous job failed
env:
GH_TOKEN: ${{ github.token }}
run: exit $(gh run view -R chainguard-images/images ${{github.run_id}} --json jobs | jq | tee /dev/stderr | jq -r '.jobs[0,-2].conclusion' | grep failure | wc -c)