Stay organized with collections Save and categorize content based on your preferences.
Edit on GitHub
Report issue
Page history

Using Cloud Build as a test runner

Author(s): @tmatsuo ,   Published: 2017-02-01

Takashi Matsuo | Developer Programs Engineer | Google

Contributed by Google employees.

Cloud Build lets you create Docker container images from your source code. The Cloud SDK provides the container builds subcommand for using this service easily.

For example, here is a simple command to build a Docker image:

gcloud builds submit -t .

This command sends the files in the current directory to Google Cloud Storage, then on one of the Cloud Build VMs, fetch the source code, run Docker build, and upload the image to Container Registry.

By default, Cloud Build runs docker build command for building the image. You can also customize the build pipeline by having custom build steps described below.

Cloud Build build steps

The build pipeline consists of one or more build steps. A build step is normally defined by the name of a Docker image and a list of arguments. For more details, see the configuration file documentation.

Running tests in a build step

If you can use any arbitrary Docker image as the build step, and the source code is available, then you can run unit tests as a build step. By doing so, you always run the test with the same Docker image. You don't have to worry about environment differences on CI systems anymore.

There is a demonstration repository at cloudbuild-test-runner-example. This tutorial uses the demonstration repository as part of its instructions.

Test runner for phpunit

The test runner is in the php/test/runner subdirectory with the following two files.

The first file is Dockerfile:

FROM alpine

RUN mkdir -p /opt/bin
ENV PATH=${PATH}:/opt/bin:/opt/gcloud/google-cloud-sdk/bin

# Install PHP and tools
RUN apk add php5 php5-openssl php5-json php5-phar php5-dom php5-bcmath wget \
    ca-certificates coreutils unzip --update && \
    php -r "copy('', 'composer-setup.php');" && \
    php -r "if (hash_file('SHA384', 'composer-setup.php') === rtrim(file_get_contents(''))) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" && \
    php composer-setup.php --filename=composer --install-dir=/opt/bin && \
    php -r "unlink('composer-setup.php');" && \
    wget -nv && \
    chmod +x phpunit.phar && \
    mv phpunit.phar /opt/bin/phpunit && \
    apk del wget unzip && \
    rm -rf /var/cache/apk/*


The second file is

set -ex

if [ "$#" -eq 0 ]; then

cd ${TEST_DIR}

if [ -f composer.json ]; then
    composer install


Dockerfile installs php5, composer, and phpunit, then has the as the ENTRYPOINT. basically runs composer install and phpunit.

You have already built and pushed this image to with the following command:

gcloud builds submit -t .

Configuration file for Cloud Build

To run the tests, you need to have a configuration file to use your test runner. Here is an example cloudbuild.yaml file.

- name:

Travis configuration file

Here is an excerpt from .travis.yml:

- php scripts/dump_credentials.php
- scripts/

- pushd php
- gcloud container builds submit --config=cloudbuild.yaml .
- popd

To use the gcloud builds command, you need to install the Cloud SDK and configure it to use a service account. For more details about prerequisites, see the file in the repo.

The gcloud builds submit command in the script section actually runs our test. If test fails on the Container Builder VM, the whole test build will fail too.

Next steps

  • Learn more about the Cloud Build
  • Learn more about the Container Registry
  • Customize this tutorial to your project. For example, you may want to add another step for reporting test coverage, or you want to use junit, instead of phpunit.

Submit a tutorial

Share step-by-step guides

Submit a tutorial

Request a tutorial

Ask for community help

Submit a request

View tutorials

Search Google Cloud tutorials

View tutorials

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.