|
| 1 | +--- |
| 2 | +title: Building and testing Swift |
| 3 | +intro: You can create a continuous integration (CI) workflow to build and test your Swift project. |
| 4 | +product: '{% data reusables.gated-features.actions %}' |
| 5 | +versions: |
| 6 | + free-pro-team: '*' |
| 7 | + enterprise-server: '>=2.22' |
| 8 | + github-ae: '*' |
| 9 | +type: 'tutorial' |
| 10 | +topics: |
| 11 | + - 'CI' |
| 12 | + - 'Swift' |
| 13 | +--- |
| 14 | + |
| 15 | +{% data reusables.actions.enterprise-beta %} |
| 16 | +{% data reusables.actions.enterprise-github-hosted-runners %} |
| 17 | +{% data reusables.actions.ae-beta %} |
| 18 | + |
| 19 | +### Introduction |
| 20 | + |
| 21 | +This guide shows you how to build and test a Swift package. |
| 22 | + |
| 23 | +{% if currentVersion == "github-ae@latest" %} To build and test your Swift project on {% data variables.product.prodname_ghe_managed %}, you will need to create a custom operating system image that includes the necessary Swift dependencies. For instructions on how to make sure your {% data variables.actions.hosted_runner %} has the required software installed, see "[Creating custom images](/actions/using-github-hosted-runners/creating-custom-images)." |
| 24 | +{% else %}{% data variables.product.prodname_dotcom %}-hosted runners have a tools cache with preinstalled software, and the Ubuntu and macOS runners include the dependencies for building Swift packages. For a full list of up-to-date software and the preinstalled versions of Swift and Xcode, see "[About GitHub-hosted runners](/actions/using-github-hosted-runners/about-github-hosted-runners#supported-software)."{% endif %} |
| 25 | + |
| 26 | +### Prerequisites |
| 27 | + |
| 28 | +You should already be familiar with YAML syntax and how it's used with {% data variables.product.prodname_actions %}. For more information, see "[Workflow syntax for {% data variables.product.prodname_actions %}](/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions)." |
| 29 | + |
| 30 | +We recommend that you have a basic understanding of Swift packages. For more information, see "[Swift Packages](https://developer.apple.com/documentation/swift_packages)" in the Apple developer documentation. |
| 31 | + |
| 32 | +### Starting with the Swift workflow template |
| 33 | + |
| 34 | +{% data variables.product.prodname_dotcom %} 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](https://github.com/actions/starter-workflows/blob/main/ci/swift.yml). |
| 35 | + |
| 36 | +To get started quickly, add the template to the `.github/workflows` directory of your repository. |
| 37 | + |
| 38 | +{% raw %} |
| 39 | +```yaml{:copy} |
| 40 | +name: Swift |
| 41 | +
|
| 42 | +on: [push] |
| 43 | +
|
| 44 | +jobs: |
| 45 | + build: |
| 46 | +
|
| 47 | + runs-on: macos-latest |
| 48 | +
|
| 49 | + steps: |
| 50 | + - uses: actions/checkout@v2 |
| 51 | + - name: Build |
| 52 | + run: swift build |
| 53 | + - name: Run tests |
| 54 | + run: swift test |
| 55 | +``` |
| 56 | +{% endraw %} |
| 57 | + |
| 58 | +### Specifying a Swift version |
| 59 | + |
| 60 | +To use a specific preinstalled version of Swift on a {% data variables.product.prodname_dotcom %}-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`](https://github.com/marketplace/actions/setup-swift) action. |
| 61 | + |
| 62 | +If you are using a self-hosted runner, you must install your desired Swift versions and add them to `PATH`. |
| 63 | + |
| 64 | +The examples below demonstrate using the `fwal/setup-swift` action. |
| 65 | + |
| 66 | +#### Using multiple Swift versions |
| 67 | + |
| 68 | +You can configure your job to use a multiple versions of Swift in a build matrix. |
| 69 | + |
| 70 | +{% raw %} |
| 71 | +```yaml{:copy} |
| 72 | +name: Swift |
| 73 | +
|
| 74 | +on: [push] |
| 75 | +
|
| 76 | +jobs: |
| 77 | + build: |
| 78 | + name: Swift ${{ matrix.swift }} on ${{ matrix.os }} |
| 79 | + strategy: |
| 80 | + matrix: |
| 81 | + os: [ubuntu-latest, macos-latest] |
| 82 | + swift: ["5.2", "5.3"] |
| 83 | + runs-on: ${{ matrix.os }} |
| 84 | + steps: |
| 85 | + - uses: fwal/setup-swift@v1 |
| 86 | + with: |
| 87 | + swift-version: ${{ matrix.swift }} |
| 88 | + - uses: actions/checkout@v2 |
| 89 | + - name: Build |
| 90 | + run: swift build |
| 91 | + - name: Run tests |
| 92 | + run: swift test |
| 93 | +``` |
| 94 | +{% endraw %} |
| 95 | + |
| 96 | +#### Using a single specific Swift version |
| 97 | + |
| 98 | +You can configure your job to use a single specific version of Swift, such as `5.3.3`. |
| 99 | + |
| 100 | +{% raw %} |
| 101 | +```yaml{:copy} |
| 102 | +steps: |
| 103 | + - uses: fwal/setup-swift@v1 |
| 104 | + with: |
| 105 | + swift-version: "5.3.3" |
| 106 | + - name: Get swift version |
| 107 | + run: swift --version # Swift 5.3.3 |
| 108 | +``` |
| 109 | +{% endraw %} |
| 110 | + |
| 111 | +### Building and testing your code |
| 112 | + |
| 113 | +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: |
| 114 | + |
| 115 | +{% raw %} |
| 116 | +```yaml{:copy} |
| 117 | +steps: |
| 118 | + - uses: actions/checkout@v2 |
| 119 | + - uses: fwal/setup-swift@v1 |
| 120 | + with: |
| 121 | + swift-version: "5.3.3" |
| 122 | + - name: Build |
| 123 | + run: swift build |
| 124 | + - name: Run tests |
| 125 | + run: swift test |
| 126 | +``` |
| 127 | +{% endraw %} |
0 commit comments