Import existing certificate and profiles to Match and configure Fastlane for multiple Targets.

Jasmine Elamblakatt
5 min readJan 15, 2021

Managing multiple target profiles, distributing a .p12 file to the team and setting up the certificates, and resolving issues around it is a hassle.

Fastlane can automate all these tasks for us like managing certificates and profile, creating new certificate and profile when it gets expired, securing them in a Git Repo for team usage, decrypting and installing the certificates building and archiving the project with the right certificate and profile, incrementing the build version and many more tasks like this…

Here in the post let’s check

  • how to import the existing certificates to Git Repo(as I don’t want Fastlane Match to create certificates for me as it would revoke my existing certificates then)
  • Decrypt and get the certificate from Git Repo and increment the build version and upload it to TestFlight all with a single command😃

If you want information regarding the initial setup of Fastlane please refer to my previous post regarding Fastlane setup.

So let’s get started…

1. Import existing certificates and provisioning profile to Match Git Repo:

Match in Fastlane helps to create all required certificates & profiles and stores them in a separate Git repository, Google Cloud, or Amazon S3. So developers can access the Git Repo to use credentials for code signing. It’s the easiest way to share signing credentials across teams.

So to import the existing certificate and profiles to Git Repo we can use the below Match import command, we need to provide the apple Id, git URL, bundle identifier, and Team Name

fastlane match import \
—test@gmail.com \
--git_url https://github.com/test/SampleFastlaneRepo.git \
--app_identifier com.sample.fastlanetest \
--team_name "iOS Team Name" \
--type appstore

Note: For adding to a specific branch use as below

fastlane match import \
—test@gmail.com \
--git_url https://github.com/test/SampleFastlaneRepo.git \
--app_identifier com.sample.fastlanetest \
--team_name "iOS Team Name" \
--type appstore \
--git_branch feature/FastlaneCert \
--clone_branch_directly true

After running this command, it will ask for the .cer file, .p12 file, and the profiles path. Then it will ask for the passphrase to secure your certificates and profiles just make sure you remember the passphrase provided.

Once the paths are provided it will then encrypt the certificates and provisioning profiles and upload them to Git Repo.

Here we have successfully imported our existing certificates and profile to the encrypted Git Repo.

2. Setup .env Files for different Targets

As my project contains different targets i.e Prod and Dev so the configurations related to these targets can be added to the environmental files. The files are text files with .env.TargetName format. After adding all the environmental file my Fastlane folder looks like this

.env file contain all the iOS related configurations and Match parameters required like Git Repo path

.env file

For each target create a .env.target file and specify the target and scheme related information in the file

.env.dev file

3. Fastfile:

Fastfile contains all the commands in the form of different lanes to automate the project that can be run with Fastlane.

So I have added 3 lanes in the Fastfile to automate my process:

a. Signing:

desc “Sync signing”
lane :signing do

sync_code_signing
mapping = Actions.lane_context[
SharedValues::MATCH_PROVISIONING_PROFILE_MAPPING
]
update_code_signing_settings()
end

“sync_code_signing” will easily sync the certificates and profiles across the team (via match)

Actions can communicate with each other using a shared hash lane_context, that can be accessed in other actions, plugins, or lanes: lane_context[SharedValues:XYZ]. The match provisioning profile mapping

mapping = Actions.lane_context[
SharedValues::MATCH_PROVISIONING_PROFILE_MAPPING
]

“update_code_signing_settings()” it configures Xcode’s Codesigning options

It checks the FL_PROJECT_USE_AUTOMATIC_SIGNING option in the .env file and updates the Xcode project setup as for Release we don’t need automatic signing.

b. build:

This lane will call the signing lane then will increment the build version and will generate a signed .ipa file with “build_ios_app” action.

desc “Build binary”
lane :build do
signing
increment_build_number(xcodeproj: “SampleFastlane.xcodeproj”)
build_ios_app
end

c. upload

This lane call the first build lane, build lane will call the signing lane and then will generate a signed .ipa file with the “upload_to_testflight” action it will uploads the build to TestFlight.

“upload_to_testflight” picks the required parameters from the .env file.

desc “Upload to Testlfight”
lane :upload do
build
upload_to_testflight
end

Here the setup is done, now let’s run Fastlane.

4. Run Fastlane

To get all the certificates and profiles from Match and setup the project configurations just go to the project location in the terminal and run below the first lane as below

fastlane signing --env prod

To increment the build version and to generate signed .ipa file use below command

fastlane build --env prod

To do the above 2 options and to upload it to TestFlight just do

fastlane upload --env prod

By just running the last lane “upload” you can get all the certificates and profiles from Git Repo, set up the certificate, increment the build version and upload the build to TestFlight and checks the missing compliances also😃

So just run the Fastlane command and enjoy your day.

Thank you for reading.🙏 If this post helped you, do clap for me 😃

HaPpY CoDiNg….

--

--