Build and Test Swift Packages in Azure Pipelines

Build and Test Swift Packages in Azure Pipelines

With the introduction of Swift Package Manager (SPM), a number of projects are migrating fully but the majority are keeping SPM alongside Carthage and/or CocoaPods.

Migrating a package from CocoaPods to SPM may be backed with a couple of reasons such as better versioning, better support inside Xcode, etc. Internally, we started migrating some of our reusable code from CocoaPods to SPM. At this time, we can only migrate if some criteria are met:

  1. Pre-complied binaries are not required. This happens to be most of our packages since they are only in git repositories accessible internally. SPM does not yet support pre-compiled binaries a.k.a binary frameworks. See forum for more.
  2. There are no static resources that need redistribution e.g. json files, images, etc. Once again, SPM does not yet support resources. See bug report for more.

In the upcoming Swift 5.3 release, these should be fixed.

With that out of the way, we sort to setup automated builds for each code check-in in Azure DevOps which is what we use for all of our development work. The pipeline is simple with only 4 steps, and the first one being optional depending on how you handle versioning. I thought I should share this with whomever might be looking for it.

Here is the pipeline definition file. You can add it to the root of your repository in a file named azure-pipelines.yml.

# Build and test swift packages with GitVersion
 
trigger:
  batch: true
  branches:
    include:
      - master
 
pool:
  vmImage: 'macos-latest'
 
steps:
  - task: GitVersion@5
    displayName: 'GitVersion'
 
  - script: swift package resolve
    failOnStderr: true
    displayName: 'swift package resolve'
 
  - script: swift build --build-tests
    failOnStderr: true
    displayName: 'swift build --build-tests'
 
  - script: swift test
    # setting failOnStderr: true will cause failure
    # because 'swift test' writes content to stderr
    #  failOnStderr: true
    displayName: 'swift test'