Skip to content

Commit 8ad75f9

Browse files
authored
ci: Avoid invoking cargo on non-rust changes (#121)
Currently the `justfile` invokes `cargo` to determine the validator version. This means that it will install the Rust version in `rust-toolchain` when the runner OS includes another Rust version. This is unnecessary in the proxy-init release workflow. This change removes the `action-prune-docker` just recipe to a dedicated script in `.github/docker-prune.sh` so that it can be run without any of the `justfile` dependencies. This also makes the script covered by `just sh-lint`. This change also moves validator packaging into a dedicated `validator/.justfile` so that invoking the top-level justfile does not eagerly load defaults with `cargo`. Signed-off-by: Oliver Gould <[email protected]>
1 parent 82907dd commit 8ad75f9

File tree

5 files changed

+104
-91
lines changed

5 files changed

+104
-91
lines changed

.github/docker-prune.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Delete all files under the buildkit blob directory that are not referred
5+
# to any longer in the cache manifest file
6+
manifest_sha=$(jq -r .manifests[0].digest < "$RUNNER_TEMP/.buildx-cache/index.json")
7+
manifest=${manifest_sha#"sha256:"}
8+
files=("$manifest")
9+
while IFS= read -r f; do
10+
files+=("$f")
11+
done < <(jq -r '.manifests[].digest | sub("^sha256:"; "")' < "$RUNNER_TEMP/.buildx-cache/blobs/sha256/$manifest")
12+
13+
for file in "$RUNNER_TEMP"/.buildx-cache/blobs/sha256/*; do
14+
for name in "${files[@]}"; do
15+
if [[ "${file##*/}" == "$name" ]]; then
16+
rm -f "$file"
17+
echo "deleted: $name"
18+
break;
19+
fi
20+
done
21+
done

.github/workflows/release-proxy-init.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ jobs:
4444
permissions:
4545
id-token: write # needed for signing the images with GitHub OIDC token
4646
steps:
47-
- uses: extractions/setup-just@95b912dc5d3ed106a72907f2f9b91e76d60bdb76
4847
- uses: docker/setup-qemu-action@e81a89b1732b9c48d79cd809d8d81d79c4647a18
4948
- uses: docker/setup-buildx-action@95cb08cb2672c73d4ffd2f422e6d11953d2a9c70
5049

@@ -61,7 +60,7 @@ jobs:
6160
--cache-from type=local,src="$RUNNER_TEMP/.buildx-cache" \
6261
--cache-to type=local,dest="$RUNNER_TEMP/.buildx-cache",mode=max \
6362
--platform="linux/amd64,linux/arm64,linux/arm/v7"
64-
- run: just action-prune-docker
63+
- run: .github/docker-prune.sh
6564

6665
# Only publish images on release
6766
- if: needs.meta.outputs.mode == 'release'

.github/workflows/release-validator.yml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ jobs:
1414
meta:
1515
timeout-minutes: 3
1616
runs-on: ubuntu-latest
17-
container: ghcr.io/linkerd/dev:v30-tools
17+
container: ghcr.io/linkerd/dev:v30-rust
1818
steps:
19+
- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
1920
- id: meta
2021
shell: bash
2122
run: |
@@ -28,18 +29,15 @@ jobs:
2829
) >> "$GITHUB_OUTPUT"
2930
else
3031
sha="${{ github.sha }}"
31-
( echo version="test-${sha:0:7}"
32+
( echo version="v$(just validator --evaluate version)-${sha:0:7}"
3233
echo mode=test
3334
) >> "$GITHUB_OUTPUT"
3435
fi
35-
- if: steps.meta.outputs.mode == 'release'
36-
uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
3736
- if: steps.meta.outputs.mode == 'release'
3837
name: Check that validator version matches release version
3938
shell: bash
4039
run: |
41-
version=$(cargo metadata --format-version=1 \
42-
| jq -r '.packages[] | select(.name == "linkerd-network-validator") | .version')
40+
version=$(just validator --evaluate version)
4341
# shellcheck disable=SC2193
4442
if [[ "v${version}" != '${{ steps.meta.outputs.version }}' ]]; then
4543
echo "::error ::Crate version v${version} does not match tag ${{ steps.meta.outputs.version }}"
@@ -78,7 +76,7 @@ jobs:
7876
) >> "$GITHUB_ENV"
7977
8078
- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
81-
- run: just validator-package
79+
- run: just validator package
8280
env:
8381
CARGO_RELEASE: "1"
8482
VALIDATOR_VERSION: ${{ needs.meta.outputs.version }}

justfile

Lines changed: 12 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ default: lint test
1616

1717
lint: sh-lint md-lint rs-clippy proxy-init-lint action-lint action-dev-check
1818

19-
build: proxy-init-build validator-build
20-
2119
test: rs-test proxy-init-test-unit proxy-init-test-integration
2220

2321
# Check whether the Go code is formatted.
@@ -36,39 +34,6 @@ rs-toolchain := ""
3634

3735
export RUST_BACKTRACE := env_var_or_default("RUST_BACKTRACE", "short")
3836

