Skip to content
play

GitHub Action

Setup Swift environment for macOS, Linux and Windows

v1.7.0 Latest version

Setup Swift environment for macOS, Linux and Windows

play

Setup Swift environment for macOS, Linux and Windows

Setup Swift environment by optionally downloading and adding it to the PATH

Installation

Copy and paste the following snippet into your .yml file.

              

- name: Setup Swift environment for macOS, Linux and Windows

uses: SwiftyLab/setup-swift@v1.7.0

Learn more about this action in SwiftyLab/setup-swift

Choose a version

Setup Swift

GitHub Action Supports macOS, Linux & Windows CI/CD CodeFactor codecov Get it from Marketplace

GitHub Action that will setup Swift environment with specified version. This action supports the following functionalities:

  • Works on Linux, macOS and Windows.
  • Supports installing latest major/minor/patch.
  • Provides snapshots as soon as published in swift.org.
  • Verifies toolchain snapshots before installation (gpg for Linux and Windows, pkgutil for macOS) .
  • Allows development snapshots by enabling development flag and optional version.
  • Prefers existing Xcode installations.
  • Caches installed setup in tool cache and actions cache(Swift 5.10 and after does not support caching on Windows).
  • Allows fetching snapshot metadata without installation (can be used to setup docker images).

Latest supported toolchains

Release Type Latest Available
Stable Latest Release
Latest Release Tag
Development Latest Development Snapshot
Latest Development Snapshot Tag
Latest Development Snapshot Date
Trunk Development Latest Trunk Development Snapshot Tag
Latest Trunk Development Snapshot Date

Usage

To run the action with the latest stable release swift version available, simply add the action as a step in your workflow:

- uses: SwiftyLab/setup-swift@latest

Or use the latest development snapshots by enabling the development flag:

- uses: SwiftyLab/setup-swift@latest
  with:
    development: true

After the environment is configured you can run swift and xcode commands using the standard run step:

- uses: SwiftyLab/setup-swift@latest
- name: Get swift version
  run: swift --version
- name: Get swift version in macOS
  if: runner.os == 'macOS'
  run: xcrun --toolchain ${{ env.TOOLCHAINS }} swift --version

A specific Swift version can be set using the swift-version input, see the format:

- uses: SwiftyLab/setup-swift@latest
  with:
    swift-version: "5.1.0"
- name: Get swift version
  run: swift --version # Swift 5.1.0
- name: Get swift version in macOS
  if: runner.os == 'macOS'
  run: xcrun --toolchain ${{ env.TOOLCHAINS }} swift --version # Swift 5.1.0

Works perfect together with job matrixes:

name: Swift ${{ matrix.swift }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
  matrix:
    os: [ubuntu-latest, macos-latest]
    swift: ["5.4.3", "5.2.4"]
steps:
- uses: SwiftyLab/setup-swift@latest
  with:
    swift-version: ${{ matrix.swift }}
- name: Get swift version
  run: swift --version

See action.yml for complete list of inputs and outputs, see wiki for inner workings of this action.

Specifying version

This project uses strict semantic versioning to determine what version of Swift to configure. This differs slightly from the official convention used by Swift.

For example, Swift is available as version 5.1 but using this as value for swift-version will be interpreted as a version range of 5.1.X where X is the latest patch version available for that major and minor version.

In other words specifying...

  • "5.1" will resolve to latest patch version (aka 5.1.5)
  • "5.1.0" will resolve to version 5.1
  • "4" will resolve to latest minor and patch version (aka 4.2.4)
  • "4.0.0" will resolve to version 4.0
Additionally, to use custom toolchains, download URL can be provided. The download URL must point to a `tar` archive for `Linux`, `pkg` file for `macOS` and `exe` file for `Windows`.

i.e. for macOS: https://github.com/swiftwasm/swift/releases/download/swift-wasm-5.10-SNAPSHOT-2024-03-30-a/swift-wasm-5.10-SNAPSHOT-2024-03-30-a-macos_x86_64.pkg for Linux: https://github.com/swiftwasm/swift/releases/download/swift-wasm-5.10-SNAPSHOT-2024-03-30-a/swift-wasm-5.10-SNAPSHOT-2024-03-30-a-ubuntu22.04_x86_64.tar.gz

[!IMPORTANT] When using custom toolchains, please ensure that the toolchain can be installed and used on the GitHub runner, this action won't be able to validate this for custom toolchains.

Caveats

YAML interprets eg. 4.0 as a float, this action will then interpret that as 4 which will result in eg. Swift 4.2.4 being resolved. Quote your inputs! Thus surround version input with quotations:

- uses: SwiftyLab/setup-swift@v1
  with:
    swift-version: '5.0'

Not:

- uses: SwiftyLab/setup-swift@v1
  with:
    swift-version: 5.0

Keeping the action up-to-date

You have two options for keeping this action up-to-date: either use the latest tag to always have the latest changes or define a specific version (like v1.0.0).

Latest version tag (Recommended)

You can use the latest tag to always have the latest changes. This action is updated automatically as soon as new toolchain snapshots are published in swift.org, by using the latest tag you have access to these data automagically.

Specific version

You can the specific version tag together with Dependabot to keep the action up-to-date. You will automatically get notifed when the action updates and you can read the changelog directly in the PR opened by dependabot.

License

setup-swift is released under the MIT license. See LICENSE for details. The Swift logo is a trademark of Apple Inc.