If you just want a one-off Docker image, use cr_deploy_docker() or make your own build via cr_buildstep_docker()

If you want the Docker image to rebuild each git commit, then you also need a build trigger. This can be enabled using cr_deploy_docker_trigger()

The below example builds this package’s Dockerfile upon each commit, for a Dockerfile located in the cloud_build/ folder.

repo <- cr_buildtrigger_repo("MarkEdmondson1234/googleCloudRunner")
cr_deploy_docker_trigger(repo, "test3", dir = "cloud_build")

Kaniko cache

A common workflow is to make lots of repeat builds of Docker images as you update the files and libraries within the Dockerfile. If this is a heavy operation it can take 20mins+ to build the image.

If it is a long build, consider using the kaniko cache option in your Docker builds. This caches each step of the Dockerfile so only those that are new are built, and can considerably speed up Docker build times.

To use, select the kamiko_cache=TRUE option in your builds:

cr_deploy_docker("my_folder", kaniko_cache=TRUE)

Combining buildsteps with other builds

It may be you want the build to do other things after the Docker image is built such as use that Docker image somewhere. You can extract the buildsteps from deployed buildtriggers to combine them and avoid having two builds.

For example, say you have deployed a Dockerfile trigger:


repo <- cr_buildtrigger_repo("your-github/your-repo")

# first time - by default will be called "docker-{image}"
cr_deploy_docker_trigger(repo, image = "my-build")

If you want to extract the docker buildsteps you can find it in dock_build$build$steps below:

# get the buildtrigger details
dock_build <- cr_buildtrigger_get("docker-my-build")

# contains the buildsteps from the deployment

You can then combine those buildsteps in the usual way with other buildsteps. The example below assumes you’ve made an R docker image with some of your custom dependencies that you then want to immediately run your R code within:

# uses the docker image previously created
my_r_step <- cr_buildstep_r(
  r = "my_r_code.R",
  name = "docker-my-build"

# combine the buildsteps
new_buildsteps <- c(dock_build$build$steps, my_r_step)

# remake the build yaml
yml <- cr_build_yaml(new_buildsteps)

You can then reconfigure the build trigger with your new build yaml. The below writes the yaml to a file to be read by the build trigger:

# write it out to the git repo we are in
cr_build_write(yml, "new_buildsteps.yml")

repo <- cr_buildtrigger_repo("your-github/your-repo")

# overwrite the deployed build trigger to read the yml
  name = "docker-my-build",
  trigger = repo,
  overwrite = TRUE