39-
# The version name to use for packages.
40-
_validator-version := env_var_or_default("VALIDATOR_VERSION", ```
41-
cargo metadata --format-version=1 \
42-
| jq -r '.packages[] | select(.name == "linkerd-network-validator") | .version' \
43-
| head -n 1
44-
```)
45-
46-
# The architecture name to use for packages. Either 'amd64', 'arm64', or 'arm'.
47-
_arch := env_var_or_default("ARCH", "amd64")
48-
49-
# If a `package_arch` is specified, then we change the default cargo `--target`
50-
# to support cross-compilation. Otherwise, we use `rustup` to find the default.
51-
_cargo-target := if _arch == "amd64" {
52-
"x86_64-unknown-linux-musl"
53-
} else if _arch == "arm64" {
54-
"aarch64-unknown-linux-musl"
55-
} else if _arch == "arm" {
56-
"armv7-unknown-linux-musleabihf"
57-
} else {
58-
`rustup show | sed -n 's/^Default host: \(.*\)/\1/p'`
59-
}
60-
61-
# Support cross-compilation when `_arch` changes.
62-
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER := "aarch64-linux-gnu-gcc"
63-
export CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER := "arm-linux-gnueabihf-gcc"
64-
_strip := if _arch == "arm64" { "aarch64-linux-gnu-strip" } else if _arch == "arm" { "arm-linux-gnueabihf-strip" } else { "strip" }
65-
66-
_target-dir := "target" / _cargo-target / rs-build-type
67-
_validator-bin := _target-dir / "linkerd-network-validator"
68-
_validator-package-name := "linkerd-network-validator-" + _validator-version + "-" + _arch
69-
_validator-package-dir := "target/package" / _validator-package-name
70-
_shasum := "shasum -a 256"
71-
7237
_cargo := env_var_or_default("CARGO", "cargo") + if rs-toolchain != "" { " +" + rs-toolchain } else { "" }
7338

7439
# Fetch Rust dependencies
@@ -81,29 +46,26 @@ rs-fmt-check:
8146

8247
# Lint Rust code
8348
rs-clippy:
84-
{{ _cargo }} clippy --frozen --workspace --all-targets --no-deps {{ _cargo-fmt }}
49+
{{ _cargo }} clippy {{ _cargo-build-flags }} --all-targets --no-deps {{ _cargo-fmt }}
8550

8651
# Audit Rust dependencies
8752
rs-audit-deps:
8853
{{ _cargo }} deny check
8954

9055
# Build Rust unit and integration tests
9156
rs-test-build:
92-
{{ _cargo-test }} --no-run --frozen --workspace {{ _cargo-fmt }}
57+
{{ _cargo-test }} {{ _cargo-build-flags }} --workspace --no-run {{ _cargo-fmt }}
9358

9459
# Run unit tests in whole Rust workspace
9560
rs-test *flags:
96-
{{ _cargo-test }} --frozen --workspace \
97-
{{ if rs-build-type == "release" { "--release" } else { "" } }} \
98-
{{ flags }}
61+
{{ _cargo-test }} {{ _cargo-build-flags }} --workspace {{ flags }}
9962

10063
# Check a specific Rust crate
10164
rs-check-dir dir *flags:
10265
cd {{ dir }} \
103-
&& {{ _cargo }} check --frozen \
104-
{{ if rs-build-type == "release" { "--release" } else { "" } }} \
105-
{{ flags }} \
106-
{{ _cargo-fmt }}
66+
&& {{ _cargo }} check {{ _cargo-build-flags }} {{ flags }} {{ _cargo-fmt }}
67+
68+
_cargo-build-flags := "--frozen" + if rs-build-type == "release" { " --release" } else { "" }
10769

