Continually build, test and deliver updates for your app to your users helps you get new features faster and with less release bugs. In an earlier blog post "Automatically build your Android app with AWS codebuild", Adrian Hall explains how you can use AWS CodeBuild to compile an Android app and publish the APK on Amazon S3.
In this blog post we add a continuous integration and continuous delivery (CI / CD) pipeline using AWS CloudFormation, AWS CodePipeline, AWS CodeBuild and AWS Device Farm. After you complete the steps in this blog, you have a working pipeline that lets you build your app, test it on real physical devices, and deliver the packaged APK file to an S3 bucket every time a change is made and pushed to the source code repository.
Create an Android project with Android Studio
Let's start creating a new project with Android Studio. If you already have a project that you want to use, you can skip this step. Otherwise, follow the steps on the Android Studio website to create a project. When you select the devices you want to target, select Phone and tablet . By default, a module with the name & # 39; app & # 39; made.
Create the CI / CD pipeline using AWS CloudFormation
Our pipeline uses multiple AWS services. We use an AWS CloudFormation template to automatically make all of these resources available to us. The pipeline consists of the following:
- A codecommit repository to store the code of the Android app.
- A codebuild project for compiling, packaging and delivering the Android app.
- A Device Farm project to test the Android app on real physical devices.
- An S3 bucket to store building artifacts.
- A code pipeline pipeline to check for changes to the codecommit repository. Then it triggers a build using CodeBuild and tests with Device Darm.
- AWS Lambda functions for calling the Device Farm test from CodePipeline, and also for providing the Device Darm project from AWS CloudFormation
Create the CloudFormation stack by following these steps:
- Open the AWS CloudFormation console.
- Click the Create Stack button.
- Download the Cloudformation template from https://github.com/aws-samples/aws-code-snippets/blob/master/CloudFormation/DeviceFarm.yaml .
- Select Upload a template to Amazon S3 then click on Choose File and select the template you downloaded in the previous step
- Type a stack name (for example myapp-cicd-pipeline ).
- Check the default parameters and then click Next . (Note that if the app module does not have the name & # 39; app & # 39;, you need to change the AppModuleName parameter).
- (Optional) Set all the options you need in Options and then click Next .
- Select I acknowledge that AWS CloudFormation can create IAM resources. (The stack uses IAM roles for Lambda, CodeBuild and CodePipeline.)
- Click the button .
AWS CloudFormation makes you stack. After it is completed, the stack is in the & # 39; CREATE_COMPLETE & # 39; Click the stack in the list of CloudFormation stacks, and then click the Outputs tab in the bottom half of the screen. The stack performs two values that we use in the next steps. They are CodeRepoCloneUrlHttp which contains the Git URL to which we push the code, and OutputApkUrl which contains the URL of the most recently built and tested APK file.
Configure the project to use CodeCommit
If your development environment has not yet been configured for AWS CodeCommit, you must follow the setup steps in the [AWSCodeCommitUserGuide to install the Git client, grant CodeCommit privileges to your IAM user, and set the embedding help. You can skip step 4 because we configure an existing project to use the Git repository.
To configure your Android project for use with CodeCommit, you must add the CodeCommit repository created by the CloudFormation stack as a Git remote control. Use the output value CodeRepoCloneUrlHttp from the previous step and execute the following command from the workspace of your project:
Print the code in CodeCommit
We are now ready to send the source code for your Android app to the codecommit repository. If you do this, an execution of the code pipeline pipeline will start. A few minutes after you have pushed the code, you have a fully tested APK file in the output S3 bucket.
From the workspace of your Android app you have to check if all files have been checked in to the Git repository.
Add all files in the workspace of your app:
Make the changes to your local Git repository:
Push the changes in the CodeCommit repository:
View the build
To view the progress of CodePipeline implementation, you must open the CodePipeline console. Locate the pipeline with the stack name of the CloudFormation stack that you created as a prefix and click on the name. From here you can view the progress of the execution of the pipeline.
The execution goes through four stages (Source, Build, Test and Deliver).
- Source : the newest code comes from the CodeCommit repository and is packaged as a ZIP file for use in other phases.
- Build : CodePipeline triggers a build CodeBuild to compile and package the app as an APK file.
- Test : CodePipeline calls a Lambda function that uploads the APK file to Device Farm and performs a test on real devices.
- Deliver : the built-in and tested APK file is delivered in the specified S3 bucket
You can view the test results from Device Farm from the Device Farm console. Click on the project named demo-app-devicefarm if you used the default settings when you created the stack. Otherwise, click the name of the project that you specified in the CloudFormation stack parameters. On the Automated Testing tab, a test is displayed if it is currently running or has been completed. If you do not see a test, the test phase may not yet have been carried out by CodePipeline. After the test is complete, you can view the results and also screenshots of the test.
The final stage of the pipeline is to deliver the artefact to Amazon S3. Open the Amazon S3 console and find the artefact bucket. As the prefix of your stack & # 39; myapp-cicd-pipeline & # 39; is, the artifact bucket has a prefix of & # 39; myapp-cicd-pipeline-artifactbucket- & # 39 ;. After the pipeline has completed the execution and the build and test phases have been successfully completed, an APK file is delivered to this S3 bucket.
The stack output from CloudFormation has an output with a key named OutputApkUrl . The value contains a URL that you can use to download the APK file directly. You can also use the URL to install the APK file on an Android device.
We have automated building, testing and delivery for an Android app using AWS services. With a little work you can change the AWS CloudFormation template to also sign the app with your certificate and deliver it directly to the Google Play Store. To automate delivery to the Google Play Store, you can use tools such as fastlane. You can call these tools from CodeBuild.
About the author
This post was contributed by Vinay Selvaraj. Vinay is a consultant with AWS Professional Services that specializes in helping clients migrate to the cloud. You can contact him via Twitter at @vinayselvaraj.