From 53d1591aa3e6f73210352c7c652ea1d51f0b2447 Mon Sep 17 00:00:00 2001 From: Aron Atkins Date: Wed, 24 Apr 2024 14:22:42 -0400 Subject: [PATCH] default image from RSCONNECT_IMAGE (#1065) * default image from RSCONNECT_IMAGE fixes #1063 * test precedence between image and RSCONNECT_IMAGE --- NEWS.md | 3 ++ R/bundle.R | 7 +++++ R/deployApp.R | 3 +- man/deployApp.Rd | 3 +- man/writeManifest.Rd | 3 +- tests/testthat/test-writeManifest.R | 46 ++++++++++++++++++++++++++++- 6 files changed, 61 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index 301837a9..141b109e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,9 @@ * `deployApp(logLevel = "quiet")` and `writeManifest(quiet=TRUE)` suppress output when using renv to analyze dependencies. (#1051) +* `deployApp()` and `writeManifest()` receive the default value for the + `image` argument from the `RSCONNECT_IMAGE` environment variable. (#1063) + # rsconnect 1.2.2 * Use internally computed SHA1 sums and PKI signing when SHA1 is disabled diff --git a/R/bundle.R b/R/bundle.R index 23dfc48d..2806bc74 100644 --- a/R/bundle.R +++ b/R/bundle.R @@ -126,6 +126,13 @@ createAppManifest <- function(appDir, verbose = FALSE, quiet = FALSE) { + if (is.null(image)) { + imageEnv <- Sys.getenv("RSCONNECT_IMAGE", unset = NA) + if (!is.na(imageEnv) && nchar(imageEnv) > 0) { + image <- imageEnv + } + } + if (needsR(appMetadata)) { extraPackages <- inferRPackageDependencies(appMetadata) # provide package entries for all dependencies diff --git a/R/deployApp.R b/R/deployApp.R index d9f4b9c7..61f6c851 100644 --- a/R/deployApp.R +++ b/R/deployApp.R @@ -144,7 +144,8 @@ #' made. Currently has an effect only on deployments to shinyapps.io. #' @param image Optional. The name of the image to use when building and #' executing this content. If none is provided, Posit Connect will -#' attempt to choose an image based on the content requirements. +#' attempt to choose an image based on the content requirements. You can +#' override the default by setting the environment variable `RSCONNECT_IMAGE`. #' @param envManagement Optional. Should Posit Connect install R and Python #' packages for this content? (`TRUE`, `FALSE`, or `NULL`). #' The default, `NULL`, will not write any values to the bundle manifest, diff --git a/man/deployApp.Rd b/man/deployApp.Rd index 99829f6f..dd9c9d5f 100644 --- a/man/deployApp.Rd +++ b/man/deployApp.Rd @@ -183,7 +183,8 @@ made. Currently has an effect only on deployments to shinyapps.io.} \item{image}{Optional. The name of the image to use when building and executing this content. If none is provided, Posit Connect will -attempt to choose an image based on the content requirements.} +attempt to choose an image based on the content requirements. You can +override the default by setting the environment variable \code{RSCONNECT_IMAGE}.} \item{envManagement}{Optional. Should Posit Connect install R and Python packages for this content? (\code{TRUE}, \code{FALSE}, or \code{NULL}). diff --git a/man/writeManifest.Rd b/man/writeManifest.Rd index dbc4eae6..8c22674a 100644 --- a/man/writeManifest.Rd +++ b/man/writeManifest.Rd @@ -70,7 +70,8 @@ there are \code{.qmd} files in the bundle, or if there is a \item{image}{Optional. The name of the image to use when building and executing this content. If none is provided, Posit Connect will -attempt to choose an image based on the content requirements.} +attempt to choose an image based on the content requirements. You can +override the default by setting the environment variable \code{RSCONNECT_IMAGE}.} \item{envManagement}{Optional. Should Posit Connect install R and Python packages for this content? (\code{TRUE}, \code{FALSE}, or \code{NULL}). diff --git a/tests/testthat/test-writeManifest.R b/tests/testthat/test-writeManifest.R index 85c6ecd7..29ce0f80 100644 --- a/tests/testthat/test-writeManifest.R +++ b/tests/testthat/test-writeManifest.R @@ -228,7 +228,18 @@ test_that("Deploying static content with _quarto.yaml succeeds without quartoInf expect_equal(manifest$metadata$appmode, "static") }) -test_that("Sets environment.image in the manifest if one is provided", { +test_that("environment.image is not set when image is not provided", { + skip_on_cran() + + withr::local_options(renv.verbose = TRUE) + + appDir <- test_path("shinyapp-simple") + + manifest <- makeManifest(appDir) + expect_null(manifest$environment) +}) + +test_that("environment.image is set when image is provided", { skip_on_cran() withr::local_options(renv.verbose = TRUE) @@ -237,11 +248,44 @@ test_that("Sets environment.image in the manifest if one is provided", { manifest <- makeManifest(appDir, image = "rstudio/content-base:latest") expect_equal(manifest$environment$image, "rstudio/content-base:latest") +}) + +test_that("environment.image is set and uses a provided image even when RSCONNECT_IMAGE is set", { + skip_on_cran() + + withr::local_options(renv.verbose = TRUE) + withr::local_envvar(RSCONNECT_IMAGE = "rstudio/content-base:older") + + appDir <- test_path("shinyapp-simple") + + manifest <- makeManifest(appDir, image = "rstudio/content-base:latest") + expect_equal(manifest$environment$image, "rstudio/content-base:latest") +}) + +test_that("environment.image is not set when RSCONNECT_IMAGE is empty", { + skip_on_cran() + + withr::local_options(renv.verbose = TRUE) + withr::local_envvar(RSCONNECT_IMAGE = "") + + appDir <- test_path("shinyapp-simple") manifest <- makeManifest(appDir) expect_null(manifest$environment) }) +test_that("environment.image is set when RSCONNECT_IMAGE is nonempty", { + skip_on_cran() + + withr::local_options(renv.verbose = TRUE) + withr::local_envvar(RSCONNECT_IMAGE = "rstudio/content-base:latest") + + appDir <- test_path("shinyapp-simple") + + manifest <- makeManifest(appDir) + expect_equal(manifest$environment$image, "rstudio/content-base:latest") +}) + test_that("Sets environment.environment_management in the manifest if envManagement is defined", { skip_on_cran()