read

Travis CI is a service, providing machines, to help build your app, test, deploy, and more.

Note: It integrates very easily with github, but does NOT support bitbucket etc.

It is FREE for open source projects eg. public Github project.

First 100 builds are free too, as a trial. Afterwhich, it starts from $69/mth for 1 concurrent job. Read about alternatives in last section of this post.

Setting Up

  1. Create .travis.yml in project root and configure (see below)
  2. Push to repository
  3. Sign in your Github account on travis-ci.com
  4. Configure on the dashboard

The most important step is with .travis.yml configuration file.

You can customize all you want. A bare minimum one for swift looks like this:

osx_image: xcode9.2
language: swift
script:
  - fastlane run_tests

The lane “run_tests”, builds and run unit tests. The result will then be available to travis-ci.

When to build

There are 2 options to turn on/off in the dashboard:

  1. Build branch updates
  2. Build pull request updates

The default is both ON.

Having (1) ON could be build crazy, because every commit and push will trigger a build. But you could do tweak more, with advanced configuration.

When to build (Advanced)

You may further restrict when to build. You can safelist branches and tags, with regex.

So let’s say you want only build with tag such as “build-123”, you can use regex in the yaml file:

# safelist
branches:
  only:
  - master
  - /^build-\d+$/

Another way is to write bash commands in the script phrase.

For example, if you want to run a lane named “beta” when the commit message has “[beta]” in it:

script: 
  - if [[ "$TRAVIS_COMMIT_MESSAGE" = *"[beta]"* ]]; then bundle exec fastlane beta; else bundle exec fastlane some_other_lane; fi

TRAVIS_COMMIT_MESSAGE is one of many environment variables provided in travis.

After build comes deploy

Deploy is an optional phase, the CD in CI/CD.

If build is successful, then the deploy phase will start.

For iOS, the deploy phase could be pushing the build to Testflight.

Encrypting secure env var

In .travis.yml, you will frequently see secure key.

These are environment variables that you can use everywhere - travis.yml, Fastfile or any script.

As they are sensitive values, you have to encrypt them in .travis.yml.

Run this in terminal:

# Install the tool
gem install travis

# Login
travis login --pro

# Encrypt the env var and add to travis.yml
travis encrypt SOMEVAR="secretvalue" --add

For every key-value, it will add a “secure” to the env.global list.

env:
  global:
  - secure: some-encrypted-value
  - secure: another-encrypted-value

You can then use the key eg. SOMEVAR as an environment variable.

Bonus: Dependencies in multiple repository

If your project has dependencies to multiple repositories, there are 2 approach:

  1. Deploy key
  2. User key

User key is the best practice, with a dedicated CI user account. This means creating an independent Github account, with access to those repositories, then generate the user’s SSH key for use in travis.

User key is preferred over Deploy key because Deploy key is per repo, hence it gets hard to maintain many keys (1 key per repo).

Alternatives

The thing with Travis CI is that it is expensive, starting from $69/mth.

Bamboo is part of Atlassian, so it integrates well with Bitbucket, Jira, etc, and starts from $10/mth. Setup for Xcode.

Shippable has a generous free hosted service providing 150 builds/mth for private project.

Xcode Server aka XCS have bots (like jobs). It integrates to Xcode nicely.

Jenkins is free and you can self host it, on a macOS machine, that also means maintaining it.

Also, sample Spotify CI scripts for their open source projects. They use Travis CI and others.


Image

@samwize

¯\_(ツ)_/¯

Back to Home