Powered By GitBook
cloudbuild-test-runner
Learn how to use Cloud Build as a test runner.
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:
1
gcloud builds submit -t gcr.io/my-project/my-image .
Copied!
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:
1
FROM alpine
2
​
3
RUN mkdir -p /opt/bin
4
ENV PATH=${PATH}:/opt/bin:/opt/gcloud/google-cloud-sdk/bin
5
​
6
# Install PHP and tools
7
RUN apk add php5 php5-openssl php5-json php5-phar php5-dom php5-bcmath wget \
8
ca-certificates coreutils unzip --update && \
9
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
10
php -r "if (hash_file('SHA384', 'composer-setup.php') === rtrim(file_get_contents('https://composer.github.io/installer.sig'))) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" && \
11
php composer-setup.php --filename=composer --install-dir=/opt/bin && \
12
php -r "unlink('composer-setup.php');" && \
13
wget -nv https://phar.phpunit.de/phpunit.phar && \
14
chmod +x phpunit.phar && \
15
mv phpunit.phar /opt/bin/phpunit && \
16
apk del wget unzip && \
17
rm -rf /var/cache/apk/*
18
​
19
COPY run_tests.sh /run_tests.sh
20
ENTRYPOINT ["/run_tests.sh"]
Copied!
The second file is run_tests.sh:
1
#!/bin/bash
2
set -ex
3
​
4
if [ "$#" -eq 0 ]; then
5
TEST_DIR='/workspace'
6
else
7
TEST_DIR=${1}
8
fi
9
​
10
cd ${TEST_DIR}
11
​
12
if [ -f composer.json ]; then
13
composer install
14
fi
15
​
16
phpunit
Copied!
Dockerfile installs php5, composer, and phpunit, then has the run_test.sh as the ENTRYPOINT.
run_tests.sh basically runs composer install and phpunit.
You have already built and pushed this image to gcr.io/cloud-dpes/phpunit-test-runner with the following command:
1
gcloud builds submit -t gcr.io/cloud-dpes/phpunit-test-runner .
Copied!

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.
1
steps:
2
- name: gcr.io/cloud-dpes/phpunit-test-runner
Copied!

Travis configuration file

Here is an excerpt from .travis.yml:
1
install:
2
- php scripts/dump_credentials.php
3
- scripts/install_gcloud.sh
4
​
5
script:
6
- pushd php
7
- gcloud container builds submit --config=cloudbuild.yaml .
8
- popd
Copied!
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 TRAVIS.md 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.
Last modified 6mo ago