10870
# If recipe is run in github actions (and cargo-action-fmt is installed), then add a
10971
# command suffix that formats errors
@@ -115,10 +77,10 @@ _cargo-fmt := if env_var_or_default("GITHUB_ACTIONS", "") != "true" { "" } else
11577
```
11678
}
11779

118-
# When available, use cargo-nextest to run Rust tests; if the binary is not available,
119-
# use default test runner
80+
# Use cargo-nextest to run Rust tests if available locally. We use the default
81+
# runner in CI.
12082
_cargo-test := _cargo + ```
121-
if command -v cargo-nextest >/dev/null 2>&1 ; then
83+
if [ "${GITHUB_ACTIONS:-}" != "true" ] && command -v cargo-nextest >/dev/null 2>&1 ; then
12284
echo " nextest run"
12385
else
12486
echo " test"
@@ -129,18 +91,8 @@ _cargo-test := _cargo + ```
12991
## validator
13092
##
13193

132-
# Build validator code
133-
validator-build *flags:
134-
{{ _cargo }} build --workspace -p linkerd-network-validator \
135-
--target={{ _cargo-target }} \
136-
{{ if rs-build-type == "release" { "--release" } else { "" } }} \
137-
{{ flags }}
138-
139-
validator-package: rs-fetch validator-build
140-
@-mkdir -p target/package
141-
cp {{ _validator-bin }} target/package/{{ _validator-package-name }}
142-
{{ _strip }} target/package/{{ _validator-package-name }}
143-
{{ _shasum }} target/package/{{ _validator-package-name }} >target/package/{{ _validator-package-name }}.shasum
94+
validator *args:
95+
{{ just_executable() }} --justfile=validator/.justfile {{ args }}
14496

14597
##
14698
## proxy-init
@@ -172,7 +124,7 @@ proxy-init-image:
172124
docker buildx build . \
173125
--tag={{ _image }} \
174126
--platform={{ docker-arch }} \
175-
--load \
127+
--load
176128

177129
# Build docker image for iptables-tester (Development)
178130
proxy-init-test-image:
@@ -287,25 +239,3 @@ sh-lint:
287239
done < <(find . -type f ! \( -path ./.git/\* -or -path \*/target/\* \)) | xargs)
288240
echo "shellcheck $files" >&2
289241
shellcheck $files
290-
291-
# Prune Docker BuildKit cache (in CI)
292-
action-prune-docker:
293-
#!/usr/bin/env bash
294-
set -euxo pipefail
295-
# Delete all files under the buildkit blob directory that are not referred
296-
# to any longer in the cache manifest file
297-
manifest_sha=$(jq -r .manifests[0].digest < "$RUNNER_TEMP/.buildx-cache/index.json")
298-
manifest=${manifest_sha#"sha256:"}
299-
files=("$manifest")
300-
while IFS= read -r f; do
301-
files+=("$f")
302-
done < <(jq -r '.manifests[].digest | sub("^sha256:"; "")' < "$RUNNER_TEMP/.buildx-cache/blobs/sha256/$manifest")
303-
for file in "$RUNNER_TEMP"/.buildx-cache/blobs/sha256/*; do
304-
for name in "${files[@]}"; do
305-
if [[ "${file##*/}" == "$name" ]]; then
306-
rm -f "$file"
307-
echo "deleted: $name"
308-
break;
309-
fi
310-
done
311-
done

validator/.justfile

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# This justfile includes recipes for building and packaging the validator for
2+
# release. This file is separated so that we can invoke cargo, etc when building
3+
# defaults. If this logic were in the top-level justfile, then these tools would
4+
# be invoked (possibly updating Rust, etc), for unrelated targets.
5+
#
6+
# Users are expected to interact with this via the top-level Justfile.
7+
8+
# The version name to use for packages.
9+
version := env_var_or_default("VALIDATOR_VERSION", ```
10+
cd .. && cargo metadata --format-version=1 \
11+
| jq -r '.packages[] | select(.name == "linkerd-network-validator") | .version' \
12+
| head -n 1
13+
```)
14+
15+
# The architecture name to use for packages. Either 'amd64', 'arm64', or 'arm'.
16+
_arch := env_var_or_default("ARCH", "amd64")
17+
18+
# If an `_arch` is specified, then we change the default cargo `--target` to
19+
# support cross-compilation. Otherwise, we use `rustup` to find the default.
20+
_cargo-target := if _arch == "amd64" {
21+
"x86_64-unknown-linux-musl"
22+
} else if _arch == "arm64" {
23+
"aarch64-unknown-linux-musl"
24+
} else if _arch == "arm" {
25+
"armv7-unknown-linux-musleabihf"
26+
} else {
27+
`rustup show | sed -n 's/^Default host: \(.*\)/\1/p'`
28+
}
29+
30+
_target-dir := "../target" / _cargo-target / "release"
31+
_bin := _target-dir / "linkerd-network-validator"
32+
_package-name := "linkerd-network-validator-" + version + "-" + _arch
33+
_package-dir := "../target/package"
34+
_shasum := "shasum -a 256"
35+
36+
export RUST_BACKTRACE := env_var_or_default("RUST_BACKTRACE", "short")
37+
38+
# Support cross-compilation when `_arch` changes.
39+
_strip := if _arch == "arm64" { "aarch64-linux-gnu-strip" } else if _arch == "arm" { "arm-linux-gnueabihf-strip" } else { "strip" }
40+
41+
_cargo := env_var_or_default("CARGO", "cargo")
42+
43+
# If recipe is run in github actions (and cargo-action-fmt is installed), then add a
44+
# command suffix that formats errors
45+
_cargo-fmt := if env_var_or_default("GITHUB_ACTIONS", "") != "true" { "" } else {
46+
```
47+
if command -v cargo-action-fmt >/dev/null 2>&1 ; then
48+
echo "--message-format=json | cargo-action-fmt"
49+
fi
50+
```
51+
}
52+
53+
package: build
54+
@-mkdir -p {{ _package-dir }}
55+
cp {{ _bin }} {{ _package-dir / _package-name }}
56+
{{ _strip }} {{ _package-dir / _package-name }}
57+
{{ _shasum }} {{ _package-dir / _package-name }} > {{ _package-dir / _package-name }}.shasum
58+
59+
build *flags:
60+
{{ _cargo }} fetch --locked
61+
{{ _cargo }} build --workspace -p linkerd-network-validator \
62+
--release \
63+
--target={{ _cargo-target }} \
64+
{{ flags }} \
65+
{{ _cargo-fmt }}

0 commit comments

Comments
 (0)