fastlane + bitrise for iOS

Alfredo Luco G
5 min readMay 19, 2021
Photo by blocks on Unsplash

Bitrise is a tool that simplify the mobile pipelines of gitlab, github or Bitbucket. In fact, it’s very interactive with some common ci+cd task such as testing (UI and unit tests), deployment (ipa to testflight), send slack messaging and so on. However it’s hard to set up some bitrise enviroment variables for code signing. Perhaps, you’re wondering what tool you need to implement as a bitrise third party, the answer is Fastlane.

Setup Enviroment

As a first step go on to the project’s root project and execute this command from terminal:

sudo gem install fastlane

That command allows you to make some fastlane scripts. The fastlane tool allows you to handle ci+cd task such as testing or deployment so we’ll use this to replace some bitrise tasks. Now when you hace fastlane installed you need to create a Gemfile, thats ‘cos any virtual machine needs to run some dependencias (such as cocoapods, fastlane and others). So, you need to define a Gemfile that will be implemented by bundler.

Bundler is a tool that install some dependencies. So in that case you need execute the following command:

sudo gem install bundler

Now, put the following instructions of your Gemfile:

source 'https://rubygems.org'gem 'cocoapods'
gem "fastlane"
gem 'xcov', '~>1.5'

Now you can use the fastlane tool, so go to the terminal and put the following command:

fastlane init

You’ll see a folder called fastlane. That contains two main files Appfile and Fastfile.

Now let’s configure the Appfile. The Appfile defines your current iOS app enviroment such as your app identifier (bundleID), team identifier and itc team identifier. In this case you can get the app identifier from xCode, but the team identifier and itc team identifier you need to look from apple developer portal:

Now, to track your itc team identifier you need to see the following json (of course you need to login in apple developer) https://appstoreconnect.apple.com/WebObjects/iTunesConnect.woa/ra/user/detail

Notice your itc team identifier is your contentProviderId.

Now you need to put the following lines in your Appfile:

app_identifier("com.Test.App") # The bundle identifier of your app
team_id("xxx") # your team id
itc_team_id("xxx") # your content provider id

Preparing some Tests

Before run some fastlane instructions you need to set up your xcode project as “Apple generic” version system. So go to your build settings and change the version system as Apple Generic

For some reason if won’t works you can define a ruby function that allows to you run your xcode project as an apple generic system:

def enable_manual_provisioning(project_file)
UI.message 'Switching to manual code signing'
fastlane_require 'xcodeproj'
project = Xcodeproj::Project.open(project_file)
target = project.native_targets.detect { |target| target.name == 'AppTest' }
target.build_configurations.each do |item|
item.build_settings['VERSIONING_SYSTEM'] = "Apple Generic"
end
project.save
end

Now we can prepare our lane, please add the following lines into your Fastfile. Fastfile define your lanes (commands) to arrange some ci tasks.

default_platform(:ios)platform :ios do
desc "Run some tests"
# Unit tests lane :tests do
clear_derived_data
scan(
workspace: "AppTest.xcworkspace",
devices: ["iPhone 8"],
force_quit_simulator: true,
reset_simulator: true,
reinstall_app: true,
scheme: "AppTest"
)
end
end

That code simply clean your project and run your tests. To run that just put fastlane tests in your terminal (please you must be in your project’s root directory)

Now it’s time to build it on bitrise. So go to bitrise.io and register an account. You must click on “Add New App” and follow the steps. They are very intuitive.Now you need to take a look of workflow steps. You will see a few steps so you must put a swiftlint step and fastlane step and you might remove de iOS testing step:

Now, lets configure our fastlane step. You simply put our fastlane command into the step like this:

So let’s arrange a new build and see what happens, so please go back your app and put start/schedule a new build.

NOTE: If your build does not succeed maybe you need to install a virtual machine with the same enviroments (e.g MAC OSX Catalina 10.15.4 + 4 GB RAM + 2 CPU)

Arrange the deployment

Photo by SpaceX on Unsplash

To make an automatic deployment we need first an appstore connect api key so first go to appstore connect and create a new api key:

Now download the p8 file and save it into the fastlane folder. Now we are going to make two differente lanes; first the build archive and second the testflight lane. But before that, for provising profile reasons we need to turn off the automatic managing profile and set up manually the distribution and development certificates.

NOTE: It can be solve with match by fastlane but it’s pretty complicated so maybe you need to do that to save time.

Now there’s the build lane:

lane :beta do
cert
increment_build_number()
build_app(scheme: "AppTest",
workspace: "AppTest.xcworkspace",
include_bitcode: true,
clean: true)
end

Basically, this lane retrieve the certificates, then increment the build number and finally build the app to distribution.

After setting this you are going to upload to testflight with the following lane:

# testflight  lane :upload do
app_store_connect_api_key(
key_id: "xxxx",
issuer_id: "xxxx-xxxx-xxxx-xxxx-xxxxxxxxx",
key_filepath: "./fastlane/xxxxx.p8",
in_house: false,
duration: 200
)
cert
sigh(force: true)
beta
pilot(
changelog: "alpha",
groups: ["Testers"],
notify_external_testers: true,
first_name: "xxxx",
last_name: "xxxx",
beta_app_feedback_email: "xxxx@xxx.com",
beta_app_description: "Alpha",
uses_non_exempt_encryption: false,
wait_processing_interval: 300,
beta_app_review_info: {
contact_email: "xxx@xxx.com",
contact_first_name: "xxx",
contact_last_name: "xxx",
contact_phone: "+11xxxxxxxx",
notes: "Alpha"
},
)
end

Basically this line follows these steps:

  1. Authenticate with appstore connect within your api key
  2. Retrieve your distribution and development certificates
  3. Force to retrieve all your provisioning profiles
  4. Build your beta (custom lane described above)
  5. Build the pilot to deploy to testflight yout alpha as a change log with one group of testers and notify them.

Now you simply put fastlane upload in bitrise with your deploy workflow:

So, that’s it.

Happy coding

--

--