How to setup and use GitHub Actions as a CI for Swift Packages

GACalc

This repo shows how to setup and use GitHub Actions as a CI for Swift Packages.







Available environments on GitHib

List of the all available environments and tools is here

Actions

Below you will find a list of actions that can be used for Swift Package Development. Simply create a new action on GitHub, and copy & paste the code.

Build package

name: Build Package

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

jobs:
  build:
    runs-on: macos-11

    steps:
    - uses: actions/checkout@v2
        
    - name: Build
      run: swift build -v

Test package

name: Test Package

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

jobs:
  test:
    runs-on: macos-11

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

Test package & Gather Code Coverage using codecov.io

Replace the {YOUR_PACKAGE_NAME} with the name of your package. For example if the name of the package is gacalc, then replace the fragment {YOUR_PACKAGE_NAME}PackageTests with GacalcPackageTests.

Note: Although the name of the package is all lowercased, the replaced name starts with uppercased first letter!
You can double check the name locally. Run the `swift build` command in the Terminal. Then open the `.build` folder: `open .build`.
Inside the `.build/debug` folder you will find either `{YOUR_PACKAGE_NAME}PackageTests.product` or `{YOUR_PACKAGE_NAME}PackageTests.xctest` file.

name: Test Package

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

jobs:
  test:
    runs-on: macos-11

    steps:
      - name: Get Sources
        uses: actions/checkout@v2

      - name: Build Package
        run: swift build -v

      - name: Run tests
        run: swift test --enable-code-coverage -v
      
      - name: Setup Xcode
        uses: maxim-lobanov/setup-xcode@v1.4.0
        with:
          xcode-version: "13.1"

      - name: Gather code coverage
        run: xcrun llvm-cov export -format="lcov" .build/debug/{YOUR_PACKAGE_NAME}PackageTests.xctest/Contents/MacOS/{YOUR_PACKAGE_NAME}PackageTests -instr-profile .build/debug/codecov/default.profdata > coverage_report.lcov

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v2
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          fail_ci_if_error: true
          files: ./coverage_report.lcov
          verbose: true

When this action is executed for a pull request, the codevcov bot will add a comment with a new code coverage statistics:

Code linting using SwiftLint

Linting using macOS as host machine:

name: Lint code

on:
  pull_request:
    paths:
      - '.github/workflows/codelint.yml'
      - '.swiftlint.yml'
      - '**/*.swift'

jobs:
  SwiftLint:
    runs-on: macos-11
    steps:
      - uses: actions/checkout@v1
      
      - name: Lint code using SwiftLint
        run: swiftlint lint --reporter github-actions-logging

Linting using ubuntu as host machine with a SwiftLint Github Action:

name: Lint code

on:
  pull_request:
    paths:
      - '.github/workflows/codelint.yml'
      - '.swiftlint.yml'
      - '**/*.swift'

jobs:
  SwiftLint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      
      - name: Lint code using SwiftLint
        uses: norio-nomura/action-swiftlint@3.2.1

Both options lint the code. If there are any warnings or errors they will be added, as comments, to the PR’s code:

GitHub

View Github