|
| 1 | +#' Add deployment CI for GitHub Actions |
| 2 | +#' |
| 3 | +#' Creates a minimal GitHub Actions workflow for deploying a `{golem}` app to |
| 4 | +#' Posit Connect via `{rsconnect}`. If needed, this function also creates a |
| 5 | +#' root `app.R` and `.rscignore` by calling [add_positconnect_file()]. The |
| 6 | +#' generated Posit Connect entrypoint uses `{pkgload}`, so `{pkgload}` is added |
| 7 | +#' to `DESCRIPTION`. |
| 8 | +#' |
| 9 | +#' @inheritParams add_module |
| 10 | +#' |
| 11 | +#' @export |
| 12 | +#' |
| 13 | +#' @return The path to the created workflow, invisibly. |
| 14 | +add_github_action <- function( |
| 15 | + golem_wd = get_golem_wd(), |
| 16 | + open = TRUE |
| 17 | +) { |
| 18 | + add_deploy_ci_( |
| 19 | + template = "github-action-template.yml", |
| 20 | + output = fs_path( |
| 21 | + golem_wd, |
| 22 | + ".github", |
| 23 | + "workflows", |
| 24 | + "shiny-deploy.yaml" |
| 25 | + ), |
| 26 | + golem_wd = golem_wd, |
| 27 | + open = open |
| 28 | + ) |
| 29 | +} |
| 30 | + |
| 31 | +#' Add deployment CI for GitLab |
| 32 | +#' |
| 33 | +#' Creates a minimal GitLab CI file for deploying a `{golem}` app to Posit |
| 34 | +#' Connect via `{rsconnect}`. If needed, this function also creates a root |
| 35 | +#' `app.R` and `.rscignore` by calling [add_positconnect_file()]. The |
| 36 | +#' generated Posit Connect entrypoint uses `{pkgload}`, so `{pkgload}` is added |
| 37 | +#' to `DESCRIPTION`. |
| 38 | +#' |
| 39 | +#' @inheritParams add_module |
| 40 | +#' |
| 41 | +#' @export |
| 42 | +#' |
| 43 | +#' @return The path to the created GitLab CI file, invisibly. |
| 44 | +add_gitlab_ci <- function(golem_wd = get_golem_wd(), open = TRUE) { |
| 45 | + add_deploy_ci_( |
| 46 | + template = "gitlab-ci-template.yml", |
| 47 | + output = fs_path(golem_wd, ".gitlab-ci.yml"), |
| 48 | + golem_wd = golem_wd, |
| 49 | + open = open |
| 50 | + ) |
| 51 | +} |
| 52 | + |
| 53 | +#' @noRd |
| 54 | +add_deploy_ci_ <- function( |
| 55 | + template, |
| 56 | + output, |
| 57 | + golem_wd = get_golem_wd(), |
| 58 | + open = TRUE |
| 59 | +) { |
| 60 | + golem_wd <- fs_path_abs(golem_wd) |
| 61 | + |
| 62 | + ensure_deploy_entrypoint_(golem_wd = golem_wd) |
| 63 | + ensure_deploy_dependencies_(golem_wd = golem_wd) |
| 64 | + |
| 65 | + if (fs_file_exists(output)) { |
| 66 | + cli_alert_info(sprintf("The '%s'-file already exists.", basename(output))) |
| 67 | + return(open_or_go_to(output, open)) |
| 68 | + } |
| 69 | + |
| 70 | + fs_dir_create(dirname(output), recurse = TRUE) |
| 71 | + |
| 72 | + writeLines( |
| 73 | + render_ci_template_(template = template, golem_wd = golem_wd), |
| 74 | + con = output |
| 75 | + ) |
| 76 | + |
| 77 | + if (basename(output) == "shiny-deploy.yaml") { |
| 78 | + ensure_github_gitignore_(golem_wd = golem_wd) |
| 79 | + usethis_use_build_ignore(".github") |
| 80 | + } else { |
| 81 | + usethis_use_build_ignore(".gitlab-ci.yml") |
| 82 | + } |
| 83 | + |
| 84 | + cat_created(output) |
| 85 | + open_or_go_to(output, open) |
| 86 | +} |
| 87 | + |
| 88 | +#' @noRd |
| 89 | +render_ci_template_ <- function(template, golem_wd = get_golem_wd()) { |
| 90 | + app_name <- get_golem_name(golem_wd = golem_wd) |
| 91 | + |
| 92 | + template_lines <- readLines( |
| 93 | + golem_sys("utils", template), |
| 94 | + warn = FALSE |
| 95 | + ) |
| 96 | + |
| 97 | + gsub("__APPNAME__", app_name, template_lines, fixed = TRUE) |
| 98 | +} |
| 99 | + |
| 100 | +#' @noRd |
| 101 | +ensure_deploy_entrypoint_ <- function(golem_wd = get_golem_wd()) { |
| 102 | + app_file <- fs_path(golem_wd, "app.R") |
| 103 | + rscignore_file <- fs_path(golem_wd, ".rscignore") |
| 104 | + |
| 105 | + if (!fs_file_exists(app_file)) { |
| 106 | + add_positconnect_file(golem_wd = golem_wd, open = FALSE) |
| 107 | + return(invisible(golem_wd)) |
| 108 | + } |
| 109 | + |
| 110 | + if (!fs_file_exists(rscignore_file)) { |
| 111 | + add_rscignore_file(golem_wd = golem_wd, open = FALSE) |
| 112 | + } |
| 113 | + |
| 114 | + return(invisible(golem_wd)) |
| 115 | +} |
| 116 | + |
| 117 | +#' @noRd |
| 118 | +ensure_deploy_dependencies_ <- function(golem_wd = get_golem_wd()) { |
| 119 | + desc_file <- fs_path(golem_wd, "DESCRIPTION") |
| 120 | + deps <- desc_get_deps(file = desc_file) |
| 121 | + has_pkgload <- any( |
| 122 | + deps$package == "pkgload" & |
| 123 | + deps$type %in% c("Depends", "Imports") |
| 124 | + ) |
| 125 | + |
| 126 | + if (!has_pkgload) { |
| 127 | + desc_set_dep("pkgload", type = "Imports", file = desc_file) |
| 128 | + } |
| 129 | + |
| 130 | + return(invisible(golem_wd)) |
| 131 | +} |
| 132 | + |
| 133 | +#' @noRd |
| 134 | +ensure_github_gitignore_ <- function(golem_wd = get_golem_wd()) { |
| 135 | + where <- fs_path(golem_wd, ".github", ".gitignore") |
| 136 | + |
| 137 | + if (!fs_file_exists(where)) { |
| 138 | + writeLines("*.html", con = where) |
| 139 | + return(invisible(where)) |
| 140 | + } |
| 141 | + |
| 142 | + content <- readLines(where, warn = FALSE) |
| 143 | + |
| 144 | + if (!"*.html" %in% content) { |
| 145 | + writeLines( |
| 146 | + c(content, "*.html"), |
| 147 | + con = where |
| 148 | + ) |
| 149 | + } |
| 150 | + |
| 151 | + return(invisible(where)) |
| 152 | +} |
0 commit comments