From e4253ffd7bf313e3b8a9696a7428adf8978a1055 Mon Sep 17 00:00:00 2001 From: Ashok Pon Kumar Date: Mon, 19 Jul 2021 23:21:23 +0530 Subject: [PATCH] fix: Fix build script context paths (#565) Signed-off-by: Ashok Pon Kumar Co-authored-by: Akash Nayak --- .../templates/buildimages.bat | 2 +- .../templates/buildimages.sh | 2 +- .../generators/containerimagesbuildscript.go | 38 +++++++++++++------ .../dockerfilebuildscriptgenerator.go | 5 ++- .../artifacts/containerimagebuildscript.go | 10 +++-- 5 files changed, 40 insertions(+), 17 deletions(-) diff --git a/assets/inbuilt/transformers/generators/containerimagesbuildscript/templates/buildimages.bat b/assets/inbuilt/transformers/generators/containerimagesbuildscript/templates/buildimages.bat index dffd717fc..d2ed80a57 100755 --- a/assets/inbuilt/transformers/generators/containerimagesbuildscript/templates/buildimages.bat +++ b/assets/inbuilt/transformers/generators/containerimagesbuildscript/templates/buildimages.bat @@ -14,6 +14,6 @@ {{range $buildscript := . }} pushd {{ $buildscript.PathWindows }} -{{ $buildscript.BuildScriptName }} +{{ $buildscript.BuildScript }} popd {{end}} \ No newline at end of file diff --git a/assets/inbuilt/transformers/generators/containerimagesbuildscript/templates/buildimages.sh b/assets/inbuilt/transformers/generators/containerimagesbuildscript/templates/buildimages.sh index 79fcfaf4a..c57669b54 100755 --- a/assets/inbuilt/transformers/generators/containerimagesbuildscript/templates/buildimages.sh +++ b/assets/inbuilt/transformers/generators/containerimagesbuildscript/templates/buildimages.sh @@ -14,6 +14,6 @@ {{range $buildscript := . }} cd {{ $buildscript.PathUnix }} -./{{ $buildscript.BuildScriptName }} +./{{ $buildscript.BuildScript }} cd - {{end}} \ No newline at end of file diff --git a/internal/transformer/classes/generators/containerimagesbuildscript.go b/internal/transformer/classes/generators/containerimagesbuildscript.go index c66db654c..4afbbcf1e 100644 --- a/internal/transformer/classes/generators/containerimagesbuildscript.go +++ b/internal/transformer/classes/generators/containerimagesbuildscript.go @@ -34,9 +34,9 @@ type ContainerImagesBuildScript struct { // ImageBuildTemplateConfig represents template config used by ImagePush script type ImageBuildTemplateConfig struct { - BuildScriptName string - PathUnix string - PathWindows string + BuildScript string + PathUnix string + PathWindows string } // Init Initializes the transformer @@ -69,27 +69,43 @@ func (t *ContainerImagesBuildScript) Transform(newArtifacts []transformertypes.A for _, a := range newArtifacts { if a.Artifact == artifacts.ContainerImageBuildScriptArtifactType { for _, shScript := range a.Paths[artifacts.ContainerImageBuildShScriptPathType] { - relPath, err := filepath.Rel(t.Env.GetEnvironmentOutput(), filepath.Dir(shScript)) + contextPath := filepath.Dir(shScript) + if ctxPath, ok := a.Paths[artifacts.ContainerImageBuildShScriptContextPathType]; ok { + contextPath = ctxPath[0] + } + relPath, err := filepath.Rel(t.Env.GetEnvironmentOutput(), contextPath) + if err != nil { + logrus.Errorf("Unable to make path relative : %s", err) + continue + } + scriptPath, err := filepath.Rel(contextPath, shScript) if err != nil { logrus.Errorf("Unable to make path relative : %s", err) continue } shScripts = append(shScripts, ImageBuildTemplateConfig{ - BuildScriptName: filepath.Base(shScript), - PathUnix: common.GetUnixPath(relPath), - PathWindows: common.GetWindowsPath(relPath), + BuildScript: scriptPath, + PathUnix: common.GetUnixPath(relPath), }) } for _, batScript := range a.Paths[artifacts.ContainerImageBuildBatScriptPathType] { - relPath, err := filepath.Rel(t.Env.GetEnvironmentOutput(), filepath.Dir(batScript)) + contextPath := filepath.Dir(batScript) + if ctxPath, ok := a.Paths[artifacts.ContainerImageBuildShScriptContextPathType]; ok { + contextPath = ctxPath[0] + } + relPath, err := filepath.Rel(t.Env.GetEnvironmentOutput(), contextPath) + if err != nil { + logrus.Errorf("Unable to make path relative : %s", err) + continue + } + scriptPath, err := filepath.Rel(contextPath, batScript) if err != nil { logrus.Errorf("Unable to make path relative : %s", err) continue } batScripts = append(batScripts, ImageBuildTemplateConfig{ - BuildScriptName: filepath.Base(batScript), - PathUnix: common.GetUnixPath(relPath), - PathWindows: common.GetWindowsPath(relPath), + BuildScript: scriptPath, + PathWindows: common.GetWindowsPath(relPath), }) } } diff --git a/internal/transformer/classes/generators/dockerfilebuildscriptgenerator.go b/internal/transformer/classes/generators/dockerfilebuildscriptgenerator.go index f935360bc..0853482bc 100644 --- a/internal/transformer/classes/generators/dockerfilebuildscriptgenerator.go +++ b/internal/transformer/classes/generators/dockerfilebuildscriptgenerator.go @@ -142,7 +142,10 @@ func (t *DockerfileImageBuildScript) Transform(newArtifacts []transformertypes.A Name: artifacts.ContainerImageBuildScriptArtifactType, Artifact: artifacts.ContainerImageBuildScriptArtifactType, Paths: map[string][]string{artifacts.ContainerImageBuildShScriptPathType: {filepath.Join(common.ScriptsDir, "builddockerimages.sh")}, - artifacts.ContainerImageBuildBatScriptPathType: {filepath.Join(common.ScriptsDir, "builddockerimages.bat")}}, + artifacts.ContainerImageBuildShScriptContextPathType: {"."}, + artifacts.ContainerImageBuildBatScriptPathType: {filepath.Join(common.ScriptsDir, "builddockerimages.bat")}, + artifacts.ContainerImageBuildBatScriptContextPathType: {"."}, + }, }) return pathMappings, nartifacts, nil } diff --git a/types/transformer/artifacts/containerimagebuildscript.go b/types/transformer/artifacts/containerimagebuildscript.go index d5627b5e4..77becbaa9 100644 --- a/types/transformer/artifacts/containerimagebuildscript.go +++ b/types/transformer/artifacts/containerimagebuildscript.go @@ -21,13 +21,17 @@ import ( ) const ( - // ContainerImageBuildScriptArtifactType represents the image push script artifact type + // ContainerImageBuildScriptArtifactType represents the image build script artifact type ContainerImageBuildScriptArtifactType transformertypes.ArtifactType = "ContainerImageBuildScript" ) const ( - // ContainerImageBuildShScriptPathType represents the image push script path type + // ContainerImageBuildShScriptPathType represents the image build script path type ContainerImageBuildShScriptPathType transformertypes.PathType = "ContainerImageBuildShScript" - // ContainerImageBuildBatScriptPathType represents the image push script path type + // ContainerImageBuildBatScriptPathType represents the image build script path type ContainerImageBuildBatScriptPathType transformertypes.PathType = "ContainerImageBuildBatScript" + // ContainerImageBuildShScriptContextPathType represents the image build script path type + ContainerImageBuildShScriptContextPathType transformertypes.PathType = "ContainerImageBuildShScriptContextScript" + // ContainerImageBuildBatScriptContextPathType represents the image build script path type + ContainerImageBuildBatScriptContextPathType transformertypes.PathType = "ContainerImageBuildBatScriptContextScript" )