Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: shiny2docker
Title: Generate Dockerfiles for 'Shiny' Applications
Version: 0.0.2
Version: 0.0.2.9000
Authors@R:
person("Vincent", "Guyader",
email = "vincent@thinkr.fr",
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@

# shiny2docker dev version

* `set_gitlab_ci()` now accepts a `tags` parameter so you can specify one or
more GitLab runner tags to use for the CI job.

# shiny2docker 0.0.2

## New functions
Expand Down
34 changes: 29 additions & 5 deletions R/set_gitlab_ci.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
#' and push the created image to the GitLab container registry.
#'
#' @param path A character string specifying the directory where the
#' `.gitlab-ci.yml` file will be copied. If missing, the user will be prompted to use
#' the current directory.
#' `.gitlab-ci.yml` file will be copied. If missing, the user will be prompted to use
#' the current directory.
#' @param tags Optional character vector of GitLab runner tags. If provided, the
#' function will add these tags to the generated CI job so the appropriate
#' runner is selected. You can provide multiple tags.
#'
#' @return A logical value indicating whether the file was successfully copied (`TRUE`)
#' or not (`FALSE`).
Expand All @@ -16,7 +19,10 @@
#' @examples
#' # Copy the .gitlab-ci.yml file to a temporary directory
#' set_gitlab_ci(path = tempdir())
set_gitlab_ci <- function(path) {
#'
#' # Copy the file and specify runner tags
#' set_gitlab_ci(path = tempdir(), tags = c("shiny_build", "prod"))
set_gitlab_ci <- function(path, tags = NULL) {

# Check if the 'path' parameter is provided
if (missing(path)) {
Expand All @@ -37,7 +43,7 @@ set_gitlab_ci <- function(path) {
stop("Directory creation failed. Please check the path and permissions.")
}
cli::cli_alert_success("Directory created: {path}")
}
}

cli::cli_alert_info("Copying .gitlab-ci.yml file to: {path}")

Expand All @@ -49,13 +55,31 @@ set_gitlab_ci <- function(path) {
}
cli::cli_alert_info("Found source file at: {source_file}")

# Path to destination file
dest_file <- file.path(path, ".gitlab-ci.yml")

# Copy the gitlab-ci.yml file to the destination directory
success <- file.copy(
from = source_file,
to = file.path(path, ".gitlab-ci.yml"),
to = dest_file,
overwrite = TRUE
)

# If copy succeeded and tags supplied, add them to the job
if (isTRUE(success) && !is.null(tags)) {
cli::cli_alert_info("Adding runner tags to .gitlab-ci.yml: {paste(tags, collapse = ', ')}")
yaml_lines <- readLines(dest_file)
stage_line <- grep("^\\s*stage:", yaml_lines)[1]
if (length(stage_line) == 1 && !is.na(stage_line)) {
tag_lines <- c(" tags:", paste0(" - ", tags))
yaml_lines <- append(yaml_lines, tag_lines, after = stage_line)
writeLines(yaml_lines, dest_file)
} else {
cli::cli_alert_danger("Unable to locate stage line in .gitlab-ci.yml for tag insertion")
Comment thread
VincentGuyader marked this conversation as resolved.
success <- FALSE
}
}

if (isTRUE(success)) {
cli::cli_alert_success(".gitlab-ci.yml file successfully copied to {path}")
} else {
Expand Down
3 changes: 3 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ library(shiny2docker)

# Copy the .gitlab-ci.yml file to the current directory
set_gitlab_ci()

# Specify runner tags
set_gitlab_ci(tags = c("shiny_build", "prod"))
```

### Configure GitHub Actions for Docker Builds
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ library(shiny2docker)

# Copy the .gitlab-ci.yml file to the current directory
set_gitlab_ci()

# Specify runner tags
set_gitlab_ci(tags = c("shiny_build", "prod"))
```

### Configure GitHub Actions for Docker Builds
Expand Down
9 changes: 8 additions & 1 deletion man/set_gitlab_ci.Rd

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

1 change: 1 addition & 0 deletions shiny2docker.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: 77ecc2cf-a94d-4ad3-a353-68d0edad10ea

RestoreWorkspace: No
SaveWorkspace: No
Expand Down
31 changes: 31 additions & 0 deletions test_tags.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Test de la fonction set_gitlab_ci avec et sans tags

# Charger la fonction
source("R/set_gitlab_ci.R")

# Test 1: Sans tags
cat("=== Test 1: Sans tags ===\n")
temp_dir1 <- tempdir()
result1 <- set_gitlab_ci(path = temp_dir1)
cat("Résultat:", result1, "\n")

# Afficher le contenu généré
cat("\nContenu généré (sans tags):\n")
content1 <- readLines(file.path(temp_dir1, ".gitlab-ci.yml"))
cat(content1, sep = "\n")

cat("\n\n=== Test 2: Avec tags ===\n")
# Test 2: Avec tags
temp_dir2 <- file.path(tempdir(), "test_with_tags")
dir.create(temp_dir2, showWarnings = FALSE)
result2 <- set_gitlab_ci(path = temp_dir2, tags = c("docker", "linux"))
cat("Résultat:", result2, "\n")

# Afficher le contenu généré
cat("\nContenu généré (avec tags):\n")
content2 <- readLines(file.path(temp_dir2, ".gitlab-ci.yml"))
cat(content2, sep = "\n")

# Vérifier que les tags sont présents
has_tags <- any(grepl("tags:", content2))
cat("\nTags détectés dans le fichier:", has_tags, "\n")
15 changes: 15 additions & 0 deletions tests/testthat/test-set_gitlab_ci.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
library(testthat)

# Create temp directory for testing
tmp_dir <- tempfile()

test_that("set_gitlab_ci inserts multiple tags", {
set_gitlab_ci(path = tmp_dir, tags = c("shiny_build", "prod"))
expect_true(file.exists(file.path(tmp_dir, ".gitlab-ci.yml")))
ci_lines <- readLines(file.path(tmp_dir, ".gitlab-ci.yml"))
expect_true(any(grepl("^\\s*tags:\\s*$", ci_lines)))
expect_true(any(grepl("^\\s*tags:\\s*$", ci_lines)))
expect_true(any(grepl("shiny_build", ci_lines)))
expect_true(any(grepl("prod", ci_lines)))
file.remove(file.path(tmp_dir, ".gitlab-ci.yml"))
})
3 changes: 3 additions & 0 deletions vignettes/introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ The package provides the `set_gitlab_ci()` function to simplify the process of c
```{r gitlab-ci, eval=FALSE}
# Copy the .gitlab-ci.yml file to the current directory
set_gitlab_ci(path = ".")

# Specify runner tags
set_gitlab_ci(path = ".", tags = c("shiny_build", "prod"))
```

Once the `.gitlab-ci.yml` file is in place, you can integrate it with GitLab CI/CD to automate your Docker image build and deployment process.
Expand Down