-
Notifications
You must be signed in to change notification settings - Fork 0
311 lines (265 loc) · 10.1 KB
/
Copy pathcheck-pr.yml
File metadata and controls
311 lines (265 loc) · 10.1 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
name: Check PR
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
paths:
- 'pkgs/**'
- 'modules/**'
- 'flake.nix'
- 'flake.lock'
- '.github/workflows/check-pr.yml'
permissions:
contents: read
jobs:
changed-things:
name: Build changed packages and module checks
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false
steps:
- name: Free disk space
uses: jlumbroso/free-disk-space@v1.3.1
with:
# Keep existing swap; large Nix builds benefit more from swap than
# the small amount of disk space reclaimed by removing it.
swap-storage: false
- name: Add build swap
run: |
set -euo pipefail
sudo fallocate -l 16G /mnt/nix-build-swapfile
sudo chmod 600 /mnt/nix-build-swapfile
sudo mkswap /mnt/nix-build-swapfile
sudo swapon /mnt/nix-build-swapfile
free -h
- name: Checkout PR head
uses: actions/checkout@v4
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
- name: Fetch base commit
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
run: |
set -euo pipefail
git fetch --no-tags --depth=1 https://github.com/${{ github.repository }}.git "$BASE_SHA"
- uses: cachix/install-nix-action@v31
with:
nix_path: nixpkgs=channel:nixos-unstable
- uses: cachix/cachix-action@v17
with:
name: tixpkgs
authToken: ${{ secrets.CACHIX_AUTH_TOKEN }}
- name: Detect changed packages and module checks
id: detect
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
# shellcheck disable=SC2016,SC2295
set -euo pipefail
git diff --name-only "$BASE_SHA" "$HEAD_SHA" | sort -u > changed-files.txt
echo "Changed files:"
sed 's/^/ /' changed-files.txt
mapfile -t all_packages < <(
nix eval .#packages.x86_64-linux \
--apply 'pkgs: builtins.attrNames pkgs' \
--json | jq -r '.[]' | sort -u
)
mapfile -t all_module_checks < <(
nix eval .#checks.x86_64-linux \
--apply 'checks: builtins.attrNames checks' \
--json | jq -r '.[]' | grep '^module-' | sort -u || true
)
package_exists() {
local needle="$1"
printf '%s\n' "${all_packages[@]}" | grep -Fxq -- "$needle"
}
add_unique() {
local file="$1"
local value="$2"
grep -Fxq -- "$value" "$file" 2>/dev/null || printf '%s\n' "$value" >> "$file"
}
module_check_exists() {
local needle="$1"
printf '%s\n' "${all_module_checks[@]}" | grep -Fxq -- "$needle"
}
add_checks_for_module() {
local kind="$1"
local module_path="$2"
local prefix="module-${kind}-${module_path//\//-}-"
local found=false
while IFS= read -r check; do
if [[ "$check" == "$prefix"* ]]; then
add_unique module-checks.txt "$check"
found=true
fi
done < <(printf '%s\n' "${all_module_checks[@]}")
if [ "$found" = false ]; then
printf "No module check found for %s module %s (looked for %s*).\n" \
"$kind" "$module_path" "$prefix"
fi
}
module_path_from_file() {
local kind="$1"
local file="$2"
local base="modules/${kind}/"
local rel="${file#"$base"}"
local name dir stem
name="$(basename "$rel")"
dir="$(dirname "$rel")"
if [ "$name" = "default.nix" ] || [ "$name" = "README.md" ]; then
[ "$dir" != "." ] && printf '%s\n' "$dir"
return 0
fi
local probe="$dir"
while [ "$probe" != "." ]; do
if [ -f "modules/${kind}/${probe}/default.nix" ]; then
printf '%s\n' "$probe"
return 0
fi
probe="$(dirname "$probe")"
done
if [[ "$name" == *.nix ]]; then
stem="${rel%.nix}"
printf '%s\n' "$stem"
fi
}
: > packages.txt
: > module-checks.txt
while IFS= read -r file; do
case "$file" in
pkgs/*)
IFS=/ read -r _top _bucket package _rest <<< "$file"
if [ -n "${package:-}" ]; then
if [[ -z "${_rest:-}" && "$package" == *.nix ]]; then
package="${package%.nix}"
fi
if package_exists "$package"; then
add_unique packages.txt "$package"
else
printf "Changed package path %s maps to %s, but that package is not currently exported.\n" \
"$file" "$package"
fi
fi
;;
modules/nixos/*)
module_path="$(module_path_from_file nixos "$file" || true)"
if [ -n "${module_path:-}" ]; then
add_checks_for_module nixos "$module_path"
fi
;;
modules/home-manager/*)
module_path="$(module_path_from_file home-manager "$file" || true)"
if [ -n "${module_path:-}" ]; then
add_checks_for_module home-manager "$module_path"
fi
;;
modules/tests/nixos/*.nix)
rel="${file#modules/tests/nixos/}"
check="module-nixos-${rel%.nix}"
check="${check//\//-}"
if module_check_exists "$check"; then
add_unique module-checks.txt "$check"
else
printf "Changed test %s did not map to an exported check (%s).\n" "$file" "$check"
fi
;;
modules/tests/home-manager/*.nix)
rel="${file#modules/tests/home-manager/}"
check="module-home-manager-${rel%.nix}"
check="${check//\//-}"
if module_check_exists "$check"; then
add_unique module-checks.txt "$check"
else
printf "Changed test %s did not map to an exported check (%s).\n" "$file" "$check"
fi
;;
modules/flake/checks.nix|modules/flake/modules.nix)
printf '%s\n' "${all_module_checks[@]}" >> module-checks.txt
sort -u -o module-checks.txt module-checks.txt
;;
modules/flake/packages.nix)
printf '%s\n' "${all_packages[@]}" >> packages.txt
sort -u -o packages.txt packages.txt
;;
esac
done < changed-files.txt
sort -u -o packages.txt packages.txt
sort -u -o module-checks.txt module-checks.txt
packages_json="$(jq -Rsc 'split("\n") | map(select(length > 0))' packages.txt)"
module_checks_json="$(jq -Rsc 'split("\n") | map(select(length > 0))' module-checks.txt)"
{
echo "packages=${packages_json}"
echo "module-checks=${module_checks_json}"
} >> "$GITHUB_OUTPUT"
{
echo "## Detected PR checks"
echo
echo "### Packages"
if [ -s packages.txt ]; then while IFS= read -r package; do printf -- "- \`%s\`\n" "$package"; done < packages.txt; else echo "- none"; fi
echo
echo "### Module checks"
if [ -s module-checks.txt ]; then while IFS= read -r check; do printf -- "- \`%s\`\n" "$check"; done < module-checks.txt; else echo "- none"; fi
} >> "$GITHUB_STEP_SUMMARY"
- name: Build changed packages
env:
PACKAGES: ${{ steps.detect.outputs.packages }}
run: |
set -euo pipefail
mapfile -t packages < <(jq -r '.[]' <<< "$PACKAGES")
if [ "${#packages[@]}" -eq 0 ]; then
echo "No changed packages to build."
exit 0
fi
failed=()
for pkg in "${packages[@]}"; do
echo "::group::package ${pkg}"
if nix build --print-build-logs ".#${pkg}"; then
echo "Built package ${pkg}"
else
failed+=("${pkg}")
fi
echo "::endgroup::"
done
if [ "${#failed[@]}" -ne 0 ]; then
printf 'Failed packages: %s\n' "${failed[*]}" >&2
exit 1
fi
- name: Build module checks
env:
MODULE_CHECKS: ${{ steps.detect.outputs.module-checks }}
run: |
set -euo pipefail
mapfile -t checks < <(jq -r '.[]' <<< "$MODULE_CHECKS")
if [ "${#checks[@]}" -eq 0 ]; then
echo "No module checks to build."
exit 0
fi
failed=()
for check in "${checks[@]}"; do
echo "::group::module check ${check}"
if nix build --print-build-logs ".#checks.x86_64-linux.${check}"; then
echo "Built module check ${check}"
else
failed+=("${check}")
fi
echo "::endgroup::"
done
if [ "${#failed[@]}" -ne 0 ]; then
printf 'Failed module checks: %s\n' "${failed[*]}" >&2
exit 1
fi
auto-merge:
name: Enable auto-merge
runs-on: ubuntu-latest
needs: changed-things
if: github.event.pull_request.user.login == '74k2'
permissions:
contents: write
pull-requests: write
steps:
- name: Enable auto-merge
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: gh pr merge "$PR_NUMBER" --auto --squash --delete-branch --repo "$GITHUB_REPOSITORY"