Github Actions CI for Swift Projects
Tags: github, ci, swift, ios
Published: 2019-08-29 09:38:12

---
title: Github Actions CI for Swift Projects
published: true
description: Let's take a look at how configure Github Actions CI for Swift projects
tags: github, CI, swift, iOS
cover_image: https://thepracticaldev.s3.amazonaws.com/i/1jhwsdip2f3xdv6pwp6y.jpg
canonical_url: https://medium.com/rosberryapps/github-actions-ci-for-swift-projects-c129baceed1a
---

Github [introduced](https://github.blog/2019-08-08-github-actions-now-supports-ci-cd) CI and CD based on actions. It allows you to automate how you analyse, build, test and deploy your projects on any platform, including Linux, macOS and Windows.

I was excited over this news and can't wait to try it out. My idea was to automate building and unit testing of Swift projects.
There are two types of actions and we should know the difference before starting.

## Docker container vs JS

Originally all actions were based on Docker and used HCL syntax for workflows. Actions based on Docker run only in Linux virtual environment. With CI/CD announcement Github released JavaScript based actions and new YAML syntax for workflows.

For iOS projects you can use only JS actions because they should be run on macOS. Moreover using a JavaScript action simplifies the action code and executes faster than a Docker container action.

## Lights! Camera! Github Action!

As an example I will use [Texstyle](https://github.com/rosberry/texstyle). It's a framework for formatting attributed strings. Let's start with a complete configuration and inspect it step by step:
{% gist https://gist.github.com/artemnovichkov/25d2409d240661ca49bf8d62cf8d45e4 %}

At first you should select an event to trigger the workflow. My workflow is triggered on any pull request in the repository. Optionally you can label the workflow with `name`.

Workflows must contain at least one job. My workflow contains only one job for testing. It is possible to run up to 20 jobs concurrently per repository across all workflows.

Jobs run on virtual host machines that are specified with `runs-on`. In my case it's `macOS-latest`, because I want to use xcodebuild. It's a standard Apple developer tool and works on macOS only.

Texstyle supports both iOS and tvOS, so I want to run xcodebuild with two destinations. To specify different variations, I used `strategy.matrix`. I added two simulators with actual OS versions.

And the last step is to adding steps 🙃. I used `checkout` action to access the contents of the repository. You can specify the version of actions after @ sign with a release version, branch or specific commit.
Everything is ready for testing. I added `run` with bash commands to change current directory, run xcodebuild and send code coverage data to [Codecov](https://codecov.io). To use matrix variables, I added `env` with the required variable name.

To read more about workflow syntax, check the [documentation](https://help.github.com/en/articles/workflow-syntax-for-github-actions).

Comparing to Travis CI, my configuration looked like:
{% gist https://gist.github.com/artemnovichkov/dbe1a07916c889ecdae015c06387347a
%}

That's it! Here is an example of live logs:
![https://github.com/rosberry/texstyle/pull/15/checks](https://thepracticaldev.s3.amazonaws.com/i/vc4uw61l63d9fybvz28w.png)
But that's not the end, it's just the start! Suppose that we want to reject pull requests if there are any linting warnings or errors. The most popular lint tool for Swift is [Swiftlint](https://github.com/realm/SwiftLint). For now it's possible to run it and see warnings in logs that is not enough. It would be great to see annotations from SwiftLint Violations via [GitHub Checks API](https://blog.github.com/2018-05-07-introducing-checks-api/), or even have suggested changes. And actions allow to use this API easily.

## Conclusion

As for me Github Actions CI is a game changer. It is deeply integrated into the service. And community-driven actions allow to reuse common scenarios.

Let's summarise my experience with pros and cons.

Pros:
- Various triggers for workflows. Pull requests, staring repos, project card statuses, scheduled events etc.
- Speed. According to logs for Texstyle, last builds were 2 min 12 sec on Travis CI and 1 min 8 sec on Github CI.
- Preinstalled software. macOS images have fastlane, swiftlint, Cocoapods, Carthage etc.
- [Marketplace](https://github.com/marketplace?type=actions). There are plenty of actions developed by community. You can reuse any of them.
- Pricing. Of course, it's free for open source. The time limits look pretty good - 2000 minutes for free plan and 3000 minutes with a Pro plan including private repositories 😏.

Cons:
- Still in beta. First actions were written in HCL syntax. Support for it will be removed on September 30. And maybe it's not the end, YAML syntax may be updated as well. So be ready to migrate your actions before public release.
- Debugging. It's hard to debug workflows as any CI service. You must trigger related event and wait for all steps. It's not possible to test it locally, even validate workflow syntax.
- Documentation. On of the goals of this article is to clarify Actions benefit for myself and give it a try on real project. I had many unobvious errors, official documentation and StackOverflow won't help. For instance, a multiline run command won't work without a `name` label. I believe open source workflows will help to avoid dump errors and handle edge cases in the future.

Thanks for reading! If you have any questions or ideas of workflows, feel free to leave comments or ping me on Twitter [@iosartem](http://twitter.com/iosartem).

## References

[Automating your workflow with GitHub Actions](https://help.github.com/en/categories/automating-your-workflow-with-github-actions)
[Github Actions](https://github.com/actions)
[Awesome Actions](https://github.com/sdras/awesome-actions)