CI/CD for Swift Projects: GitHub Actions Step-by-Step Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why CI/CD is Needed for Swift Projects?



Development for iOS and macOS requires a high culture of code and reliability. Every change can break the build or lead to regressions in tests. Manual checking is a slow and unreliable process. This is where CI/CD (Continuous Integration / Continuous Deployment) comes to the rescue.



GitHub Actions is a powerful and built-in tool for automating workflows directly in your repository. It allows you to automatically build the project, run tests, analyze code, and even publish the app to the App Store or TestFlight on every push or Pull Request creation.



In this article, we will break down how to set up a full CI/CD pipeline for a Swift project on GitHub Actions. You will learn how to create a workflow, configure building, running tests, linting, and deployment. All code examples are ready to use.



Main Part



1. GitHub Actions Basics for Swift



GitHub Actions works based on YAML files stored in the .github/workflows/ directory. Each file describes one or more workflows. For a Swift project, we need a runner with macOS, since building iOS/macOS applications is only possible on this platform.



A basic workflow for a Swift project looks like this:



name: Swift CI

on: push: branches: [ "main" ] pull_request: branches: [ "main" ]

jobs: build-and-test: runs-on: macos-latest

steps: - uses: actions/checkout@v4

- name: Build run: swift build

- name: Run tests run: swift test


Let's break down the key points:

  • on — triggers: run on push and pull request to main.
  • runs-on: macos-latest — use the latest macOS version for the runner.
  • actions/checkout — standard action for cloning the repository.
  • swift build and swift test — Swift Package Manager commands.


This minimal pipeline already provides a basic check. But for a real iOS/macOS application, this is not enough — you need to configure Xcode, simulators, and dependencies.



2. Configuring Build and Tests for iOS/macOS Application



If your project uses Xcode (e.g., .xcworkspace or .xcodeproj), the swift build command won't work. Instead, we use xcodebuild. You also need to specify the scheme and destination (e.g., iPhone simulator).



Example workflow for an iOS application:



name: iOS CI

on: push: branches: [ "develop", "main" ] pull_request: branches: [ "develop", "main" ]

jobs: test: runs-on: macos-14

steps: - uses: actions/checkout@v4

- name: Select Xcode version run: sudo xcode-select -s /Applications/Xcode_15.2.app

- name: Install CocoaPods (if you use) run: pod install

- name: Build and Test run: | xcodebuild clean test \ -workspace YourApp.xcworkspace \ -scheme "YourApp Scheme" \ -destination 'platform=iOS Simulator,name=iPhone 15,OS=17.2' \ -resultBundlePath TestResults env: DEVELOPER_DIR: /Applications/Xcode_15.2.app/Contents/Developer

- name: Upload test results if: always() uses: actions/upload-artifact@v4 with: name: test-results path: TestResults.xcresult


Important details: