Creating a Serverless Mobile Delivery Pipeline

This tutorial describes how to create a serverless mobile delivery pipeline in Google Cloud Platform (GCP):

  • First, it walks you through setting up a test Android application that you can build and distribute using Gradle commands on your local machine. These commands build the Android application package (APK) and upload it to Beta by Crashlytics for distribution using the mobile distribution platform from Fabric for Firebase.

  • Next, to automate this process, you create a semi-custom Docker image for building Android applications with Cloud Build, referred to here as Android Builder. To complete the automation, the tutorial walks you through triggering the Android Builder with changes to the Android application's source code.

Objectives

  • Create a build task that builds a signed APK.
  • Create a build task that uses Crashlytics to distribute new builds.
  • Create a custom Docker image (the Android Builder) that has the required Android SDK dependencies.
  • Create a build trigger in GCP that uses the Android Builder Docker image to build the signed APK and distribute it using Fabric Crashlytics.

Costs

This tutorial uses billable components of GCP, including:

The base Android Builder image is about 1.9 GB. The Pricing Calculator estimates the cost of storage of 2 GB environment at around $.05 per month. Building the base Android Builder image takes about 11 minutes and costs $.03. Each additional build should take about 5 minutes and cost $.02 See Cloud Build pricing for more information.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Select or create a GCP project.

    Go to the Manage resources page

  3. Asegúrate de tener habilitada la facturación para tu proyecto.

    Aprende a habilitar la facturación

  4. Enable the Compute Engine API.

    Enable the API

  5. Install and initialize the Cloud SDK.
  6. If you don't already have a Fabric account, sign up for one. Fabric uses this account to distribute pre-release (beta) versions of your Android application.
  7. Make sure you have Gradle installed on your computer.
  8. Make sure you have Android Studio installed on your computer.
  9. Make sure that Git is installed on your computer.

This tutorial assumes the following:

  • You're running macOS or Linux. Some of these procedures could be adapted to a Windows environment, but the tutorial does not provide complete instructions for that use case.
  • You have some familiarity with Android development, Android Studio, and Gradle and Git.

Get the signed Android beta build working locally

In this section, you set up a test Android application in Android Studio. The application can be built and distributed using Gradle build tasks run in the terminal window of your computer.

Create a test Android application to build

  1. In Android Studio, create a new Android Project that uses an empty activity.

    The following screenshots show the steps in the wizard for performing this task.

    Android Studio wizard, page 1

    Android Studio wizard, page 2

    Android Studio wizard, page 3

    Android Studio wizard, page 4

  2. From the terminal window on your computer, change to the directory where you created your test application:

    cd ${HOME}/android-apps/TestAndroidBuild

  3. In that directory, initialize a Git repository:

    git init

  4. Edit the .gitignore file and add the names of files and extensions that Git should not push to your source code repository:

    cat >> .gitignore <<EOF
    *.jks
    fabric.properties
    keystore.properties
    EOF

  5. Commit the application to the repository:

    git add -A
    git commit -m "empty application"

  6. Configure the gcloud tool to use your project. For [PROJECT_NAME], use the project name that you selected or created.

    PROJECT = [PROJECT_NAME]
    gcloud config configurations create [PROJECT_NAME]
    gcloud config config set project [PROJECT_NAME]

  7. Create a cloud source repository for your Android application:

    gcloud source repos create android-application

  8. Add the cloud source repository as a remote repository for your Android application:

    git remote add google \
        https://source.developers.google.com/p/${PROJECT}/r/android-application

Create the signing key and keystore

