-
Notifications
You must be signed in to change notification settings - Fork 151
166 lines (139 loc) · 5.73 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
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: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
ref: ${{ github.event.pull_request.head.sha || github.sha }}
- run: make init # Initialize modules.
- run: terraform fmt -check -recursive -diff
- run: ./lint.sh
- name: Enforce YAML formatting
uses: docker://ghcr.io/wolfi-dev/wolfictl:latest@sha256:4c9087d2c2fccf2c0f3470cfa695c605bc0dcd7bcbb1ee9c64f80f62d8e1cfdc
with:
entrypoint: wolfictl
args: lint yam images/
- id: changed
uses: tj-actions/changed-files@v42
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 -uR)) # randomize
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: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
ref: ${{ github.event.pull_request.head.sha || github.sha }}
- uses: hashicorp/setup-terraform@a1502cd9e758c50496cc9ac5308c4843bcd56d36 # v3
with:
terraform_version: '1.3.*'
terraform_wrapper: false
# Make cosign/crane CLI available to the tests
- uses: sigstore/cosign-installer@9614fae9e5c5eddabb09f90a270fcb487c9f7149 # v3.3.0
- uses: imjasonh/setup-crane@00c9e93efa4e1138c9a7a5c594acd6c75a2fbf0c # v0.3
- uses: chainguard-dev/actions/setup-k3d@main
with:
k3s-image: cgr.dev/chainguard/k3s:latest@sha256:0ef62d22d89f611f9df50b9cc86d1f23d3d92d39062d4d13e500736f4e29d0d9
- name: Build
timeout-minutes: 60
run: |
set -eo pipefail
make init
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@main
with:
json-file: /tmp/mega-module.tf.json
- name: Upload terraform logs
if: always()
uses: actions/upload-artifact@694cdabd8bdb0f10b2cea11669e1bf5453eed0a6 # 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@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: technote-space/workflow-conclusion-action@45ce8e0eb155657ab8ccf346ade734257fd196a5 # v3.0.3
- if: ${{ env.WORKFLOW_CONCLUSION == 'success' }}
working-directory: /tmp
run: echo ${{ env.WORKFLOW_CONCLUSION }} && exit 0
- if: ${{ env.WORKFLOW_CONCLUSION == 'failure' }}
working-directory: /tmp
run: echo ${{ env.WORKFLOW_CONCLUSION }} && exit 1