Automated iOS beta distribution using Travis CI and Testflight
Creating an adhoc build for your beta testers is not something really technically challenging thing, but it could still be time consuming. Wouldn’t it be great to have someone delegated for the task?
You probably heard the practice of continuous integration:
CI is originally intended to be used in combination with automated unit tests written through the practices of test-driven development
The fact is whether or not you have written unit tests for your project, we can actually make use of Travis-CI to help on creating and uploading the build to your app distribution platform, automagically.
Our requirements
- A github private repo, project setup with Cocoapods and using Swift
- Only execute a build when a new commit pushed to specific branch (we will name the branch “testflight”
- Compiling both staging and production version of our app and distribute to Testflight
1. download the script
2. Add .travis.yml to your root of your repository
- Replace MYAPP, TARGET_STAGING, TARGET_RELEASE with your project setup
3. Create a folder “scripts/travis” and move the following files into the folder
- add-key.sh
- remove-key.sh
- testflight.sh
4. Properly config and make sure of your Xcode mobile provisioning and code signing profile
Just make afterwards it can be properly compiled by trying to build to your iPhone.
5. Export files from the Keychain app to the “scripts/travis” folder
- “Apple Worldwide Developer Relations Certification Authority” to apple.cert
- Your iPhone Distribution certificate to dist.cert
- Your iPhone Distribution private key to dist.p12 (choose password)
6. Execute following lines in terminal from the root of your repository
- Install travis command line and login (Only needed for the first time you install travis)
$ gem install travis
$ travis login --pro
- Encrypt your dist.p12's password and obtain your Testflight team token and api token
$ travis encrypt "KEY_PASSWORD=YOUR_PASSWORD" --add
$ travis encrypt "TEAM_TOKEN=TESTFLIGHT_TEAM_TOKEN" --add
$ travis encrypt "API_TOKEN=TESTFLIGHT_API_TOKEN" --add
Afterwards your .travis.yml should look something like this, with the appended “secure” rows.
7. Install the corresponding provisioning
- Open the Accounts menu by Xcode > Preferences…
Copy the mobile provisioning from your finder, since I’m building up both staging/production version I should find two provisioings with different app id and put them into the “scripts/travis/profile” folder.
8. Commit the files to the “testflight” branch
The branch name can really be anything you like, just make sure you also change it inside “testflight.sh” and “.travis.yml” file.
You should ALSO commit your project.pbxproj file if you have updated your code signing identity and provisionings in step 4.
9. Enable and configure your repository in Travis
Finally, push your repository and check the travis console, the queued job should starts and executes within a minute.
You should see something similar printed out in the console when it finishes. Now your app should have uploaded successfully to Testflight!
Final words
So that’s it, whenever a commit get pushed to the “testflight” branch, our team gets a new beta distribution automatically, this automation saved us hours of repetitive work.
Reminded that Travis charges a certain amount per month for private repositories, while open sources projects are completely free. Though You should be aware that we’re committing our p12 into the repositories, while it doesn’t matter for private repositories, if it was a public one it could be an security issue.
For more on how to protect your credentials in open source projects, objc.io has a decent article on describing that also with the sample script for beta distribution with HockeyApp.
If you don’t want to rely on Travis’s cloud solution, you can also setup your own server using Jenkins, which is also a decent CI solution which lets you deploy your automation server in your own machine.
Credits
The original version of the scripts goes to Johannes Würbach, I slightly modified and make it supported distributing two versions of the app in one go.