Building and testing Swift

You can create a continuous integration (CI) workflow to build and test your Swift project.

Introduction

This guide shows you how to build and test a Swift package.

To build and test your Swift project on GitHub AE, the necessary Swift dependencies are required. You must install the required software on your self-hosted runners. For more information about self-hosted runners, see "Hosting your own runners."

Prerequisites

You should already be familiar with YAML syntax and how it's used with GitHub Actions. For more information, see "Workflow syntax for GitHub Actions."

We recommend that you have a basic understanding of Swift packages. For more information, see "Swift Packages" in the Apple developer documentation.

Starting with the Swift workflow template

GitHub provides a Swift workflow template that should work for most Swift projects, and this guide includes examples that show you how to customize this template. For more information, see the Swift workflow template.

To get started quickly, add the template to the .github/workflows directory of your repository.

YAML
name: Swift

on: [push]

jobs:
  build:

    runs-on: macos-latest

    steps:
      - uses: actions/checkout@v2
      - name: Build
        run: swift build
      - name: Run tests
        run: swift test

Specifying a Swift version

To use a specific preinstalled version of Swift on a GitHub-hosted runner, use the fwal/setup-swift action. This action finds a specific version of Swift from the tools cache on the runner and adds the necessary binaries to PATH. These changes will persist for the remainder of a job. For more information, see the fwal/setup-swift action.

If you are using a self-hosted runner, you must install your desired Swift versions and add them to PATH.

The examples below demonstrate using the fwal/setup-swift action.

Using multiple Swift versions

You can configure your job to use a multiple versions of Swift in a build matrix.

YAML
# This workflow uses actions that are not certified by GitHub.
# São fornecidas por terceiros e regidas por
# termos de serviço, política de privacidade e suporte separados
# documentação.

name: Swift

on: [push]

jobs:
  build:
    name: Swift ${{ matrix.swift }} on ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
        swift: ["5.2", "5.3"]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: fwal/setup-swift@d43a564349d1341cd990cfbd70d94d63b8899475
        with:
          swift-version: ${{ matrix.swift }}
      - uses: actions/checkout@
      - name: Build
        run: swift build
      - name: Run tests
        run: swift test

Using a single specific Swift version

You can configure your job to use a single specific version of Swift, such as 5.3.3.

YAML
steps:
  - uses: fwal/setup-swift@d43a564349d1341cd990cfbd70d94d63b8899475
    with:
      swift-version: "5.3.3"
  - name: Get swift version
    run: swift --version # Swift 5.3.3

Building and testing your code

You can use the same commands that you use locally to build and test your code using Swift. This example demonstrates how to use swift build and swift test in a job:

YAML
steps:
  - uses: actions/checkout@v2
  - uses: fwal/setup-swift@d43a564349d1341cd990cfbd70d94d63b8899475
    with:
      swift-version: "5.3.3"
  - name: Build
    run: swift build
  - name: Run tests
    run: swift test

Esse documento ajudou você?

Política de Privacidade

Ajude-nos a tornar esses documentos ótimos!

Todos os documentos do GitHub são de código aberto. Você percebeu que algo que está errado ou não está claro? Envie um pull request.

Faça uma contribuição

Ou, aprenda como contribuir.