Creating a basic build configuration file
This page describes how to create a build configuration file that you can use to start a build on Cloud Build.
A build config file defines the fields that are needed for Cloud Build to perform your tasks. You'll need a build config file if you're starting builds using the gcloud command-line tool or build triggers. You can write the build config file using the YAML or the JSON syntax.
Before you begin
- Read Build Configuration Overview to understand all the fields you can include in a build config file.
Creating a build config
The following steps explain how to create a simple build config file. Each of the fields in the config file defines a part of the task you want to perform. The only required field in the build config file is the name field for a step; all other fields are optional.
Create the build config file. In your project root directory, create a file named
cloudbuild.yaml. This is your Cloud Build build config file.Add the steps field. The
stepssection in the build config file contains the build steps that you want Cloud Build to perform.Add the first step. Under
steps:, add anamefield and point it to a container image to execute your task. Cloud Build and its developer community provides several container images with common tools and languages installed in them. You can use any of these images (also called Cloud Builders), or any publicly available image in a build step. For information on different types of container images you can use in a build step, see Cloud Builders.The following snippet shows a build step with a
dockerbuildergcr.io/cloud-builders/docker, which is a container image running Docker.Add step arguments. The
argsfield of a step takes a list of arguments and passes them to the builder referenced by thenamefield. If the builder in thenamefield has an entrypoint,argsin the list will be used to access that entrypoint. If the builder in thenamefield does not have an entrypoint, the first element inargswill be used as the entrypoint.In the following example:
buildis the entrypoint to the Docker cloud builder-tis the Docker taggcr.io/my-project/my-imageis the name of the image to be built.is the location of the source code
Include any additional fields for the step. You can add more fields to your build step such as environment variables and working directories, to configure your build steps. For a description of all the fields you can include in a build step, see Build steps.
In the following example, the
timeoutfield is included to specify that thedockerstep must be timed-out after 500s:Add more steps. You can add any number of build steps to your build config file by including additional
namefields and pointing them to cloud builders.The following snippet includes two more steps to the build config file:
- a docker build step to invoke the
docker pushcommand, to push the image build in the previous step to Container Registry. a kubectl build step to invoke the
kubectl set imagecommand, which deploys the image to a Kubernetes Engine cluster. Theenvfield is included to specify the Compute Engine zone and Kubernetes Engine cluster.
- a docker build step to invoke the
Include additional build configuration. You can configure the build further by including fields such as
machineTypeandtags. For the complete list of fields you can include in the build config file see Build Configuration Overview.The following example adds the following fields for the build:
machineTypethat specifies the virtual machine size to run the build.timeoutthat specifies the time after which the build will be timed out.tagsfor annotating the build.
Store the built images and artifacts. If your build produces any container images, you can choose to store them in Container Registry. You can do this using the
imagesfield.The following example stores the image built in the docker step to Container Registry:
If your build produces any non-container artifacts, you can store them in Cloud Storage using the
artifactsfield. For instructions on doing this, see Storing Images and Artifacts.
What's next
- Learn how to run your builds manually and using triggers using the build config file.
- Learn how to write build configs for including dependencies, and building, testing, and deploying artifacts.