Helper for creating build steps for upload to Cloud Build

cr_buildstep(
  name,
  args = NULL,
  id = NULL,
  prefix = "gcr.io/cloud-builders/",
  entrypoint = NULL,
  dir = "",
  env = NULL,
  waitFor = NULL,
  volumes = NULL,
  secretEnv = NULL
)

Arguments

name

name of docker image to call appended to prefix

args

character vector of arguments

id

Optional id for the step

prefix

prefixed to name - set to "" to suppress. Will be suppressed if name starts with gcr.io or *-docker.pkg.dev

entrypoint

change the entrypoint for the docker container

dir

The directory to use, relative to /workspace e.g. /workspace/deploy/

env

Environment variables for this step. A character vector for each assignment

waitFor

Whether to wait for previous buildsteps to complete before running. Default it will wait for previous step.

volumes

volumes to connect and write to

secretEnv

A list of secrets stored in Secret Manager referred to in args via a $$var

Details

This uses R to make building steps for cloudbuild.yml files harder to make mistakes with, and also means you can program creation of cloud build steps for use in R or other languages. Various templates with common use cases of buildsteps are also available that wrap this function, refer to the "See Also" section.

WaitFor

By default each buildstep waits for the previous, but if you pass "-" then it will start immediately, or if you pass in a list of ids it will wait for previous buildsteps to finish who have that id. See Configuring Build Step Order for details.

Build Macros

Fields can include the following variables, which will be expanded when the build is created:-

  • $PROJECT_ID: the project ID of the build.

  • $BUILD_ID: the autogenerated ID of the build.

  • $REPO_NAME: the source repository name specified by RepoSource.

  • $BRANCH_NAME: the branch name specified by RepoSource.

  • $TAG_NAME: the tag name specified by RepoSource.

  • $REVISION_ID or $COMMIT_SHA: the commit SHA specified by RepoSource or resolved from the specified branch or tag.

  • $SHORT_SHA: first 7 characters of $REVISION_ID or $COMMIT_SHA.

Or you can add your own custom variables, set in the Build Trigger. Custom variables always start with $_ e.g. $_MY_VAR

secretEnv

You can pass secrets that are stored in Secret Manager directly instead of using a dedicated buildstep via cr_buildstep_secret

Within the code passed to args those secrets are referred to via $$SECRET_NAME. If used then cr_build_yaml must also include the availableSecrets argument.

Examples

cr_project_set("my-project")
#>  2022-03-26 19:55:48 > ProjectId set to my-project
#> [1] "my-project"
cr_bucket_set("my-bucket")
#>  2022-03-26 19:55:48 > Bucket set to my-bucket
#> [1] "my-bucket"
# creating yaml for use in deploying cloud run
image <- "gcr.io/my-project/my-image:$BUILD_ID"
cr_build_yaml(
  steps = c(
    cr_buildstep("docker", c("build", "-t", image, ".")),
    cr_buildstep("docker", c("push", image)),
    cr_buildstep("gcloud", c(
      "beta", "run", "deploy", "test1",
      "--image", image
    ))
  ),
  images = image
)
#> ==cloudRunnerYaml==
#> steps:
#> - name: gcr.io/cloud-builders/docker
#>   args:
#>   - build
#>   - -t
#>   - gcr.io/my-project/my-image:$BUILD_ID
#>   - '.'
#> - name: gcr.io/cloud-builders/docker
#>   args:
#>   - push
#>   - gcr.io/my-project/my-image:$BUILD_ID
#> - name: gcr.io/cloud-builders/gcloud
#>   args:
#>   - beta
#>   - run
#>   - deploy
#>   - test1
#>   - --image
#>   - gcr.io/my-project/my-image:$BUILD_ID
#> images:
#> - gcr.io/my-project/my-image:$BUILD_ID

# use premade docker buildstep - combine using c()
image <- "gcr.io/my-project/my-image"
cr_build_yaml(
  steps = c(
    cr_buildstep_docker(image),
    cr_buildstep("gcloud",
      args = c(
        "beta", "run", "deploy",
        "test1", "--image", image
      )
    )
  ),
  images = image
)
#> ==cloudRunnerYaml==
#> steps:
#> - name: gcr.io/cloud-builders/docker
#>   args:
#>   - build
#>   - -f
#>   - Dockerfile
#>   - --tag
#>   - gcr.io/my-project/my-image:latest
#>   - --tag
#>   - gcr.io/my-project/my-image:$BUILD_ID
#>   - '.'
#>   id: building image
#> - name: gcr.io/cloud-builders/docker
#>   args:
#>   - push
#>   - gcr.io/my-project/my-image
#>   id: pushing image
#> - name: gcr.io/cloud-builders/gcloud
#>   args:
#>   - beta
#>   - run
#>   - deploy
#>   - test1
#>   - --image
#>   - gcr.io/my-project/my-image
#> images:
#> - gcr.io/my-project/my-image

# list files with a new entrypoint for gcloud
cr_build_yaml(steps = cr_buildstep("gcloud", c("-c", "ls -la"),
  entrypoint = "bash"
))
#> ==cloudRunnerYaml==
#> steps:
#> - name: gcr.io/cloud-builders/gcloud
#>   entrypoint: bash
#>   args:
#>   - -c
#>   - ls -la

# to call from images not using gcr.io/cloud-builders stem
cr_buildstep("alpine", c("-c", "ls -la"), entrypoint = "bash", prefix = "")
#> [[1]]
#> ==cloudRunnerBuildStep==
#> name: alpine
#> entrypoint: bash
#> args:
#> - -c
#> - ls -la
#> 

# to add environment arguments to the step
cr_buildstep("docker", "version", env = c("ENV1=env1", "ENV2=$PROJECT_ID"))
#> [[1]]
#> ==cloudRunnerBuildStep==
#> name: gcr.io/cloud-builders/docker
#> args:
#> - version
#> env:
#> - ENV1=env1
#> - ENV2=$PROJECT_ID
#> 

# to add volumes wrap in list()
cr_buildstep("test", "ls", volumes = list(list(name = "ssh", path = "/root/.ssh")))
#> [[1]]
#> ==cloudRunnerBuildStep==
#> name: gcr.io/cloud-builders/test
#> args:
#> - ls
#> volumes:
#> - name: ssh
#>   path: /root/.ssh
#>