Skip to content

Commit

Permalink
add cr_buildstep_targets #155
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkEdmondson1234 committed Dec 19, 2021
1 parent d6d1b6e commit c123179
Show file tree
Hide file tree
Showing 26 changed files with 230 additions and 73 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@
^data-raw$
^NEWS\.txt$
^\.github$
^\.lintr$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ _targets.yaml
tests/testthat/targets
tests/testthat/_targets
tests/testthat/_targets_cloudbuild
cloudbuild_targets.yaml
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ export(cr_buildstep_r)
export(cr_buildstep_run)
export(cr_buildstep_secret)
export(cr_buildstep_slack)
export(cr_buildstep_targets)
export(cr_buildstep_targets_setup)
export(cr_buildstep_targets_teardown)
export(cr_buildtrigger)
export(cr_buildtrigger_copy)
export(cr_buildtrigger_delete)
Expand Down
152 changes: 105 additions & 47 deletions R/build_targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
#'
#' \itemize{
#' \item Create your `targets` workflow.
#' \item Create a Dockerfile that holds the R and system dependencies for your workflow. You can test the image using \link{cr_deploy_docker()}. Include \code{library(targets)} dependencies - a Docker image with \code{targets} installed is available at \code{gcr.io/gcer-public/targets}.
#' \item Run \code{cr_build_targets()} to create the cloudbuild yaml file.
#' \item Create a Dockerfile that holds the R and system dependencies for your workflow. You can test the image using \link{cr_deploy_docker}. Include \code{library(targets)} dependencies - a Docker image with \code{targets} installed is available at \code{gcr.io/gcer-public/targets}.
#' \item Run \code{cr_build_targets} to create the cloudbuild yaml file.
#' \item Run the build via \link{cr_build} or similar. Each build should only recompute outdated targets.
#' \item Optionally create a build trigger via \link{cr_buildtrigger}.
#' \item Trigger a build. The first trigger will run the targets pipeline, subsequent runs will only recompute the outdated targets.
#' }
#'
#' @return A Yaml object as generated by \link{cr_build_yaml}
#' @param path File path to write the Google Cloud Build yaml workflow file. Set to NULL to write no file and just return the \link{Yaml} object.
#' @param path File path to write the Google Cloud Build yaml workflow file. Set to NULL to write no file and just return the \code{Yaml} object.
#' @param task_image An existing Docker image that will be used to run your targets workflow after the targets meta has been downloaded from Google Cloud Storage
#' @param target_folder Where target metadata will sit within the Google Cloud Storage bucket as a folder. Defaults to RStudio project name.
#' @param target_folder Where target metadata will sit within the Google Cloud Storage bucket as a folder. If NULL defaults to RStudio project name or "targets_cloudbuild" if no RStudio project found.
#' @param bucket The Google Cloud Storage bucket the target metadata will be saved to in folder `target_folder`
#' @param ... Other arguments passed to \link{cr_build_yaml()}
#' @param ... Other arguments passed to \link{cr_build_yaml}
#' @inheritDotParams cr_build_yaml
#' @param task_args A named list of additional arguments to send to \link{cr_buildstep_r()} when its executing the \link[targets]{tar_make()} command (such as environment arguments)
#' @param tar_make The R script that will run in the tar_make() step. Modify to include custom settings such as "script"
#' @param task_args A named list of additional arguments to send to \link{cr_buildstep_r} when its executing the \link[targets]{tar_make} command (such as environment arguments)
#' @param tar_make The R script that will run in the \code{tar_make()} step. Modify to include custom settings
#'
#' @examples
#'
Expand All @@ -34,64 +34,45 @@
#' # adding custom environment args and secrets to the build
#' cr_build_targets(
#' task_image = "gcr.io/my-project/my-targets-pipeline",
#' target_folder = "my_targets_project",
#' options = list(env = c(
#' "ENV1=1234",
#' "ENV_USER=Dave"
#' )),
#' availableSecrets = cr_build_yaml_secrets("MY_PW", "my-pw"),
#' task_args = list(secretEnv = "MY_PW")
#' )
#' @seealso \link{cr_buildstep_targets} if you want to customise the build
cr_build_targets <- function(task_image = "gcr.io/gcer-public/targets",
target_folder = basename(rstudioapi::getActiveProject()),
target_folder = NULL,
path = "cloudbuild_targets.yaml",
bucket = cr_bucket_get(),
task_args = list(),
tar_make = "targets::tar_make()",
...) {
if (is.null(target_folder)) {
target_folder <- "targets_cloudbuild"


if(is.null(target_folder)) {
target_folder <- tryCatch(
basename(rstudioapi::getActiveProject()),
error = function(err){
NULL
}
)
if(is.null(target_folder)){
target_folder <- "targets_cloudbuild"
}
}

myMessage(sprintf("targets cloud location: gs://%s/%s", bucket, target_folder),
level = 3
)

bs <- c(
cr_buildstep_bash(
"mkdir /workspace/_targets && mkdir /workspace/_targets/meta && gsutil -m cp -r ${_TARGET_BUCKET}/_targets/meta /workspace/_targets || exit 0",
name = "gcr.io/google.com/cloudsdktool/cloud-sdk:alpine",
entrypoint = "bash",
escape_dollar = FALSE,
id = "get previous _targets metadata"
),
do.call(
cr_buildstep_r,
args = c(
task_args,
list(
r = tar_make,
name = task_image,
id = "target pipeline"
)
)
),
cr_buildstep_bash(
"date > buildtime.txt && gsutil cp buildtime.txt ${_TARGET_BUCKET}/_targets/buildtime.txt",
name = "gcr.io/google.com/cloudsdktool/cloud-sdk:alpine",
entrypoint = "bash",
escape_dollar = FALSE,
id = "Ensure ${_TARGET_BUCKET}/_targets/ always exists"
),
cr_buildstep_gcloud(
"gsutil",
args = c("-m", "cp", "-r", "/workspace/_targets", "${_TARGET_BUCKET}"),
id = "Upload Artifacts this way as artifacts doesn't support folders"
),
cr_buildstep_gcloud(
"gsutil",
args = c("ls", "-r", "${_TARGET_BUCKET}"),
id = "Artifacts location"
)
cr_buildstep_targets_setup("${_TARGET_BUCKET}"),
cr_buildstep_targets(task_args = task_args,
tar_make = tar_make,
task_image = task_image),
cr_buildstep_targets_teardown("${_TARGET_BUCKET}")
)

# gs://bucket-name/target-folder
Expand Down Expand Up @@ -174,7 +155,7 @@ cr_build_targets_artifacts <- function(build,
download_folder,
{
lapply(arts$name, function(x) {
gcs_get_object(x,
googleCloudStorageR::gcs_get_object(x,
bucket = bb,
saveToDisk = x,
overwrite = overwrite
Expand All @@ -185,3 +166,80 @@ cr_build_targets_artifacts <- function(build,

normalizePath(file.path(download_folder, build_folder))
}


#' Buildstep to run a targets pipeline on Cloud Build
#'
#' This is a buildstep to help upload a targets pipeline, see \link{cr_build_targets} for examples and suggested workflow
#' @export
#' @param task_args A named list of additional arguments to send to \link{cr_buildstep_r} when its executing the \link[targets]{tar_make} command (such as environment arguments)
#' @param tar_make The R script that will run in the \code{tar_make()} step. Modify to include custom settings
#' @param task_image An existing Docker image that will be used to run your targets workflow after the targets meta has been downloaded from Google Cloud Storage
#' @family Cloud Buildsteps
cr_buildstep_targets <- function(
task_args = list(),
tar_make = "targets::tar_make()",
task_image = "gcr.io/gcer-public/targets"){

do.call(
cr_buildstep_r,
args = c(
task_args,
list(
r = tar_make,
name = task_image,
id = "target pipeline"
)
)
)

}

#' @export
#' @rdname cr_buildstep_targets
#' @param bucket The Google Cloud Storage bucket and folder the target metadata will be saved to, e.g. \code{gs://my-bucket/my_target_project} You can also pass in build substitution variables such as \code{"${_MY_BUCKET}"}.
cr_buildstep_targets_setup <- function(bucket){
cr_buildstep_bash(
bash_script = paste(
c("mkdir /workspace/_targets &&",
"mkdir /workspace/_targets/meta &&",
"gsutil -m cp -r",
sprintf("%s/_targets/meta",bucket),
"/workspace/_targets",
"|| exit 0"), collapse = " "),
name = "gcr.io/google.com/cloudsdktool/cloud-sdk:alpine",
entrypoint = "bash",
escape_dollar = FALSE,
id = "get previous _targets metadata"
)
}

#' @export
#' @rdname cr_buildstep_targets
cr_buildstep_targets_teardown <- function(bucket = cr_bucket_get()){
c(
cr_buildstep_bash(
bash_script = paste(
c(
"date > buildtime.txt &&",
"gsutil cp buildtime.txt",
sprintf("%s/_targets/buildtime.txt", bucket)
), collapse = " "),
name = "gcr.io/google.com/cloudsdktool/cloud-sdk:alpine",
entrypoint = "bash",
escape_dollar = FALSE,
id = "Ensure bucket/_targets/ always exists"
),
cr_buildstep_bash(
bash_script = paste(
c("gsutil -m cp -r", "/workspace/_targets", bucket,
"&& gsutil ls -r", bucket),
collapse = " "),
name = "gcr.io/google.com/cloudsdktool/cloud-sdk:alpine",
entrypoint = "bash",
escape_dollar = FALSE,
id = "Upload Artifacts"
)
)
}

1 change: 0 additions & 1 deletion R/buildstep_templates_git.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ cr_buildstep_git <- function(git_args = c(
#'
#' Use \code{git_volume} to add the git credentials folder to other buildsteps
#'
#' @examples
git_volume <- function() {
list(list(
name = "ssh",
Expand Down
2 changes: 1 addition & 1 deletion R/buildsteps_templates.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#' @export
#'
#' @seealso \url{https://docs.codecov.io/reference#upload}
#'
#' @family Cloud Buildsteps
#' @examples
#'
#' cr_buildstep_packagetests()
Expand Down
18 changes: 10 additions & 8 deletions man/cr_build_targets.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/cr_buildstep.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/cr_buildstep_bash.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/cr_buildstep_decrypt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/cr_buildstep_df.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/cr_buildstep_docker.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/cr_buildstep_edit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/cr_buildstep_extract.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c123179

Please sign in to comment.