iOS Deployment Pipeline With Travis Stages
Learn how to create a pipeline for continuous integration in development of your iOS app with Travis Stages, from the TravisCI cloud-based server.
Join the DZone community and get the full member experience.Join For Free
the process of continuous integration performs various activities like analyzing, building, testing, and deploying ios apps. it would be a great idea to break down each build task into various stages and execute them independently. this process is also called build pipeline or deployment pipeline . as an ios developer, you might have experienced that building an ios app can take a lot of time, as can testing. the pipeline is the way to deal with each stage of ios development to get fast feedback. we probably don't want to get a build failure for code styling issues after spending hours in the build and test phase. self-hosted continuous integration servers like jenkins and teamcity have good support to create build pipelines, but cloud-based ci servers like travisci , circleci , and some others are also rolling out deployment pipelining features. in this post, we will see how to setup ios deployment pipeline using travis stages feature. please note, i have used travisci as an example because i used it for some of my open-source projects. there might be similar features available for other could based ci servers as well.
stages of ios deployment
before jumping into travisci setup, we will see the important stages of deploying any ios apps. the major build phases are
- analyzing source code for code styling issues,
- testing with xctest or other frameworks,
- building and archiving ios app,
- distributing an ios app to different beta testing services, and
- uploading the ios app to itunes connect.
these are the major stages that have to be automated on a continuous integration server to enable automatic deployments.
just in case you are new to travisci , it is a cloud-based continuous integration platform embedded with github, which allows you to run ci builds for open-source as well as enterprise projects. it has good documentation to get started with continuous integration. travisci has announced a new feature, travis stages, to enable deployment pipelines for open-source as well as enterprise projects. the stages can be enabled by adding the following code sample in the.travis.yml file:
jobs: include: - stage: test script: ./test 1 - # stage name not required, will continue to use `test` script: ./test 2 - stage: deploy script: ./deploy
in order to apply this principle in ios projects, let's create an open-source project on github (you can use any ios app):
- create a blank single view app named swiftlint-ci with unit and ui test targets in xcode.
- add swiftlint using cocoapods for checking the swift code syntax. this can be done by creating a podfile and running the pod install command.
now that we have a basic ios project to be configured for ci, we can now add support to run the build on travisci and enable the following stages:
- build stage to lint swift code with swiftlint
- build stage to run unit tests
- build stages to run ui tests
creating travis.yml with stages
travisci uses macos virtual images to run builds; the list of the all the images can be found here . we can pick latest xcode9 images to run a build for our demo app. we have to run a script to execute swiftlint and run xctest using fastlane scan . we can set up our travis.yml file like this:
language: objective-c osx_image: xcode9 notifications: email: false branches: only: - master before_install: - gem install fastlane --no-ri --no-rdoc --no-document jobs: include: - stage: swiflint script: - ./pods/swiftlint/swiftlint --reporter junit - stage: xctest script: - fastlane scan -s swiftlint-citests - stage: xcuitest script: - fastlane scan -s swiftlint-ciuitests
the config file above will run three different stages for swiftlint, unit testing, and ui testing respectively. once build is triggered on travisci, it will look like this:
this indicates that we have a build pipeline for each stage, and no stage will run unless the previous stage is passed.
extending travis stages for ios deployment
now that we have setup up build pipeline for code analysis and running unit and ui tests. we can take this pipeline further to deploy an ios app to itunes connects. automating and distributing builds are not in the scope of this post but you can read my previous post on automating ios deployments to itunes connects using fastlane tools here . we can extend travis stages to deploy an app like this:
jobs: include: - stage: swiflint script: - ./pods/swiftlint/swiftlint --reporter junit - stage: xctest script: - fastlane scan -s swiftlint-citests - stage: xcuitest script: - fastlane scan -s swiftlint-ciuitests - stage: build app # write a script to create keychain, import certificate, download provisioning profies in the travisvm before build script: - fastlane gym -s swiftlint-ci -q release - stage: upload to testflight script: - fastlane pilot upload --changelog "new features"
this configuration will take our ios app to the testflight assuming we set up all the provisioning and certificates in the travisci virtual machines. note that you have to use different fastlane actions to create keychain, import certificates to the keychain to allow travisci virtual machine codesign an app. in this way, we can break down ios deployment phases into travisci stages for better visibility.
parallelizing travis builds within stages
we can still parallelize travis builds within stages to speed up the overall build execution. we can run unit and ui tests in parallel as well we can distribute an ipa to multiple platforms like fabric and testflight at the same time. not sure travisci has documented the parallelism but there is a trick to run the builds in parallel in the stage; we have to give the same name to the stage to run in parallel. in our swiftlint-ci app, we can parallelize running unit tests as well as ui tests by calling it the "xctest" stage, something like this:
jobs: include: - stage: swiflint script: - ./pods/swiftlint/swiftlint --reporter junit - stage: xctest script: - fastlane scan -s swiftlint-citests - stage: xctest script: - fastlane scan -s swiftlint-ciuitests
note that we have given the same name to the build stage but are running different scripts for each stage. this will run the "xctest" stage in the parallel. that's a trick to run travis builds in parallel within a stage. the parallel build will look like this:
pros and cons of using travis stages
although the travis build stages seem to be great options to create deployment pipelines, there are some pitfalls we need to consider. here are some pros and cons of using travis build stages for ios deployment pipelines:
- we can have visibility of builds for each stage of the deployment pipeline.
- we can get early feedback if a build stage fails within the deployment pipeline.
- we can apply parallelism within a build stage as we can run multiple jobs in parallel in the stage. currently, we are running unit tests and ui tests in parallel.
- we can assign environment variablea per stage.
- travis can be used for deploying apps to fabric and testflight using different stages.
- still in beta and has some bugs. check known issues here .
- triggers a new vm for every build in the stage, so checking out the repo for each task is time-consuming.
- we need to run the stage with the same name to achieve parallelism, which is a duplication of script execution.
- we can not share data between stages; we need to use aws s3 bucket, which isn't feasible.
source code & travisci builds
- github source code: the sample source code for this post is available on github repository swiftlint-ci .
- travisci builds: the sequential build is available here and the parallelized build is here .
the cloud-based ci servers are providing the ability to run ios builds in multiple stages to create seamless deployment pipelines for ios apps. we can take advantage of such features for better feedback and visibility in the build process.
Published at DZone with permission of Shashikant Jagtap, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.