Building Artifacts on the Cloud

Software developers often build artifacts (Docker containers, WAR files, APK bundles etc.) from codes.

This often requires reliable internet connection, high processing power, sufficient storage space, constant upgrade of build tools among others.

Built artifacts could be published e.g. deploying containers to the cloud or hosting APK files on Play store.
However, this also includes steps that might be difficult when done manually or on a local machine.

In this tutorial, you will leverage Cloud Build to build artifacts extremely fast on the Cloud using the following examples:

  • Building container artifacts and storing on Container Registry.
  • Building non-container artifacts and storing on Cloud Storage.
  • Automating builds from repositories using Cloud Build Triggers.

Cloud Build

Cloud Build is a service that executes your builds on Google Cloud Platform infrastructure.

You can write build configurations to provide instructions to Cloud Build on what tasks to perform and can either use the build step/builders provided by Cloud Build , Cloud Build community or write your own custom build steps/builders .

Cloud Build offers the first 120 build minutes per day for free.
You can simple get started by enabling Cloud Build for your GCP project .

Before you begin, ensure you have setup Cloud SDK or Cloud Shell.

Building Docker containers to Container Registry

Cloud Build allows you to build Docker images and push the built images to Container Registry.

You can get my sample source codes for a Containerized Flask Application here and Dockerfile below:

FROM python:3.7-stretch
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential
COPY . /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD [""]

There should be a Dockerfile on your project root directory and then you can run the following command to Build on the Cloud which also publishes your Docker container to Container Registry.

gcloud builds submit --tag[PROJECT_ID]/my-image .

You’ve just built a Docker image named my-image using a Dockerfile and pushed the image to Container Registry.

You can also build on the Cloud using build configuration file cloudbuild.yaml which is created on the project root directory with content as follows:

- name: ''
args: [ 'build', '-t', '[PROJECT_ID]/my-image', '.' ]
- '[PROJECT_ID]/my-image'

Then trigger the build using the following command:

gcloud builds submit --config cloudbuild.yaml .

Note that:

  • [PROJECT_ID] should be replaced with your actual project ID.
  • . is used to the current working directory for build time.
  • You can specify other tags for your image using : e.g. my-image:dev and also pass a longer timeout for larger images e.g. --timeout=600s

Building non-containers artifacts to Cloud Storage

Cloud Build supports builders that you can reference to execute specific tasks such as npm, go, gradle, gradle etc.

However, I would use a custom gradle builder to build an Android project APK and then enable Cloud Build to store the artifacts to a Cloud Storage Bucket.

You can get my sample source codes for the Android application here and modified build configuration file cloudbuild.yaml below:

# Set a persistent volume according to (search for volumes)
- name: 'ubuntu'
- name: 'vol1'
path: '/persistent_volume'
args: ['cp', '-a', '.', '/persistent_volume']

# Build APK with Gradle Image from mounted /persistent_volume using name: vol1
- name: ''
- name: 'vol1'
path: '/persistent_volume'
args: ['run', '-v', 'vol1:/home/app', '--rm', '', '/bin/sh', '-c', 'cd /home/app && ./gradlew clean assembleDebug']
# Push the APK file from vol1 to your GCS Bucket.
location: 'gs://[STORAGE_LOCATION]/'
paths: ['*.apk']
timeout: 1200s

Then trigger the build using the following command:

gcloud builds submit --config cloudbuild.yaml .

Note that:

  • [STORAGE_LOCATION] represents the path to your Cloud Storage bucket.
  • is the gradle builder
  • Cloud Build service account should have access to write to Cloud Storage OR that the Bucket is publicly open.

Automating builds

Cloud Build can import source code from Google Cloud Storage, Cloud Source Repositories, GitHub, or Bitbucket, execute a build to your specifications, and produce artifacts.

We’ll proceed to create a trigger that listens to changes on a particular branch on our source codes and performs the operation in our cloudbuild.yaml configuration file.

  • Open the Cloud Build Triggers page
  • Click on Connect repository
  • Select your Source Code option and Continue.
  • Authenticate and Create Push Trigger with required inputs.

Above is a sample Cloud Build Trigger page.

You can monitor Build Triggers on the History page

If you would like some insights on where to deploy your artifacts, check out Google Cloud Compute Options Guide .

Additional Resources on Cloud Build




A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Recommended from Medium

Most digital projects are set up for failure


Hibernate 2nd level cache with HazelCast, Spring Boot and Spring Data JPA


Don’t dip your toes in serverless — you have to dive right in

When you LoopBack, it all connects — Rebel Connect

Building a Continuous Delivery flow for your Infrastructure as Code.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Software / DevOps Engineer | Google Developer Expert for Cloud |

More from Medium

Tutorial 4: Setup Web Application Security Protection and Detection Lab in Google Cloud — Data…

GCP Storage Transfer Service Job using AWS ARN

SFTP using CyberDuck via IAP Tunnel in Google Cloud Platform

Get started with Google Cloud Platform