Building and testing iOS and iPadOS apps with AWS DevOps and mobile services
Continuous integration/continuous deployment (CI/CD) helps automate software delivery processes. With the software delivery process automated, developers can test and deliver features faster. In iOS app development, testing your apps on real devices allows you to understand how users will interact with your app and to detect potential issues in real time.
AWS has a collection of tools designed to help developers build, test, configure, and release cloud-based applications for mobile devices. This blog post shows you how to leverage some of those tools and integrate third-party build tools like Jenkins into a CI/CD Pipeline in AWS for iOS app development and testing.
A new commit to the source repository triggers the pipeline. The build is done on a Jenkins server, and the build artifact from Jenkins is passed to the test phase, which is configured with AWS Device Farm to test the application on real devices. AWS CodePipeline provides the orchestration and helps automate the build and test phases. The CodePipeline continuous delivery process is illustrated in the following screenshot.
Figure: CodePipeline Continuous Delivery Architecture
Ensure you have the following prerequisites set up before beginning:
- Apple developer account
- Build server (macOS)
- Xcode Version 11.3 (installed on the build server and setup)
- Jenkins (installed on the build server)
- AWS CodePipeline Jenkins Plugin installed
- AWS CLI installed and configured on workstation
- Basic knowledge of Git
Jenkins can be installed on macOS using a homebrew package manager for macOS with the following command:
$ brew install Jenkins
Start Jenkins by typing the following command:
You can also configure Jenkins to start as a service on startup with the following command:
$ brew services start Jenkins
On a browser on your local machine, visit http://localhost:8080. You should see the setup screen shown in the following screenshot:
1. Grab the initial admin password from the terminal by typing:
$ cat /Users/administrator/.jenkins/secrets/initialAdminPassword
2. Follow the onscreen instructions to complete setup. This includes creating a first admin user, installing initial plugins, etc.
3. Make some changes to the config file to ensure Jenkins is accessible from anywhere, not just the local machine:
- Open the config file:
$ sudo nano /Users/admin/Library/LaunchAgents/homebrew.mxcl.jenkins.plist
- Find the following line:
- Change it to the following:
- Save your changes and exit.
To reach Jenkins from the internet, enter the following into a web browser:
The default Jenkins port is 8080. For example, if a public IP address 188.8.131.52, the path is 184.108.40.206:8080.
4. Install the AWS CodePipeline Jenkins plugin:
- Sign in to Jenkins using the user name and password you created. Choose Manage Jenkins, then Manage Plugins.
- Switch to the Available tab and start typing CodePipeline into the filter until AWS CodePipeline Plugin appears. Select the plugin, then select Install without restart.
- Select Restart Jenkins when installation is complete and no jobs are running.
5. Create a project. Choose New Item, then Freestyle Project. Enter a descriptive name. This example uses
iosapp as the item name.
6. In the Source Code Management section, select AWS CodePipeline and configure the plugin as shown in the following screenshot.
- AWS region: The region in which you want to create the CI/CD pipeline.
- AWS access key and AWS secret key: Create a special IAM user and apply the
AWSCodePipelineCustomActionAccessmanaged policy to that user. Use the access credentials for that user to configure this section.
- Category: Choose Build. This is also used in the pipeline configuration.
- Provider: This example uses the name Jenkins. It can be renamed, but take note of the name specified here.
- Version: Enter 1 here. This value is used in the pipeline configuration.
7. Under Build Triggers, select Poll SCM. Enter the schedule * * * * * separated by spaces, as shown in the following screenshot.
8. Under Build, select Add build step, then Execute shell. Enter the following commands, inserting your development team ID.
/usr/bin/xcodebuild build-for-testing -scheme MyNotes -destination generic/platform=iOS DEVELOPMENT_TEAM=<your development team ID> -allowProvisioningUpdates -derivedDataPath /Users/admin/.jenkins/workspace/iosapp
mkdir Payload && cp -r /Users/admin/.jenkins/workspace/iosapp/Build/Products/Debug-iphoneos/MyNotes.app Payload/
zip -r Payload.zip Payload && mv Payload.zip MyNotes.ipa
9. Under Post-build Actions, select Add post-build action, then AWS CodePipeline Publisher. Fill in the fields as shown in the following screenshot:
10. Save the configuration.
11. Retrieve the public IP address for the macOS build server.
Configure Device Farm
In this section, you configure Device Farm to test the sample iOS app on real-world devices.
- Navigate to the AWS Device Farm Console
- Choose Create a new project and enter a name for the project. Choose Create project. Note the name of the project.
- Choose the newly created project and retrieve the project ID:
- Copy the URL found in the browser into a text editor.
- Note the project ID, which can be found in the URL path:
https://us-west-2.console.aws.amazon.com/devicefarm/home?region=us-east-1#/projects/<your project ID is here>/runs
- Decide on which devices you want to test the sample app. This is known as the device pool in Device Farm. This example doesn’t use a PRIVATE device pool. It uses a CURATED device pool, which is a device pool created and managed by AWS Device Farm.
- Retrieve the ARN of the CURATED device pool for your project using the AWS CLI:
$ aws devicefarm list-device-pools --arn arn::devicefarm:us-west-2:<account-id>:project:<project id noted above> --region us-west-2 --query 'devicePools[?name==`Top Devices`]'
Note the device pool ARN.
Configure the CodeCommit repository
In this section, the source code repository is created and source code is pushed to the repository.
- Create a CodeCommit repository. Take note of the repository name.
- Connect to the newly created repository.
- Push the iOS app code from the local repository to the remote CodeCommit repository:
$ git push
Create and configure CodePipeline
CodePipeline orchestrates all phases of the example. Each action is represented as a stage.
Since you have a Jenkins stage, which is considered a custom action and has to be configured via the AWS management console, use the AWS management console to create your pipeline.
- Go to the AWS CodePipeline console and choose Create pipeline.
iosappunder Pipeline settings and select New service role.
- Leave the default Role name, and select Allow AWS CodePipeline to create a service role so it can be used with this new pipeline.
- Choose Next.
- Select AWS CodeCommit as the Source provider. Select the repository you created and the branch name, then select Next.
- Select Add Jenkins as the build provider and fill in the fields:
- Provider name: Specify the provider name you configured for this example.
- Server URL: Specify the public IP address of the Jenkins server and the port on which Jenkins is. For example, if 220.127.116.11 is the IP address and 8080 is the port, the server URL is http://18.104.22.168:8080.
- Project name: Specify the name you gave to the Jenkins Freestyle project you created.
- Choose Next.
- Choose Skip deploy stage. You are integrating with Device Farm and this is only valid as a test stage, not a deploy stage.
- Choose Create pipeline. This creates a two-stage pipeline which starts executing immediately after creation. However, you are not done yet, so stop the current execution
- Now create a
teststage with Device Farm. Choose Edit to modify the pipeline. Under the Build stage, select Add Stage and enter a stage name (such as Test). Choose Add stage again.
- In the newly added stage, choose Add action group and fill in the fields:
- Action name: Enter an Action name
- Action Provider: Select AWS Device Farm
- Region: Select US West – Oregon.
“AWS Device Farm is only supported in US-West-2 (Oregon) so this action will be a cross region action since the pipeline is in us-east-1”
- Input artifacts: Select BuildArtifact, which is the output of the Jenkins build stage
- ProjectId: This is the Device Farm project ID you noted earlier
- DevicePoolArn: This is the Device Farm ARN you noted earlier
- AppType: Enter
- App: This is the file that contains the app to test; the filename of your generated IPA is
- TestType: This is the type of test to run on the application; enter
- Leave the other fields blank and choose Done to save the action configuration, then choose Save to save the pipeline changes.
- Optionally, you can enable notifications to notify you of changes in the pipeline, such as when the pipeline completes, when a stage or action completes, or when there is a failure. To enable notifications, create a notification rule.
- Choose Release change to execute the pipeline, as shown in the following screenshot.
Verify the test on Device Farm
From the pipeline execution, you can see there is a failure in your test. Check the test results:
- Navigate to the AWS Device Farm Console.
- Select the project you created.
- All the tests that have run are listed, as seen in the following screenshot.
- Choose the test to see more details.
You can see the source of the failure. To investigate why the test failed, choose each device. The device names on which the app was tested are also shown, such as the OS version and the total duration of the test for each device. You can see screenshots of the test by switching to the Screenshots tab. More information can also be seen by clicking on a device.
Troubleshoot the failure by examining the result in each of the devices on which the test was run to determine what changes are needed in the application. After making the needed changes in the application source code, push the changes to the remote repository (in this case, a CodeCommit repository) to trigger the pipeline again. The following screenshot shows a successful pipeline execution:
The following screenshot shows a successful test:
Cleanup the following AWS resources:
- CodeCommit repository – Delete The CodeCommit repository
- CodePipeline – Delete the Pipeline
- Device Farm project – Delete Device Farm Project
This post showed you how to integrate CodePipeline with an iOS Jenkins build server and leverage the integration of CodePipeline and Device Farm to automatically build and test iOS apps on real-world devices. By taking this approach to testing iOS apps, you can visualize how an app will behave on actual devices and with the automated CI/CD pipeline, and quickly test apps as they are developed.