From ca4c8e620677391ebd06bc8bcdab58364c610f08 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Mon, 30 Nov 2026 19:43:36 +0100 Subject: [PATCH] CI: public release installs shared platforms once, libraries in parallel Changelog: - Co-Authored-By: Claude Opus 5.5 --- .github/workflows/pub-release.yml | 113 +++++++++++++++++++----------- 1 file changed, 73 insertions(+), 40 deletions(-) diff --git a/.github/workflows/pub-release.yml b/.github/workflows/pub-release.yml index 44adaa8b..ddc1c61e 100644 --- a/.github/workflows/pub-release.yml +++ b/.github/workflows/pub-release.yml @@ -231,61 +231,94 @@ jobs: # PlatformIO appends PLATFORMIO_BUILD_FLAGS after each env's flags: # last -D wins. arts=() + declare -A spec=() add() { if [ -n "$ONLY" ] && [[ " $ONLY " != *" $1$3 "* ]]; then return 0; fi - arts+=("$1|$2|$3|${4:-}") + arts+=("$1$3") + spec[$1$3]="$1|$2|$3|${4:-}" } for env in $ENVS; do add "$env" "" ""; done for env in $OBS_ENVS; do add "$env" "$OBS_FLAGS" "-observer"; done for env in $OTA_ENVS; do add "$env" "-D FW_OTA=1" "-ota" ota; done for env in $OBS_ENVS; do add "$env" "$OBS_FLAGS -D FW_OTA=1" "-observer-ota" observer-ota; done - # 1) compile PARALLEL artifacts at a time, each into its own build dir - # (one env comes in up to four flag sets). One by one this took 38 - # of the run's 40 minutes on a 24-core runner that sat mostly idle. - # Platforms, tools and libraries are installed first, one env at a - # time: concurrent first installs would race in ~/.platformio and - # .pio/libdeps. + # Builds run PARALLEL at a time: one by one they took 38 of the run's + # 40 minutes on a 24-core runner that sat mostly idle. bdir="$RUNNER_TEMP/build" logs="$RUNNER_TEMP/logs" mkdir -p "$bdir" "$logs" - for env in $(printf '%s\n' "${arts[@]}" | cut -d'|' -f1 | sort -u); do - pio pkg install -e "$env" >"$logs/install-$env.log" 2>&1 || - { cat "$logs/install-$env.log"; echo "::error::$env: pio pkg install failed"; exit 1; } - done - compile() { # , in a subshell - export FW_OTA_VARIANT=$4 PLATFORMIO_BUILD_DIR="$bdir/$1$3" PLATFORMIO_BUILD_FLAGS="$PUB_FLAGS $2" - pio run -j "$PIO_JOBS" -e "$1" || return 1 - [ -f "$bdir/$1$3/$1/firmware.zip" ] || pio run -j "$PIO_JOBS" -t mergebin -e "$1" # ESP32 + # par ...: ` ` PARALLEL at a time, + # output to $logs/.log, exit code to $logs/.rc + par() { + local fn=$1 n running=0 + shift + for n in "$@"; do + if [ "$running" -ge "$PARALLEL" ]; then wait -n || :; running=$((running - 1)); fi + (set +e; "$fn" "$n"; echo $? >"$logs/$n.rc") >"$logs/$n.log" 2>&1 & + running=$((running + 1)) + done + wait } - t0=$SECONDS running=0 - for a in "${arts[@]}"; do - IFS='|' read -r env flags suffix variant <<<"$a" - if [ "$running" -ge "$PARALLEL" ]; then wait -n || :; running=$((running - 1)); fi - (set +e; compile "$env" "$flags" "$suffix" "$variant"; echo $? >"$logs/$env$suffix.rc") >"$logs/$env$suffix.log" 2>&1 & - running=$((running + 1)) - done - wait - echo "compiled ${#arts[@]} artifacts in $((SECONDS - t0)) s ($PARALLEL at a time)" - failed=() - for a in "${arts[@]}"; do - IFS='|' read -r env flags suffix variant <<<"$a" - rc=$(cat "$logs/$env$suffix.rc" 2>/dev/null || echo "?") - echo "::group::$env$suffix (rc $rc)" - cat "$logs/$env$suffix.log" - echo "::endgroup::" - [ "$rc" = 0 ] || failed+=("$env$suffix") - done - if [ ${#failed[@]} -gt 0 ]; then - for f in "${failed[@]}"; do echo "== $f"; tail -n 30 "$logs/$f.log"; done - echo "::error::build failed: ${failed[*]}" + # report <name>...: each log as a group, in this order; fails + # the step, with the tails of the failed ones, if any failed + report() { + local title=$1 n rc failed=() + shift + for n in "$@"; do + rc=$(cat "$logs/$n.rc" 2>/dev/null || echo "?") + echo "::group::$n (rc $rc)" + cat "$logs/$n.log" + echo "::endgroup::" + [ "$rc" = 0 ] || failed+=("$n") + done + [ ${#failed[@]} -eq 0 ] && return 0 + for n in "${failed[@]}"; do echo "== $n"; tail -n 30 "$logs/$n.log"; done + echo "::error::$title failed: ${failed[*]}" exit 1 - fi + } + # 1) install. Nothing is cached here (reproducible), so each run + # downloads the platforms, toolchains and libraries: 8 minutes when + # done env by env. One env per distinct platform (+ platform_packages) + # installs those first, one at a time -- they are shared, concurrent + # installs would race in ~/.platformio. The other envs then only add + # their libraries, each into its own .pio/libdeps/<env>: in parallel. + envs=$(printf '%s\n' "${arts[@]}" | while read -r a; do echo "${spec[$a]%%|*}"; done | sort -u) + mapfile -t first < <(pio project config --json-output | python3 -c ' + import json, sys + want, seen = set(sys.argv[1].split()), set() + for name, opts in json.load(sys.stdin): + o = dict(opts) + key = (o.get("platform"), str(o.get("platform_packages"))) + if name[4:] in want and name.startswith("env:") and key not in seen: + seen.add(key) + print(name[4:])' "$envs") + install() { pio pkg install -e "${1#install-}"; } + t0=$SECONDS + for env in "${first[@]}"; do par install "install-$env"; done + report "install" "${first[@]/#/install-}" + rest=() + for env in $envs; do [[ " ${first[*]} " == *" $env "* ]] || rest+=("install-$env"); done + par install "${rest[@]}" + report "install" "${rest[@]}" + echo "installed $(wc -w <<<"$envs") envs (${#first[@]} platforms first) in $((SECONDS - t0)) s" + # 2) compile, each artifact into its own build dir: one env comes in + # up to four flag sets + compile() { + local env flags suffix variant + IFS='|' read -r env flags suffix variant <<<"${spec[$1]}" + export FW_OTA_VARIANT=$variant PLATFORMIO_BUILD_DIR="$bdir/$1" PLATFORMIO_BUILD_FLAGS="$PUB_FLAGS $flags" + pio run -j "$PIO_JOBS" -e "$env" || return 1 + [ -f "$bdir/$1/$env/firmware.zip" ] || pio run -j "$PIO_JOBS" -t mergebin -e "$env" # ESP32 + } + t1=$SECONDS + par compile "${arts[@]}" + echo "compiled ${#arts[@]} artifacts in $((SECONDS - t1)) s ($PARALLEL at a time)" + report "build" "${arts[@]}" # 2) package in order: manifest.json, deltas and the version are # written one artifact at a time. The OTA tools read .pio/build/<env>, # so that points at the artifact being packaged. mkdir -p .pio/build for a in "${arts[@]}"; do - IFS='|' read -r env flags suffix variant <<<"$a" - b="$bdir/$env$suffix/$env" + IFS='|' read -r env flags suffix variant <<<"${spec[$a]}" + b="$bdir/$a/$env" echo "::group::package $env$suffix" if [ -f "$b/firmware.zip" ]; then # nRF52 python3 bin/uf2conv/uf2conv.py "$b/firmware.hex" -c -o "$b/firmware.uf2" -f 0xADA52840 @@ -306,7 +339,7 @@ jobs: fi echo "::endgroup::" done - echo "built and packaged in $((SECONDS - t0)) s" + echo "installed, built and packaged in $((SECONDS - t0)) s" jq --arg c "$(git rev-parse HEAD)" --arg t "$TAG" '.commit = $c | .tag = $t' \ "$dist/manifest.json" >"$dist/m.tmp" && mv "$dist/m.tmp" "$dist/manifest.json" cd "$dist"