Integrating with Cloud Build

This page describes how to configure Cloud Build to store built artifacts in a Artifact Registry repository.

Before you begin

  1. If the target repository does not exist, create a new repository.
  2. The default Cloud Build service account has permissions to upload and download from repositories in the same project. If Cloud Build is in a different project than your repositories, you must grant permissions to the Cloud Build service account in the project with the repositories.

Configuring a Docker build

After you have granted permissions to the target repository, you are ready to configure Docker to authenticate and push a package to the repository. Integration using the Cloud Build service account is recommended.

To configure Docker:

  1. Set up authentication for Docker.

  2. In your build config file, add the step to build and push the images.

    The following example uses the following values for an image that is in the same directory as the build config file:

    • The repository location us-central1
    • The project ID of the build, resolved with the default substitution $PROJECT_ID
    • The repository name my-repo
    steps:
    - name: 'gcr.io/cloud-builders/docker'
      args: [ 'build', '-t', 'us-central1-docker.pkg.dev/$PROJECT_ID/${_REPOSITORY}/${_IMAGE}', '.' ]
    images:
    - 'us-central1-docker.pkg.dev/$PROJECT_ID/${_REPOSITORY}/${_IMAGE}'
    

    The variables ${_REPOSITORY} and ${_IMAGE} are used for the repository name and the name of the image in the repository so that they can be replaced at build time using custom Cloud Build substitutions.

    This approach is useful if you want to use the same build config file to push images to repositories for different environments, such as testing, staging, or production.

    For example, this command would substitute my-repo for the repository name and my-image for the image name.

    gcloud builds submit --config=cloudbuild.yaml \
      --substitutions=_REPOSITORY="my-repo",_IMAGE="my-image" .
    

Configuring a Maven build

After you have granted permissions to the target repository, you are ready to configure Maven to authenticate and push a package to the repository. Integration using the Cloud Build service account is recommended.

To configure Maven:

  1. Set up authentication for Maven. Ensure that you specify the correct target project and repository in your pom.xml file.

  2. In your Cloud Build build config file, add the step to upload the package with Maven:

    steps:
    - name: gcr.io/cloud-builders/mvn
      args: ['deploy']
    

Configuring an npm build

After you have granted permissions to the target repository, you are ready to configure npm to authenticate and push a package to the repository. Integration using the Cloud Build service account is recommended.

To configure npm:

  1. Set up authentication for npm. Ensure that you specify the correct target project and repository in your .npmrc file.

  2. Place the .npmrc file in the same directory as your Cloud Build build config file so that Cloud Build can find your authentication settings.

  3. In your build config file, add the step to upload the package to the repository.

    steps:
    - name: gcr.io/cloud-builders/npm
      args: ['publish', ${_PACKAGE}]
    

    The variable ${_PACKAGE} represents the package. You replace this custom Cloud Build substitution at build time with location of the package, the folder or a gzipped tarball that contains package.json and the other files in your package.

    For example, this command uploads a package in a directory named src that is in the same directory as the .npmrc file and the build config file.

    gcloud builds submit --config=cloudbuild.yaml --substitutions=_PACKAGE="src" .