Set up a Cloud Build trigger that builds your Android app and uploads it to a Cloud Storage bucket.
Contributed by the Google Cloud community. Not official Google documentation.
In this tutorial, you set up a Cloud Build trigger that builds your Android application and uploads it to a Cloud Storage bucket. The builds of new APK bundles are automatically triggered after code is pushed to your repository.
In the list of members, look for your Cloud Build service account named
[PROJECT_ID]@cloudbuild.gserviceaccount.com, where [PROJECT_ID] is your GCP project ID.
Click the pencil icon in that row.
Click Add another role, select Storage Object Admin under Storage, and click Save.
Set up Cloud Build
You need to ensure that your application code and all necessary files needed for building an APK are available in a code repository. Cloud Build currently supports Cloud Source Repositories, GitHub, and Bitbucket. You can also make use of the sample code for this tutorial, here.
In your repository, create a build configuration file, cloudbuild.yaml, which contains instructions for Cloud Build. The configuration file for this tutorial is as follows:
# Set a persistent volume according to https://cloud.google.com/cloud-build/docs/build-config (search for volumes)
- name: 'ubuntu'
- name: 'vol1'
args: ['cp', '-a', '.', '/persistent_volume']
# Build APK with Gradle Image from mounted /persistent_volume using name: vol1
In the command, we specify -v, which mounts our current directory as the volume, and --rm, which removes the container on exit.
You can change the -c command on your cloudbuild.yaml file if you would like to use other Gradle commands.
Cloud Build also copies the output app-debug.apk into your Cloud Storage bucket as app-debug-$SHORT_SHA.apk, where $SHORT_SHA is the first seven characters of COMMIT_SHA of the commit that triggered Cloud Build; it is meant to tag the APK builds in your GCS bucket.
Set up a Cloud Build trigger
A Cloud Build trigger listens to changes in your code repository. Follow the steps below to create a Cloud Build trigger: