iTranslated by AI
Delivering the Latest Build to TestFlight with GitHub Actions
In iOS app development, we use a mechanism called TestFlight to verify the package currently under development.
I have automated this process using GitHub Actions.
I will introduce the steps to set up GitHub Actions so that "when a commit is made to a certain branch, the latest build is distributed via TestFlight."
Implementation Flow
- Prepare certificate management using fastlane match
- Implement a lane for TestFlight in fastlane
- Configure GitHub Actions
Note that this article assumes fastlane is already set up.
If you haven't used fastlane yet, please try setting it up by referring to articles like this one.
Prepare certificate management using fastlane match
fastlane match is one of the fastlane actions designed to simplify the complex management of iOS certificates. It solves problems such as different team members using different certificates or having trouble with certificates for CI. (Official documentation is here)
First, initialize match:
fastlane match init
Then, create the certificates:
fastlane match appstore
Since we want a certificate to be used for TestFlight builds this time, we specify appstore.
In fastlane match, a separate space is created for certificate management, where certificates and provisioning profiles are managed.
You will be asked for the management method and URL during the command execution. In this case, I specified a Git repository.
To confirm, let's look at the contents of fastlane/Matchfile.
You can see that the specified method (git) and the repository URL are recorded.
git_url("https://github.com/username/repository_name.git")
storage_mode("git")
Additionally, you will be asked for a MATCH_PASSWORD during the command. You can set this to anything you like, but make sure to note it down as you will need to set it in the CI later.
Next, retrieve the created certificate with the following command:
fastlane match appstore --readonly
By specifying readonly, you can retrieve the certificate. This allows one owner to create the certificate once, and the remaining members can use that same certificate, ensuring everyone uses a common one.
The location of the downloaded certificate and provisioning profile is displayed in the fastlane match execution log, and you can import them by double-clicking the files.
Open Xcode and verify that the certificate and provisioning profile can be set. It is also recommended to turn off Auto Signing.
Preparation of the certificate is now complete.
If the fastlane command fails or if you need more detailed information, the following articles provide excellent references:
- Managing iOS Certificates using fastlane match
- Creating and Updating Certificates using fastlane match
Also, if you prefer certificate management without using match, this article might be helpful:
Implement a lane for TestFlight in fastlane
Prepare the fastlane lane to be executed by GitHub Actions. The source code will look like this:
lane :beta do
# 1. Prepare Keychain
setup_ci(provider: "travis")
# 2. Prepare App Store Connect API
api_key = app_store_connect_api_key(
key_id: ENV['ASC_KEY_ID'],
issuer_id: ENV['ASC_ISSUER_ID'],
key_content: ENV['ASC_KEY_CONTENT'],
in_house: false
)
# 3. Prepare GitHub Access
username = ENV['USERNAME']
personal_github_access_token = ENV["PERSONAL_GITHUB_ACCESS_TOKEN"]
authorization_token_str = "#{username}:#{personal_github_access_token}"
basic_authorization_token = Base64.strict_encode64(authorization_token_str)
# 4. Install certificates using fastlane match
match(
git_basic_authorization:basic_authorization_token,
api_key: api_key,
app_identifier: 'com.example.myapp',
type: "appstore",
readonly: is_ci
)
# Increment build number
increment_build_number(xcodeproj: "MyApp.xcodeproj")
# Build the app
build_app(
workspace: "MyApp.xcworkspace",
scheme: "MyApp",
export_options: {
method: "app-store",
}
)
# 5. Distribute via TestFlight
upload_to_testflight(
api_key: api_key,
skip_waiting_for_build_processing: true
)
end
Here are some additional explanations.
1. Prepare Keychain
setup_ci(provider: "travis")
It might seem strange to specify Travis suddenly, but this was written based on a method discussed in a fastlane Issue. Since it internally prepares the keychain, other methods might also work.
By the way, without this line, the build will stop at '[CP] Embed Pods Frameworks' and will not proceed.
2. Prepare App Store Connect API
api_key = app_store_connect_api_key(
key_id: ENV['ASC_KEY_ID'],
issuer_id: ENV['ASC_ISSUER_ID'],
key_content: ENV['ASC_KEY_CONTENT'],
in_house: false
)
In the past, when submitting with fastlane, there were many cumbersome parts, such as needing to set FASTLANE_SESSION every time to bypass two-factor authentication on CI.
Currently, fastlane supports the App Store Connect API, and by using it, you can achieve this simply without needing to update FASTLANE_SESSION. For details, please refer to the link below:
To issue an API Key, the following three pieces of information are required:
ASC_KEY_IDASC_ISSUER_IDASC_KEY_CONTENT
The method to prepare these is very easy to understand with screenshots in the following article by moga:
Generate them from App Store Connect. Make sure to note them down as you will set them in GitHub Secrets later.
3. Prepare GitHub Access
username = ENV['USERNAME']
personal_github_access_token = ENV["PERSONAL_GITHUB_ACCESS_TOKEN"]
authorization_token_str = "#{username}:#{personal_github_access_token}"
basic_authorization_token = Base64.strict_encode64(authorization_token_str)
In fastlane match, it is necessary to access another repository to download certificates. Here, we prepare the authentication information to access that other repository.
Here, USERNAME is your GitHub account name, and PERSONAL_GITHUB_ACCESS_TOKEN is the PERSONAL ACCESS TOKEN that can be generated on GitHub. (Click here for how to issue a PERSONAL ACCESS TOKEN)
4. Install certificates using fastlane match
match(
git_basic_authorization: basic_authorization_token,
api_key: api_key,
app_identifier: 'com.example.myapp',
type: "appstore",
readonly: is_ci
)
Download certificates and provisioning profiles using fastlane match.
Since we do not want to create certificates from CI, we use it in read-only mode by specifying readonly: is_ci.
5. Distribute via TestFlight
upload_to_testflight(
api_key: api_key,
skip_waiting_for_build_processing: true
)
Distribute the built app via TestFlight. Previously, FASTLANE_SESSION was required, but with the method using api_key, it is no longer necessary because it can bypass 2FA.
GitHub Actions charges fees based on usage time. To save time as much as possible, we include skip_waiting_for_build_processing: true to finish the process without waiting for the build upload to complete.
This completes the preparation for fastlane.
Configure GitHub Actions
Finally, let's enable the fastlane lane we just prepared to be executed from GitHub Actions.
The GitHub Actions workflow is configured as follows:
name: Upload TestFlight
on:
workflow_dispatch:
push:
branches:
- master
jobs:
build:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- name: Select Xcode version
run: sudo xcode-select -s '/Applications/Xcode_12.3.app/Contents/Developer'
- name: Show Xcode version
run: xcodebuild -version
- name: Bundle Install
run: bundle install
- uses: actions/cache@v2
with:
path: Pods
key: ${{ runner.os }}-pods-${{ hashFiles('**/Podfile.lock') }}
restore-keys: |
${{ runner.os }}-pods-
- name: Pod Install
if: steps.cache-cocoapods.outputs.cache-hit != 'true'
run: pod install
- name: Upload a new build to App Store Connect
env:
ASC_KEY_ID: ${{ secrets.ASC_KEY_ID }}
ASC_ISSUER_ID: ${{ secrets.ASC_ISSUER_ID }}
ASC_KEY_CONTENT: ${{ secrets.ASC_KEY_CONTENT }}
MATCH_PASSWORD: ${{ secrets.MATCH_PASSWORD }}
PERSONAL_GITHUB_ACCESS_TOKEN: ${{ secrets.PERSONAL_GITHUB_ACCESS_TOKEN }}
USERNAME: ${{ secrets.USERNAME }}
run: bundle exec fastlane beta
Starting with the selection of the Xcode version and the installation of libraries managed by CocoaPods, the last step executes the lane we prepared.
Set the environment variables prepared in the previous steps in GitHub Secrets, and describe them in the env section.
Setting Secrets on GitHub
Since the trigger is set as follows, TestFlight will be distributed automatically upon manual execution (workflow_dispatch) or pushing to the master branch.
on:
workflow_dispatch:
push:
branches:
- master
Conclusion
I have set up TestFlight automation using fastlane + App Store Connect API + GitHub Actions.
Running GitHub Actions on macOS consumes 10 times the minutes compared to Linux. Be careful with the costs as heavy usage can quickly exceed the free tier.
This configuration should be sufficiently convenient for scenarios where TestFlight is needed as required.
Discussion