-
Notifications
You must be signed in to change notification settings - Fork 1
224 lines (191 loc) · 8.82 KB
/
ci.yml
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
name: Continuous Integration
on:
push:
branches: [ master ]
jobs:
flake-health-check:
runs-on: ubuntu-latest
name: Flake Health Check
steps:
- uses: actions/checkout@v4
- uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/flake-checker-action@main
with:
ignore-missing-flake-lock: false
fail-mode: true
get-changes:
runs-on: ubuntu-latest
outputs:
changed_crates: ${{ steps.extract-crates.outputs.changed_crates }}
rebuild_all: ${{ steps.check-root-changes.outputs.rebuild_all }}
binaries: ${{ steps.generate-matrix.outputs.available_binaries }}
matrix: ${{ steps.generate-matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions-rust-lang/setup-rust-toolchain@v1
- name: Extract Changed Crates
id: extract-crates
run: |
changes=$(git diff --name-only ${{ github.event.before }} ${{ github.event.ref }} | grep -oP 'crates/[^/]+/' | sort -u)
crates=$(echo "$changes" | sed -E 's/crates\/([^\/]+)\//\1/g' | sort -u)
readarray -t crates <<< "$crates"
changed_crates=$(IFS=, ; echo "${crates[*]}")
echo "changed_crates=$changed_crates" >> "$GITHUB_OUTPUT"
echo "Changed Crates: $changed_crates"
# If any of the root files have changed we should rebuild all crates.
- name: Check for Root Changes
id: check-root-changes
if: ${{ steps.extract-crates.outputs.changed_crates == '' }}
run: |
changes=$(git diff --name-only ${{ github.event.before }} ${{ github.event.ref }} | grep -oP '^(Cargo.toml|Cargo.lock|flake.nix|.github/workflows/ci.yml)$' | sort -u)
if [ -n "$changes" ]; then
echo "rebuild_all=true" >> "$GITHUB_OUTPUT"
echo "All Crates will be rebuilt due to changes in root build files."
fi
- name: Generate Matrix
id: generate-matrix
run: |
if [ "${{ steps.check-root-changes.outputs.rebuild_all }}" == true ]; then
crates=$(find crates -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
else
readarray -d ',' -t crates <<< '${{ steps.extract-crates.outputs.changed_crates }}'
fi
echo "Crates: ${crates[*]}"
if [ ${#crates[@]} -eq 0 ]; then
echo "No crates have changed."
echo "matrix={}" >> "$GITHUB_OUTPUT"
exit 0
fi
cargo_metadata=$(cargo metadata --no-deps --format-version 1)
readarray -t default_members < <(echo "$cargo_metadata" | jq -r '.workspace_default_members[] | split("#")[0]' | xargs -n1 basename)
echo "available_binaries=$(echo "$cargo_metadata" | jq -r '.packages | map(select(.targets[].kind[] | contains("bin")) | .name) | join(",")')" >> "$GITHUB_OUTPUT"
# We only want to build the crates defined as default members.
readarray -t crates < <(comm -12 <(printf "%s\n" "${crates[@]}" | sort) <(printf "%s\n" "${default_members[@]}" | sort))
echo "Default Members: ${default_members[*]}"
echo "Crates to Build: ${crates[*]}"
matrix='{"system": ["x86_64-linux","aarch64-linux","x86_64-windows","aarch64-windows","x86_64-darwin","aarch64-darwin"],"crate": ['
addedAtLeastOne=false
for crate in "${crates[@]}"; do
matrix+="\"$crate\","
addedAtLeastOne=true
done
if [ "$addedAtLeastOne" = true ]; then
matrix="${matrix::-1}"
fi
matrix="${matrix}]}"
echo "$matrix" | jq .
echo "matrix=$matrix" >> "$GITHUB_OUTPUT"
build-artifacts:
needs: [ get-changes ]
if: ${{ needs.get-changes.outputs.matrix != '{}' }}
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.get-changes.outputs.matrix) }}
runs-on: ${{ endsWith(matrix.system, 'darwin') && 'macos-latest' || 'ubuntu-latest' }}
name: Build ${{ matrix.crate }} for ${{ matrix.system }}
steps:
- uses: actions/checkout@v4
- name: Set Swap Space
if: ${{ !endsWith(matrix.system, 'darwin') && !env.ACT }}
uses: pierotofy/set-swap-space@master
with:
swap-size-gb: 12
- uses: cachix/install-nix-action@v30
- uses: DeterminateSystems/magic-nix-cache-action@v8
- run: nix build .#${{ matrix.crate }}-${{ matrix.system }}-dev -L --accept-flake-config
- uses: actions/[email protected]
if: ${{ contains(needs.get-changes.outputs.binaries, matrix.crate) }}
with:
name: ${{ matrix.crate }}-${{ matrix.system }}
path: result/bin/
if-no-files-found: error
- run: touch "build-success-${{ matrix.crate }}-${{ matrix.system }}"
- uses: actions/upload-artifact@v4
with:
name: build-success-${{ matrix.crate }}-${{ matrix.system }}
path: build-success-${{ matrix.crate }}-${{ matrix.system }}
determine-tests:
needs: [ get-changes, build-artifacts ]
if: ${{ always() && needs.get-changes.outputs.matrix != '{}' && toJson(fromJson(needs.get-changes.outputs.matrix).crate) != '[]' }}
runs-on: ubuntu-latest
outputs:
test-matrix: ${{ steps.get-test-matrix.outputs.matrix }}
steps:
- uses: actions/download-artifact@v4
with:
pattern: build-success-*
- name: Get Test Matrix
id: get-test-matrix
shell: bash
run: |
crate_systems=$(echo '${{ needs.get-changes.outputs.matrix }}' | jq -r '.crate[] as $crate | .system[] as $system | "\($crate)-\($system)"')
build_success_files=$(find . -maxdepth 2 -type f -name "build-success-*" -exec basename {} \;)
echo "Build Success Files: $build_success_files"
declare -A found_artifacts=()
for crate_system in $crate_systems; do
echo "Checking for build-success-$crate_system"
if [[ ${build_success_files[*]} =~ build-success-$crate_system ]]; then
echo "Found build-success-$crate_system"
readarray -d '-' -t split <<< "$crate_system"
split_len=${#split[@]}
crate=$(IFS=$'-' ; echo "${split[*]:0:$split_len-2}")
system=$(IFS=$'-' ; echo "${split[*]:$split_len-2:2}")
found_artifacts["$crate"]+="$system,"
fi
done
if [ ${#found_artifacts[@]} = 0 ]; then
echo "No artifacts found."
echo "matrix={}" >> "$GITHUB_OUTPUT"
exit 0
fi
matrix='{"system": ["x86_64-linux","aarch64-linux","x86_64-windows","aarch64-windows","x86_64-darwin","aarch64-darwin"], "crate": ['
addedAtLeastOne=false
for crate in "${!found_artifacts[@]}"; do
systems=${found_artifacts["$crate"]}
systems=${systems::-1}
matrix+="{\"name\": \"$crate\", \"systems\": [\"${systems//,/\",\"}\"]},"
addedAtLeastOne=true
done
if [ "$addedAtLeastOne" = true ]; then
matrix="${matrix::-1}"
fi
matrix="${matrix}]}"
echo "$matrix" | jq .
echo "matrix=$matrix" >> "$GITHUB_OUTPUT"
# We always want to cleanup the matrix artifacts.
- uses: geekyeggo/delete-artifact@v5
if: ${{ always() }}
with:
name: build-success-*
run-tests:
needs: [ determine-tests ]
if: ${{ needs.determine-tests.outputs.test-matrix != '{}' }}
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.determine-tests.outputs.test-matrix) }}
runs-on: ${{ endsWith(matrix.system, 'darwin') && 'macos-latest' || 'ubuntu-latest' }}
name: Run Tests for ${{ matrix.crate.name }} on ${{ matrix.system }}
steps:
- uses: actions/checkout@v4
- uses: cachix/install-nix-action@v30
- uses: DeterminateSystems/magic-nix-cache-action@v8
- name: Should we run tests?
id: should-run-tests
run: |
echo "run_tests=false" >> "$GITHUB_OUTPUT"
host_system=$(nix eval --raw nixpkgs#system)
successful_builds=(${{ matrix.crate.systems }})
# If crate.systems does not contain the host system, the build was not successful and we should exit.
if [[ ${successful_builds[*]} =~ $host_system ]]; then
echo "Build was not successful for $host_system."
exit 0
fi
echo "run_tests=true" >> "$GITHUB_OUTPUT"
- name: Run Tests
if: ${{ steps.should-run-tests.outputs.run_tests == 'true' }}
run: nix develop --impure -c cargo nextest run --package ${{ matrix.crate.name }} --all-features
- name: Run Doc Tests
if: ${{ steps.should-run-tests.outputs.run_tests == 'true' }}
run: nix develop --impure -c cargo test --doc --package ${{ matrix.crate.name }}