To distribute the pre-release Android application, you must sign the application using a certificate stored in a Java keystore (.jks) file.

  1. In the terminal window, change to the parent directory of your application:

    cd ${HOME}/AndroidStudioProjects/TestAndroidBuild

  2. Create the Java keystore and key to sign the application, and create the keystore.properties file that stores the key alias, password, and relative path for the keystore file. Use this command:

    JKS_PASSWORD=$(openssl rand -base64 12)
    keytool -genkey -noprompt -keystore android.jks \
      -alias android-key \
      -dname "CN=example.com, OU=IT, O=Example, L=Sunnyvale, S=California, C=US" \
      -storepass ${JKS_PASSWORD} \
      -keypass ${JKS_PASSWORD}

    cat > keystore.properties <<EOF storeFile=../android.jks #relative to the app/ directory storePassword=${JKS_PASSWORD} keyPassword=${JKS_PASSWORD} keyAlias=android-key EOF

  3. Update the ${HOME}/AndroidStudioProjects/TestAndroidBuild/app/build.gradle file with the application signing properties.

    After your edits, the Gradle file looks similar to the following (changes are shown in bold):

    def betaKeystorePropertiesFile = rootProject.file("keystore.properties")
    def keystoreProperties = new Properties()
    keystoreProperties.load(new FileInputStream(betaKeystorePropertiesFile));

    android { ... defaultConfig {...} signingConfigs { beta { keyAlias keystoreProperties['keyAlias'] keyPassword keystoreProperties['keyPassword'] storeFile file(keystoreProperties['storeFile']) storePassword keystoreProperties['storePassword'] } } buildTypes { release {...} beta { initWith release signingConfig signingConfigs.beta ext.betaDistributionGroupAliases="testers" minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }

Add Fabric for beta distribution

  1. Install the Fabric Android Studio plugin.

  2. In Android Studio, click the Fabric icon and follow the instructions from the Fabric plugin to install Crashlytics.

    icon in Android Studio for installing Beta

  3. In Fabric, set up a beta email distribution group:

    1. In the browser, log in to your Fabric account.

    2. On the left side of the screen, click Beta.

      getting to the beta product

    3. Click Manage Groups, and then click New Group.

    4. For the group name, enter testers.

    5. In the Add Testers text box, enter one or more email addresses.

      set up 'testers' as group

    6. Close the window.

Build and distribute the application from your computer

In the terminal window, make sure you're at the top-level directory for your test application, and then enter the following:

gradle assembleBeta crashlyticsUploadDistributionBeta

An email is sent to the distribution group with a link that allows the user to install the application.

Create your Android build in Cloud Build

At this point, you have working Gradle build tasks that build and distribute your Android application. In this section, you set up Cloud Build to run these build tasks whenever there's a change to the master branch of the Cloud Source Repository.

Upload application signing and distribution secrets

The build and distribution tasks use the certificate that's stored in a Java keystore (.jks) file to sign the application, and a Fabric API key to distribute the application using Crashlytics. You should not put credentials in source code repositories. Therefore, you upload these credentials to a Cloud Storage bucket so that the Android Builder can download them when it builds and distributes the application.

  1. In the terminal window, change to the top-level directory of your Android application project:

    cd ${HOME}/AndroidStudioProjects/TestAndroidBuild

  2. Create a Cloud Storage bucket for the Android signing keys. For [ANDROID_SIGNING_BUCKET], use a unique name. For more information, see Bucket and Object Naming Guidelines.

    gsutil mb gs://[ANDROID_SIGNING_BUCKET]

  3. Copy the .jks distribution files to that bucket:

    gsutil cp *.jks gs://[ANDROID_SIGNING_BUCKET]

  4. Create a Cloud Storage bucket for the Fabric keys. For [FABRIC_KEYS_BUCKET], use a unique name.

    gsutil mb gs://[FABRIC_KEYS_BUCKET]

  5. Copy the fabric.properties file to that bucket:

    gsutil cp app/fabric.properties gs://[FABRIC_KEYS_BUCKET]

  6. Create the storage bucket that the Android Builder will use to save its dependencies between builds:

    gsutil mb gs://[ANDROID_BUILD_CACHE]

Create the Android Builder

  1. In a terminal window, clone your the community builders repository to a directory outside of your Android application's directory:

    git clone https://github.com/GoogleCloudPlatform/cloud-builders-community

    The GitHub repository contains the community maintained builders.

  2. Change to the directory that contains the Android Builder:

    cd cloud-builders-community/android

  3. Create a list of the Android SDK packages you need for your builds and save it to the builder directory as packages.txt. In the file, put each package on its own line.

    • Option 1. To generate the list from your computer, use this script:

      ./installed-package-list.sh

      This script adds every package from the computer it is running on to the packages.txt file. As a result, the generated packages.txt file often includes items that you don't need for your build container. Including components you don't need increases both builder and application build times. Therefore, it's recommended that after you generate the packages.txt file, you edit it to include only the SDK components that you need.

    • Option 2. Create the packages.txt file manually. The following example shows what the file might look like.

      extras;m2repository;com;android;support;constraint;constraint-layout;1.0.2
      extras;google;googleplayservices
      extras;android;gapid;3
      extras;google;auto
      extras;android;m2repository
      extras;google;webdriver
      platform-tools
      platforms;android-26

  4. In the terminal window, save your Android SDK license to a shell variable:

    ANDROID_SDK_LICENSE= \
        $(tail -1 [ANDROID_SDK_HOME]/licenses/android-sdk-license)

    Replace [ANDROID_SDK_HOME] with the appropriate location for your environment. On a Mac, [ANDROID_SDK_HOME] is typically ~/Library/Android/sdk. On Linux, it's typically ~/Android/Sdk.

  5. Create the Android Builder in your project:

    gcloud builds submit --config cloudbuild.yaml . \
        --substitutions=_ANDROID_SDK_LICENSE=$ANDROID_SDK_LICENSE

Create the Android application build trigger

In this section, you create the Application build trigger that uses the Android Builder from the previous section to build and distribute the Android application.

  1. In the terminal window, copy the example cloudbuild.yaml file from the Android base image fork to your test Android application directory:

    cd ${HOME}/android-apps/TestAndroidBuild
    cp ${HOME}/cloud-builders-community/android
    android-cloud-build-example/fabric-beta-dist-cloudbuild.yaml ./cloudbuild.yaml

  2. In the GCP Console, go to the Build Triggers page:

    Go to the Build Triggers page

  3. Click Add trigger.

  4. Select your repository hosting option.
  5. Select the repository that you want to use to trigger your builds.
  6. Enter a name for the build trigger, such as my Android build.
  7. In the Branch (regex) box, enter master. For this tutorial, you trigger new application builds whenever code is merged to the master branch. For details on how you can add build triggers to suit your needs, see the Cloud Build docs.
  8. Under Build configuration, select cloudbuild.yaml.
  9. Under cloudbuild.yaml location, enter /cloudbuild.yaml.
  10. Click Add item. This lets you set variables for the build.
  11. For each variable in the following table, enter the variable name (including the leading underscore), and then the value from your project.

    Variable Value Purpose
    _ANDROID_SIGNING_BUCKET_PATH The URL of the bucket that you created for the .jks and keystore.properties files.

    gs://[ANDROID_SIGNING_BUCKET]
    Makes the Android signing credentials available to the Android build in Cloud Build.
    _BUILD_TYPE Beta Specifies the Gradle build type to use for building the application.
    _FABRIC_API_SECRET_BUCKET_PATH The URL of the bucket that contains the fabric.properties file:

    gs://[FABRIC_KEYS_BUCKET]
    Provides the Fabric API credentials to the Android build.
    _EMAIL_DISTRIBUTION_GROUP The name of the email distribution group that you created within the Fabric UI for distributing beta versions. Selects a list of email addresses to which Fabric will distribute the beta version of the application.
    _ANDROID_BUILD_CACHE The name of a Cloud Storage bucket that you use to store Gradle dependencies between builds. Prevents Gradle from downloading the same dependencies from the internet with each build.
    _ANDROID_SDK_LICENSE Your Android SDK license Verifies that you've accepted the terms of the Android SDK license.

Execute the application build trigger

You can now test that the application build trigger works and confirm that the application is distributed to your email.

  1. In a terminal window, change directories to the top-level directory of your test Android application project:

    cd ${HOME}/android-apps/TestAndroidBuild

  2. To trigger the build, push to the master branch of the source code repository

    git push google master

  3. In the GCP Console, go to the Cloud Build page.

  4. Click the latest build in progress to see the build output for the application build.

Cleaning up

After you've finished this tutorial, you should clean up the resources that you created on GCP so you won't be billed for them in the future. The following sections describe how to delete or turn off these resources.

Delete the project

The easiest way to eliminate billing is to delete the project you created for the tutorial.

To delete the project:

  1. In the GCP Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click
      Delete project
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Delete image registries

Delete the image registries for your Android SDK image and Android application image.

  1. In the left navigation menu of the GCP Console, scroll to the Tools section and click Container Registry > Images.
  2. Select all the images.

    Selecting all the images

  3. Click Delete.

Delete Cloud Storage buckets

Delete the Cloud Storage buckets that contain the Android signing information and the Fabric properties. In Cloud Shell, use these commands, substituting your values for the placeholders:

gsutil rm gs://[ANDROID_SIGNING_BUCKET]
gsutil rm gs://[FABRIC_KEYS_BUCKET]
gsutil rm gs://[ANDROID_BUILD_CACHE]

Delete the build triggers

  1. Delete the base Android SDK build trigger.
  2. Delete the Android build trigger.

Delete the application from Fabric

  1. Log in to the Fabric dashboard and click the Fabric icon in the upper left-hand corner for the screen:

    Fabric icon

  2. Click the app tile in the dashboard:

    app tile in the Fabric dashboard

  3. Click the settings icon in the upper right-hand corner of the dashboard:

    Fabric 'settings' icon

  4. Click the Apps icon:

    Fabric 'apps' icon

  5. Click the application tile in the list:

    Fabric application tile

  6. Click the Delete app link in the upper right-hand side of the page:

    Fabric 'delete app' link

What's next

¿Te sirvió esta página? Envíanos tu opinión:

Enviar comentarios sobre…