From 089ef814be68b6c9b6f24093e0c8127162b36e77 Mon Sep 17 00:00:00 2001 From: Philip Niedertscheider Date: Sun, 29 Nov 2026 12:23:52 +0100 Subject: [PATCH] ci: Cache SwiftPM safely and remove duplicate release builds --- .github/actions/cache-swiftpm/action.yml | 58 ++++++++++++++++++++++++ .github/workflows/build-binaries.yml | 21 +++++++++ .github/workflows/build.yml | 31 ------------- .github/workflows/release.yml | 15 ++++++ .github/workflows/test.yml | 10 ++++ 5 files changed, 104 insertions(+), 31 deletions(-) create mode 100644 .github/actions/cache-swiftpm/action.yml delete mode 100644 .github/workflows/build.yml diff --git a/.github/actions/cache-swiftpm/action.yml b/.github/actions/cache-swiftpm/action.yml new file mode 100644 index 0000000..8861ba1 --- /dev/null +++ b/.github/actions/cache-swiftpm/action.yml @@ -0,0 +1,58 @@ +name: Cache SwiftPM +description: Cache exact build inputs and reusable dependency downloads separately. + +inputs: + build-variant: + description: Build configuration and target architecture or Swift SDK + required: true + cache-build: + description: Cache compiled output for builds without per-run generated inputs + default: "true" + +runs: + using: composite + steps: + - name: Fingerprint build environment + if: inputs.cache-build == 'true' + id: environment + shell: bash + run: | + FINGERPRINT="$RUNNER_TEMP/swiftpm-environment.txt" + swift --version > "$FINGERPRINT" + swift sdk list >> "$FINGERPRINT" + printf '%s\n' "$GITHUB_WORKSPACE" "$RUNNER_OS" "$RUNNER_ARCH" \ + "${ImageOS:-}" "${ImageVersion:-}" >> "$FINGERPRINT" + if [[ "$RUNNER_OS" == macOS ]]; then + sw_vers >> "$FINGERPRINT" + xcrun clang --version >> "$FINGERPRINT" + xcrun --sdk macosx --show-sdk-path >> "$FINGERPRINT" + xcrun --sdk macosx --show-sdk-build-version >> "$FINGERPRINT" + else + cat /etc/os-release >> "$FINGERPRINT" + clang --version >> "$FINGERPRINT" + ldd --version >> "$FINGERPRINT" + fi + HASH=$(shasum -a 256 "$FINGERPRINT" | cut -d ' ' -f 1) + echo "hash=$HASH" >> "$GITHUB_OUTPUT" + + # Build databases and compiled modules are reused only for identical inputs. + # In particular, generated release metadata must be written before this action. + - name: Cache exact SwiftPM build + if: inputs.cache-build == 'true' + id: build + uses: actions/cache@v6 + with: + path: .build + key: swiftpm-build-v1-${{ runner.os }}-${{ runner.arch }}-${{ steps.environment.outputs.hash }}-${{ inputs.build-variant }}-${{ hashFiles('Package.swift', 'Package.resolved', 'Sources/**', 'Tests/**', 'Makefile', '.github/actions/**', '.github/workflows/**') }} + + # SwiftPM still resolves and validates the locked dependency graph on a miss. + # Do not restore build databases or compiled modules from a partial match. + - name: Cache SwiftPM dependencies + if: steps.build.outputs.cache-hit != 'true' + uses: actions/cache@v6 + with: + path: | + .build/repositories + .build/checkouts + .build/artifacts + key: swiftpm-dependencies-v1-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('Package.swift', 'Package.resolved') }} diff --git a/.github/workflows/build-binaries.yml b/.github/workflows/build-binaries.yml index 3e987a5..9e63e2f 100644 --- a/.github/workflows/build-binaries.yml +++ b/.github/workflows/build-binaries.yml @@ -57,6 +57,13 @@ jobs: commit-sha: ${{ inputs.commit_sha }} build-date: ${{ inputs.build_date }} + - name: Cache SwiftPM + uses: ./.github/actions/cache-swiftpm + with: + build-variant: darwin-${{ matrix.architecture }}-release + # Release metadata changes every run, so exact build caches cannot be reused. + cache-build: "false" + - name: Build CLI id: build run: | @@ -127,7 +134,15 @@ jobs: with: swift-version: "6.4.0" + - name: Cache Static Linux SDK + id: linux-sdk + uses: actions/cache@v6 + with: + path: ~/.swiftpm/swift-sdks + key: swift-static-sdk-v1-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('Makefile', '.github/actions/setup-swift/**') }} + - name: Install Static Linux SDK + if: steps.linux-sdk.outputs.cache-hit != 'true' run: make install-linux-sdk - name: Generate build metadata @@ -137,6 +152,12 @@ jobs: commit-sha: ${{ inputs.commit_sha }} build-date: ${{ inputs.build_date }} + - name: Cache SwiftPM + uses: ./.github/actions/cache-swiftpm + with: + build-variant: ${{ matrix.swift_sdk }}-release + cache-build: "false" + - name: Build CLI with debug information id: build-linux run: | diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 5f0cb2f..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,31 +0,0 @@ -name: Build - -on: - push: - branches: [main] - pull_request: - workflow_dispatch: - -permissions: - contents: read - -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true - -jobs: - build: - name: Build - runs-on: macos-latest - timeout-minutes: 10 - steps: - - name: Checkout - uses: actions/checkout@v7 - - - name: Setup Swift - uses: ./.github/actions/setup-swift - with: - swift-version: "6.4.0" - - - name: Make Build - run: make build diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index edccba2..50bc35d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,6 +12,10 @@ on: permissions: contents: write +concurrency: + group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + jobs: prepare: name: Prepare Release @@ -88,6 +92,17 @@ jobs: build_date: ${{ needs.prepare.outputs.build_date }} secrets: inherit + build-required-check: + name: Build + needs: build-cli-binaries + if: always() + runs-on: ubuntu-latest + steps: + - name: Check build result + env: + BUILD_RESULT: ${{ needs.build-cli-binaries.result }} + run: test "$BUILD_RESULT" = success + release: name: Create Release needs: [prepare, build-cli-binaries] diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ff594c4..aaeeaa3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -27,6 +27,11 @@ jobs: with: swift-version: "6.4.0" + - name: Cache SwiftPM + uses: ./.github/actions/cache-swiftpm + with: + build-variant: macos-native-debug + - name: Make Test run: make test @@ -43,5 +48,10 @@ jobs: with: swift-version: "6.4.0" + - name: Cache SwiftPM + uses: ./.github/actions/cache-swiftpm + with: + build-variant: linux-native-debug + - name: Make Test run: make test