Skip to content

This repo shows how to set up and use GitHub Actions as a CI for Swift Packages

Notifications You must be signed in to change notification settings

mtynior/SwiftPackageWithGithubActionsAsCI

Repository files navigation

SwiftPackageWithGithubActionsAsCI

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

This repository is a part of an article published at This Dev Brain

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 & upload report to 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:

Test package, gather code coverage & upload report to Code Climate

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: Test & publish code coverage to Code Climate
        uses: paambaati/codeclimate-action@v3.0.0
        env:
          CC_TEST_REPORTER_ID: ${{ secrets.CODE_CLIMATE_TOKEN }}
        with:
          coverageCommand: swift test --enable-code-coverage
          debug: true
          coverageLocations: |
            ${{github.workspace}}/.build/debug/codecov/*.json:lcov-json

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: