# Build and Test Swift Packages in CI on Mac

Canonical: https://figment.so/blog/mac-swift-package-ci-build

For a pure Swift package, run swift build and swift test --parallel --xunit-output on a Mac runner, cache .build keyed on Package.resolved, and add a release build. The Intel Mac VM ($25 a month) works when Xcode 26.3 or earlier is enough.

**For a pure Swift package, run `swift build` and `swift test --parallel --xunit-output` on a Mac runner, cache `.build` keyed on `Package.resolved`, and add a release build as a second matrix leg.** With no app bundle and no storyboards, `swift build` and `swift test` give a tighter feedback loop than `xcodebuild`. They read `Package.swift` directly and can emit machine-readable test output, as the [Swift Package Manager documentation](https://www.swift.org/documentation/package-manager/) describes. Running the same tests on Linux in the same workflow catches platform-specific bugs early.

## Should I use swift build or xcodebuild in CI?

Use `swift build` for a library with no `.xcodeproj` and no Xcode-specific build settings. It matches Linux (`xcodebuild` is macOS-only) and starts faster without the `xcbuild` layer. Use `xcodebuild` when there is an app target, extension or asset catalog, when you need code signing, entitlements or `.ipa` export, or when you build against an Xcode-managed SDK or UI test plan. One repository can use both: `swift build` for the library and `xcodebuild` for a host app or UI tests.

## How do I pin the Swift toolchain version?

Each Xcode version ships one Swift toolchain, and `xcode-select` chooses which installed Xcode `swift` resolves to:

```bash
# List installed Xcode versions
ls /Applications | grep -i '^Xcode'

# Point the toolchain at a specific installation
sudo xcode-select -s /Applications/Xcode_16.3.app

# Confirm before anything else runs
xcodebuild -version
swift --version
```

Make the selection a named step in the workflow so the active version appears in the log and is easy to bump. The Swift version shipped with each Xcode release is listed in Apple's Xcode release notes; verify the Xcode and macOS pair against [Apple's Xcode release notes](https://developer.apple.com/documentation/xcode-release-notes) before you pin. On an Intel Mac VM, keep to Xcode 26.3 or earlier (see [Apple's porting guidance](https://developer.apple.com/documentation/apple-silicon/porting-your-macos-apps-to-apple-silicon)). Newer Xcode, arm64-only dependencies and ARM-only toolchains need an Apple silicon host.

## How do I run swift test in parallel with XUnit output?

Pass `--parallel` and `--xunit-output`. `--parallel` distributes test cases across cores. Historically, SwiftPM only wrote `--xunit-output` when `--parallel` was set for XCTest suites ([swiftlang/swift-package-manager#7065](https://github.com/swiftlang/swift-package-manager/issues/7065)), so keep `--parallel` in the command to stay on the supported path. When the package also contains Swift Testing suites, SwiftPM writes a second XML file next to the XCTest one, with the `-swift-testing.xml` suffix.

```bash
mkdir -p test-results

swift test \
  --parallel \
  --xunit-output test-results/results.xml
```

After the run, expect:
- `test-results/results.xml` for XCTest cases
- `test-results/results-swift-testing.xml` if Swift Testing suites exist

Upload the directory so the summary step picks up both files.

## How do I cache the .build directory?

Restore `.build` between runs so unchanged dependencies are not recompiled. SwiftPM keeps its incremental build products there. A good cache key hashes `Package.resolved` (which pins every resolved dependency) and partitions by configuration and runner OS:

```yaml
- uses: actions/cache@v4
  with:
    path: .build
    key: "${{ runner.os }}-spm-${{ matrix.configuration }}-${{ hashFiles('Package.resolved') }}"
    restore-keys: |
      ${{ runner.os }}-spm-${{ matrix.configuration }}-
```

When a dependency bump changes `Package.resolved`, the primary key misses and the restore key falls back to the newest partial match, so unchanged modules still skip recompilation.

## Why add a release build?

Some bugs only appear with `-O`, so add a release build as a second matrix leg:

```bash
swift build -c release
```

This compiles only; it does not run tests. If the package relies on `@inlinable` or specialization, release-mode tests (`swift test -c release`) may be worth the extra minutes, but debug tests plus a release build cover most library projects.

## What does the full GitHub Actions workflow look like?

```yaml
name: Swift Package CI

on:
  push:
    branches: [main]
  pull_request:

jobs:
  build-and-test:
    runs-on: [self-hosted, macOS, figment]
    strategy:
      fail-fast: false
      matrix:
        configuration: [debug, release]

    steps:
      - uses: actions/checkout@v4

      - name: Select Xcode
        run: |
          sudo xcode-select -s /Applications/Xcode.app
          swift --version

      - name: Cache .build
        uses: actions/cache@v4
        with:
          path: .build
          key: "${{ runner.os }}-spm-${{ matrix.configuration }}-${{ hashFiles('Package.resolved') }}"
          restore-keys: |
            ${{ runner.os }}-spm-${{ matrix.configuration }}-

      - name: Build (${{ matrix.configuration }})
        run: swift build -c ${{ matrix.configuration }}

      - name: Test (debug only)
        if: matrix.configuration == 'debug'
        run: |
          mkdir -p test-results
          swift test \
            --parallel \
            --xunit-output test-results/results.xml

      - name: Upload test results
        if: always() && matrix.configuration == 'debug'
        uses: actions/upload-artifact@v4
        with:
          name: swift-test-results
          path: test-results/
          retention-days: 30
```

`actions/upload-artifact@v4` returns a SHA-256 `digest` output that the download action re-verifies, per the [GitHub Actions artifact documentation](https://docs.github.com/en/actions/using-workflows/storing-workflow-data-as-artifacts). Treat it as the integrity check rather than writing your own checksum step. `retention-days` is capped by the repository, organization or enterprise retention limit, and the default is inherited from that setting. This example is illustrative, not a benchmark. It has not been timed, and the failing steps use `set -e` semantics so a failed build cannot be reported as a fast run.

## How do I test on macOS and Linux in the same pipeline?

Add a Linux job in parallel, since Swift packages often target both platforms:

```yaml
  test-linux:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Test on Linux
        run: |
          mkdir -p test-results
          swift test \
            --parallel \
            --xunit-output test-results/linux-results.xml
      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: swift-test-results-linux
          path: test-results/
```

If a test passes on macOS but fails on Linux (or the reverse), the matrix shows it on the pull request without a second workflow file.

## Which Figment Mac plan fits a Swift package?

| Scenario | Figment plan | Price |
| --- | --- | --- |
| Pure Swift library, Xcode 26.3 or earlier, no arm64-only dependencies | [Intel Mac VM](/mac-hosting#intel-vm), macOS Sequoia, 4 shared vCPUs, 4GB RAM, 80GB SSD | $0.15 an hour, $2 a day, $25 a month |
| Current Xcode or arm64-only dependencies | [Apple Silicon Mac VM](/mac-hosting#apple-silicon-vm), 4 shared vCPUs, 4GB RAM, 65GB SSD | $0.40 an hour, $6 a day, $39 a month |
| Large package graphs or parallel runners | [Dedicated M4 Pro 48 GB](/mac-hosting#dedicated-m4-pro), whole Mac, M4 Pro, 48GB RAM, 512GB SSD | $349 a month ($299 on a 12-month term) |

If the package compiles cleanly under Xcode 26.3 and its tests pass, the Intel VM is a legitimate low-cost host. It cannot run arm64-only binaries or visionOS. Check the macOS version shown before purchase against Apple's Xcode table. Figment runs the Mac; you manage the runner, Xcode and CI configuration, and we do not offer managed CI.

**Related guides**
- [Notarize a macOS App Inside a CI Pipeline](/blog/mac-notarize-app-ci-pipeline)
- [Self-Hosted Mac Runner for CircleCI Pipelines](/blog/mac-circleci-self-hosted-runner)
- [Xcode on Windows: Remote Mac Setup](/blog/mac-xcode-on-windows-remote-setup)
