diff --git a/image/ci-runner-idle-exit b/image/ci-runner-idle-exit index bef408f..06b34d3 100755 --- a/image/ci-runner-idle-exit +++ b/image/ci-runner-idle-exit @@ -13,6 +13,15 @@ # IDLE_GRACE_SECONDS (default 30, as docker stop gives an EC2 runner): the # runner's helper and listener sit in their own process group, and a TERM # alone doesn't end them. +# +# With CI_RUNNER_VCPUS (the task's vCPUs, set by the launcher), it first writes +# /home/runner/.cargo/config.toml with `[build] jobs = `: nproc over-reports +# in a task, and a CARGO_BUILD_JOBS in the environment would change +# Swatinem/rust-cache's key (it hashes every CARGO*, CC*, CFLAGS*, CXX*, CMAKE* +# and RUST* variable), so a Fargate job would miss the cache an EC2 job saved. +# Cargo reads .cargo/config.toml in every ancestor of its working directory, +# so the file reaches every checkout under /home/runner/_work, whatever +# CARGO_HOME a job sets. A file already there is left alone. set -u idle=${IDLE_SECONDS:-900} @@ -26,6 +35,21 @@ if [[ ! $grace =~ ^[0-9]+$ ]]; then exit 2 fi +cargo_jobs() { + local n=${CI_RUNNER_VCPUS:-} dir=/home/runner/.cargo + [[ -n $n ]] || return 0 + if [[ ! $n =~ ^[1-9][0-9]*$ ]]; then + echo "ci-runner: CI_RUNNER_VCPUS must be a positive integer, not '$n'; no cargo jobs set" >&2 + elif [[ -e $dir/config.toml ]]; then + echo "ci-runner: $dir/config.toml exists, leaving it; cargo's jobs not set to $n" >&2 + elif mkdir -p "$dir" && printf '[build]\njobs = %s\n' "$n" > "$dir/config.toml"; then + echo "ci-runner: cargo jobs = $n ($dir/config.toml)" + else + echo "ci-runner: couldn't write $dir/config.toml; cargo's jobs not set" >&2 + fi +} +cargo_jobs + log=$(mktemp /tmp/ci-runner-idle-exit.XXXXXX) # Its own session (its id is $pid), so a stop reaches run.sh, its helper and # the listener, whatever their process groups. diff --git a/image/smoke.sh b/image/smoke.sh index d723c7c..428b382 100755 --- a/image/smoke.sh +++ b/image/smoke.sh @@ -100,5 +100,25 @@ expect idle-exit-keeps "3 finished" "$rc $(grep -x finished <<<"$out")" out=$(IDLE_SECONDS=0 timeout 10 "$idle" true 2>&1) expect idle-exit-refuses "2" "$?" +echo "== cargo jobs" +# With CI_RUNNER_VCPUS the entrypoint caps cargo's jobs in a config file, not +# in a CARGO_* variable rust-cache would hash. cargo isn't in the image, so +# the file is checked, not a build. +cfg=/home/runner/.cargo/config.toml +rm -f "$cfg" +IDLE_SECONDS=2 timeout 10 "$idle" true >/dev/null 2>&1 +expect cargo-none no "$([[ -e $cfg ]] && echo yes || echo no)" +out=$(CI_RUNNER_VCPUS=4 IDLE_SECONDS=2 timeout 10 "$idle" true 2>&1) +expect cargo-config '[build] jobs = 4' "$(paste -sd' ' "$cfg" 2>&1)" +expect cargo-owner runner "$(stat -c %U "$cfg" 2>&1)" +expect cargo-says yes "$(grep -q 'cargo jobs = 4' <<<"$out" && echo yes || echo no)" +printf '[net]\ngit-fetch-with-cli = true\n' > "$cfg" +out=$(CI_RUNNER_VCPUS=8 IDLE_SECONDS=2 timeout 10 "$idle" true 2>&1) +expect cargo-keeps '[net] git-fetch-with-cli = true' "$(paste -sd' ' "$cfg" 2>&1)" +expect cargo-keeps-says yes "$(grep -q 'exists, leaving it' <<<"$out" && echo yes || echo no)" +rm -f "$cfg" +out=$(CI_RUNNER_VCPUS=four IDLE_SECONDS=2 timeout 10 "$idle" true 2>&1) +expect cargo-bad-value "0 no" "$? $([[ -e $cfg ]] && echo yes || echo no)" + if (( fail )); then echo "SMOKE FAILED"; exit 1; fi echo "SMOKE PASSED"