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: dockerfiler
Title: Easy Dockerfile Creation from R
Version: 0.2.4
Version: 0.2.4.9000
Authors@R: c(
person("Colin", "Fay", , "contact@colinfay.me", role = c("cre", "aut"),
comment = c(ORCID = "0000-0001-7343-1846")),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# dockerfiler 0.2.4.9xxx

- allow multistage dockerfile creation

# dockerfiler 0.2.4

- remove native pipe thanks to @HenningLorenzen-ext-bayer, this enable to use of older R versions
Expand Down
25 changes: 14 additions & 11 deletions R/add.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ add_add <- function(
glue("ADD {from} {to}")
}

add_copy <- function(
from,
to,
force = TRUE
) {
add_copy <- function(from,
to,
stage = NULL,
force = TRUE) {
if (!force) {
warn_if_not(
normalizePath(from),
file.exists,
"The file `from` doesn't seem to exists"
)
warn_if_not(normalizePath(from),
file.exists,
"The file `from` doesn't seem to exists")
}

if (is.null(stage)) {
glue("COPY {from} {to}")
} else {
glue("COPY --from={stage} {from} {to}")

}
glue("COPY {from} {to}")
}

add_workdir <- function(where) {
Expand Down
10 changes: 6 additions & 4 deletions R/dockerfile.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ self$Dockerfile <- c(self$Dockerfile, add_add(from, to, force))
#' @param from The source file.
#' @param to The destination file.
#' @param force If TRUE, overwrite the destination file.
#' @param stage Optional. Name of the build stage (e.g., `"builder"`) to copy files from. This corresponds to the `--from=` part in a Dockerfile COPY instruction (e.g., `COPY --from=builder /source /dest`). If `NULL`, the `--from=` argument is omitted.
#' @return the Dockerfile object, invisibly.
COPY = function(from, to, force = TRUE) {
self$Dockerfile <- c(self$Dockerfile, add_copy(from, to, force))
COPY = function(from, to, stage= NULL , force = TRUE) {
self$Dockerfile <- c(self$Dockerfile, add_copy(from, to, stage, force))
},
#' @description
#' Add a WORKDIR command.
Expand Down Expand Up @@ -162,9 +163,10 @@ cat(self$Dockerfile, sep = "\n")
#' @description
#' Write the Dockerfile to a file.
#' @param as The file to write to.
#' @param append boolean, if TRUE append to file.
#' @return used for side effect
write = function(as = "Dockerfile") {
base::write(self$Dockerfile, file = as)
write = function(as = "Dockerfile", append = FALSE) {
base::write(self$Dockerfile, file = as, append = append)
},
#' @description
#' Switch commands.
Expand Down
8 changes: 6 additions & 2 deletions man/Dockerfile.Rd

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

27 changes: 27 additions & 0 deletions tests/testthat/test-copy-stage.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
test_that("Dockerfile COPY with build stage works correctly", {
# Création des deux étapes du Dockerfile
stage_1 <- Dockerfile$new(FROM = "alpine", AS = "builder")
stage_1$RUN('echo "coucou depuis le builder" > /coucou')

stage_2 <- Dockerfile$new(FROM = "ubuntu")
stage_2$COPY(from = "/coucou", to = "/truc.txt", force = TRUE, stage = "builder")
stage_2$RUN("cat /truc.txt")

tmpfile <- tempfile(fileext = ".Dockerfile")
stage_1$write(as = tmpfile)
stage_2$write(as = tmpfile, append = TRUE)

docker_lines <- readLines(tmpfile)

expect_length(docker_lines, 5)

expect_equal(docker_lines[1], "FROM alpine AS builder")
expect_equal(docker_lines[2], 'RUN echo "coucou depuis le builder" > /coucou')
expect_equal(docker_lines[3], "FROM ubuntu")
expect_equal(docker_lines[4], "COPY --from=builder /coucou /truc.txt")
expect_equal(docker_lines[5], "RUN cat /truc.txt")

expect_true(any(grepl("COPY --from=builder", docker_lines)))
expect_true(any(grepl("/coucou", docker_lines)))
expect_true(any(grepl("/truc.txt", docker_lines)))
})