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
steps
section in the build config file contains the build steps that you want Cloud Build to perform.Add the first step. Under
steps:
, add aname
field 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
docker
buildergcr.io/cloud-builders/docker
, which is a container image running Docker.Add step arguments. The
args
field of a step takes a list of arguments and passes them to the builder referenced by thename
field. If the builder in thename
field has an entrypoint,args
in the list will be used to access that entrypoint. If the builder in thename
field does not have an entrypoint, the first element inargs
will be used as the entrypoint.In the following example:
build
is the entrypoint to the Docker cloud builder-t
is the Docker taggcr.io/my-project/my-image
is 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
timeout
field is included to specify that thedocker
step 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
name
fields 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 push
command, to push the image build in the previous step to Container Registry. a kubectl build step to invoke the
kubectl set image
command, which deploys the image to a Kubernetes Engine cluster. Theenv
field 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
machineType
andtags
. 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:
machineType
that specifies the virtual machine size to run the build.timeout
that specifies the time after which the build will be timed out.tags
for 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
images
field.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
artifacts
field. 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.