{"meta":{"title":"Building and testing Swift","intro":"Learn how to create a continuous integration (CI) workflow to build and test your Swift project.","product":"GitHub Actions","breadcrumbs":[{"href":"/en/actions","title":"GitHub Actions"},{"href":"/en/actions/tutorials","title":"Tutorials"},{"href":"/en/actions/tutorials/build-and-test-code","title":"Build and test code"},{"href":"/en/actions/tutorials/build-and-test-code/swift","title":"Swift"}],"documentType":"article"},"body":"# Building and testing Swift\n\nLearn how to create a continuous integration (CI) workflow to build and test your Swift project.\n\n## Introduction\n\nThis guide shows you how to build and test a Swift package.\n\nGitHub-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 [GitHub-hosted runners](/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-software).\n\n## Prerequisites\n\nYou should already be familiar with YAML syntax and how it's used with GitHub Actions. For more information, see [Workflow syntax for GitHub Actions](/en/actions/using-workflows/workflow-syntax-for-github-actions).\n\nWe recommend that you have a basic understanding of Swift packages. For more information, see [Swift Packages](https://developer.apple.com/documentation/xcode/swift-packages) in the Apple developer documentation.\n\n## Using a Swift workflow template\n\nTo get started quickly, add a workflow template to the `.github/workflows` directory of your repository.\n\nGitHub provides a workflow template for Swift that should work for most Swift projects. The subsequent sections of this guide give examples of how you can customize this workflow template.\n\n1. On GitHub, navigate to the main page of the repository.\n\n2. Under your repository name, click **<svg version=\"1.1\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" class=\"octicon octicon-play\" aria-label=\"play\" role=\"img\"><path d=\"M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0ZM1.5 8a6.5 6.5 0 1 0 13 0 6.5 6.5 0 0 0-13 0Zm4.879-2.773 4.264 2.559a.25.25 0 0 1 0 .428l-4.264 2.559A.25.25 0 0 1 6 10.559V5.442a.25.25 0 0 1 .379-.215Z\"></path></svg> Actions**.\n\n   ![Screenshot of the tabs for the \"github/docs\" repository. The \"Actions\" tab is highlighted with an orange outline.](/assets/images/help/repository/actions-tab-global-nav-update.png)\n\n3. If you already have a workflow in your repository, click **New workflow**.\n\n4. The \"Choose a workflow\" page shows a selection of recommended workflow templates. Search for \"swift\".\n\n5. Filter the selection of workflows by clicking **Continuous integration**.\n\n6. On the \"Swift\" workflow, click **Configure**.\n\n7. Edit the workflow as required. For example, change the branch on which the workflow will run.\n\n8. Click **Commit changes**.\n\n   The `swift.yml` workflow file is added to the `.github/workflows` directory of your repository.\n\n## Specifying a Swift version\n\nTo use a specific preinstalled version of Swift on a GitHub-hosted runner, use the `swift-actions/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 [`swift-actions/setup-swift`](https://github.com/marketplace/actions/setup-swift) action.\n\nIf you are using a self-hosted runner, you must install your desired Swift versions and add them to `PATH`.\n\nThe examples below demonstrate using the `swift-actions/setup-swift` action.\n\n### Using multiple Swift versions\n\nYou can configure your job to use multiple versions of Swift in a matrix.\n\n```yaml copy\n\n# This workflow uses actions that are not certified by GitHub.\n# They are provided by a third-party and are governed by\n# separate terms of service, privacy policy, and support\n# documentation.\n\n# GitHub recommends pinning actions to a commit SHA.\n# To get a newer version, you will need to update the SHA.\n# You can also reference a tag or branch, but the action may change without warning.\n\nname: Swift\n\non: [push]\n\njobs:\n  build:\n    name: Swift ${{ matrix.swift }} on ${{ matrix.os }}\n    strategy:\n      matrix:\n        os: [ubuntu-latest, macos-latest]\n        swift: [\"5.2\", \"5.3\"]\n    runs-on: ${{ matrix.os }}\n    steps:\n      - uses: swift-actions/setup-swift@65540b95f51493d65f5e59e97dcef9629ddf11bf\n        with:\n          swift-version: ${{ matrix.swift }}\n      - uses: actions/checkout@v6\n      - name: Build\n        run: swift build\n      - name: Run tests\n        run: swift test\n```\n\n### Using a single specific Swift version\n\nYou can configure your job to use a single specific version of Swift, such as `5.3.3`.\n\n```yaml copy\n# This workflow uses actions that are not certified by GitHub.\n# They are provided by a third-party and are governed by\n# separate terms of service, privacy policy, and support\n# documentation.\nsteps:\n  - uses: swift-actions/setup-swift@65540b95f51493d65f5e59e97dcef9629ddf11bf\n    with:\n      swift-version: \"5.3.3\"\n  - name: Get swift version\n    run: swift --version # Swift 5.3.3\n```\n\n## Building and testing your code\n\nYou 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:\n\n```yaml copy\n# This workflow uses actions that are not certified by GitHub.\n# They are provided by a third-party and are governed by\n# separate terms of service, privacy policy, and support\n# documentation.\nsteps:\n  - uses: actions/checkout@v6\n  - uses: swift-actions/setup-swift@65540b95f51493d65f5e59e97dcef9629ddf11bf\n    with:\n      swift-version: \"5.3.3\"\n  - name: Build\n    run: swift build\n  - name: Run tests\n    run: swift test\n```"}