From a7ecc354a9ae87fe31f7cb39e23973bee5387ade Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Sun, 6 Oct 2024 16:11:29 +0000 Subject: [PATCH] feat!: prepend/append commands to dockerfile BREAKING CHANGE: Command definition in the configuration has been changed from [[docker.commands]] distro = { id = "Ubuntu" } command = "echo runs before the base layer" to [[docker.commands.prepend]] distro = { id = "Ubuntu" } command = "echo runs before the base layer" --- config/out-of-tree.go | 9 ++++++--- container/container.go | 30 ++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/config/out-of-tree.go b/config/out-of-tree.go index 175874b..a4e2b4a 100644 --- a/config/out-of-tree.go +++ b/config/out-of-tree.go @@ -35,9 +35,12 @@ type OutOfTree struct { Timeout artifact.Duration Registry string - // Commands that will be executed before - // the base layer of Dockerfile - Commands []distro.Command + // Commands that are executed before (prepend) and after (append) the + // base layer of the Dockerfile. + Commands struct { + Prepend []distro.Command + Append []distro.Command + } } } diff --git a/container/container.go b/container/container.go index fd479f2..e358820 100644 --- a/container/container.go +++ b/container/container.go @@ -32,7 +32,12 @@ var Registry = "" var Timeout time.Duration -var Commands []distro.Command +// Commands that are executed before (prepend) and after (append) the +// base layer of the Dockerfile. +var Commands struct { + Prepend []distro.Command + Append []distro.Command +} var UseCache = true @@ -296,9 +301,15 @@ func (c Container) Build(image string, envs, runs []string) (err error) { } cf += image + "\n" - for _, c := range Commands { - // TODO check for distro type - cf += "RUN " + c.Command + "\n" + for _, cmd := range Commands.Prepend { + if cmd.Distro.ID != distro.None && cmd.Distro.ID != c.dist.ID { + continue + } + if cmd.Distro.Release != "" && cmd.Distro.Release != c.dist.Release { + continue + } + + cf += "RUN " + cmd.Command + "\n" } for _, e := range envs { @@ -309,6 +320,17 @@ func (c Container) Build(image string, envs, runs []string) (err error) { cf += "RUN " + c + "\n" } + for _, cmd := range Commands.Append { + if cmd.Distro.ID != distro.None && cmd.Distro.ID != c.dist.ID { + continue + } + if cmd.Distro.Release != "" && cmd.Distro.Release != c.dist.Release { + continue + } + + cf += "RUN " + cmd.Command + "\n" + } + buf, err := os.ReadFile(cfile) if err != nil { err = os.WriteFile(cfile, []byte(cf), os.ModePerm)