iOS build, test and deliver using Bitrise — Part 1 [BUILD]
By: Li Hao
Tags: Process, Continuous Integration, Mobile, Bitrise, iOS

Prepare our first Workflow

The workflow Bitrise has prepared for us is primary , this flow has included build for the scheme we have selected and deploy the app to TestFlight.

Bitrise Workflow Editor

To have an efficient and automated workflow, we should always design our workflow for a specific scenario and minimise the number of steps required. So instead of having everything in one workflow, I would like to split it into two workflows:

build — This will build and run all test from the project, we will use this workflow when a PR has created to our main branch. release — This will archive our app and upload to TestFlight for us to release a new build to AppStore.

The reason I split into 2 workflows is because by splitting into 2 workflows we can keep our workflows optimise and efficient.

We can simply create a new workflow based on the default primary workflow.

Click + Workflow to add a new workflow

Before we changed the build workflow, let’s have a look at what the primary workflow is doing.

primary workflow

Activate SSH key — Bitrise setup the SSH key for current workflow. Git Clone Repository — Clone our repository. Bitrise.io Cache:Pull — Download build cache from Bitrise the speed up the build time. Do anything with Script step — This step allows you to run any custom script. Certificate and profile installer — Install Apple’s developer certificate and profiles Carthage — Install our Carthage dependencies. Xcode Archive & Export — Archive and export .ipa. Deploy to Bitrise.io — Deploy our app, logs and artifacts to Bitrise, it will show on the APPS & ARTIFACTS tab when the build has finished. Bitrise.io Cache:Push — Update our build cache on Bitrise for our next build.

As I mentioned previously, our build workflow only builds and run the test from the project. We can simplify the workflow by removing step number 4, 5, 7 and 8, we will add a new step call Xcode Test for iOS to run our test.

Your build workflow should be like this after editing:

build workflow

In Xcode Test for iOS, do make sure the Scheme name is correct (you can check the $BITRISE_SCHEME on Env Vars tab. Other than that, you also can change the device and specific the OS version you want in this step.

PS: For Xcode 12 and Carthage (As of 10th Nov 2020)

If you are using Xcode12, you will need some workaround to make it work, for more details on Carthage for Xcode 12 please check on the Carthage repository: Xcode 12 Workaround.

Here is what we need to do for Carthage to work on Bitrise with Xcode12 stack:

Commit and push the carthage.sh script for the workaround to your repository’s root folder. Delete Carthage step from the workflow. Add a new Script step and execute the script with sh carthage.sh bootstrap

This will be you build workflow with Carthage Xcode12 workaround.

Trigger a manual BUILD

Until here our build workflow is ready, we can Start/Schedule a Build on our app main page, to trigger a workflow manually. You will be able to specify the branch you would like to use and the workflow you would like to trigger.

Start/Schedule a build

Wait for the build to run a while, if everything goes well, you should see a green tick on your build.

Manual trigger build

Trigger a Webhook BUILD with PR

Moving forward to our end goal, we will need to set Bitrise to trigger the build workflow when there is a PR created. We can do this in the Workflow Editor -> Triggers tab.

There are three nested tabs inside the Triggers tab:

Push Pull Request Tag

By default, Bitrise has predefined a Push trigger for *(All) branch to run primary workflow. So we will need to remove this trigger.

Delete the pre-defined push trigger

After that move to the Pull Request tab, there is also a pre-defined PR trigger for when creating a PR from *(All) Target branch to *(All) Soruce branch to trigger a primary workflow. We will change the primary workflow to our build workflow.

Change to our build workflow

And Save it, we can try to create a PR to test our build now.

When you create a PR, you should be able to see the Bitrise build on your PR page, the below image is an example from GitHub.

GitHub PR with Bitrise build integration

You should be able to see a new build on Bitrise right after your PR has been created, the build should contain your PR URL and commit hash.

Build trigger by Webhook on PR trigger

That’s the wrap! We have our first workflow in Bitrise and will auto-trigger when we create a new PR. On the next article, we will focus on automation testing in Bitrise.

Some improvement can be done:

For the workaround script we are using sh carthage.sh bootstrap , ideally we can specify the platform with --platform ios to skip unnecessary platforms and shorter our build time but I’m having command error while adding it. There are two solutions I can think of:

Create another bash script (e.g carthage_bootstrap.sh) to run sh carthage.sh bootstrap -- platfrom ios then we can do sh carthage_bootstrap.sh in the Bitrise Script step. If you are using fastlane, you can create a new lane run the Carthage workaround bash script.

Example bitrise.yml: