diff --git a/compiler/engine.go b/compiler/engine.go index 520e962e3..8bdb27b0c 100644 --- a/compiler/engine.go +++ b/compiler/engine.go @@ -92,15 +92,6 @@ type Engine interface { // InitStep step process into a yaml configuration. InitStep(*yaml.Build) (*yaml.Build, error) - // Script Compiler Interface Functions - - // ScriptStages defines a function that injects the script - // for each step in every stage in a yaml configuration. - ScriptStages(yaml.StageSlice) (yaml.StageSlice, error) - // ScriptSteps defines a function that injects the script - // for each step in a yaml configuration. - ScriptSteps(yaml.StepSlice) (yaml.StepSlice, error) - // Substitute Compiler Interface Functions // SubstituteStages defines a function that replaces every diff --git a/compiler/native/compile.go b/compiler/native/compile.go index db50486cf..c4bc1c917 100644 --- a/compiler/native/compile.go +++ b/compiler/native/compile.go @@ -417,12 +417,6 @@ func (c *Client) compileSteps(ctx context.Context, p *yaml.Build, _pipeline *api return nil, _pipeline, err } - // inject the scripts into the steps - p.Steps, err = c.ScriptSteps(p.Steps) - if err != nil { - return nil, _pipeline, err - } - // create executable representation build, err := c.TransformSteps(r, p) if err != nil { @@ -525,12 +519,6 @@ func (c *Client) compileStages(ctx context.Context, p *yaml.Build, _pipeline *ap return nil, _pipeline, err } - // inject the scripts into the stages - p.Stages, err = c.ScriptStages(p.Stages) - if err != nil { - return nil, _pipeline, err - } - // create executable representation build, err := c.TransformStages(r, p) if err != nil { diff --git a/compiler/native/compile_test.go b/compiler/native/compile_test.go index a22f82c3c..e4dcfe54c 100644 --- a/compiler/native/compile_test.go +++ b/compiler/native/compile_test.go @@ -63,25 +63,16 @@ func TestNative_Compile_StagesPipeline(t *testing.T) { installEnv := environment(nil, m, nil, nil, nil, 0) installEnv["GRADLE_OPTS"] = "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false" installEnv["GRADLE_USER_HOME"] = ".gradle" - installEnv["HOME"] = constants.DefaultHomeDir - installEnv["SHELL"] = constants.DefaultShell - installEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew downloadDependencies"}) installEnv["HELLO"] = "Hello, Global Environment" testEnv := environment(nil, m, nil, nil, nil, 0) testEnv["GRADLE_OPTS"] = "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false" testEnv["GRADLE_USER_HOME"] = ".gradle" - testEnv["HOME"] = constants.DefaultHomeDir - testEnv["SHELL"] = constants.DefaultShell - testEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew check"}) testEnv["HELLO"] = "Hello, Global Environment" buildEnv := environment(nil, m, nil, nil, nil, 0) buildEnv["GRADLE_OPTS"] = "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false" buildEnv["GRADLE_USER_HOME"] = ".gradle" - buildEnv["HOME"] = constants.DefaultHomeDir - buildEnv["SHELL"] = constants.DefaultShell - buildEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew build"}) buildEnv["HELLO"] = "Hello, Global Environment" dockerEnv := environment(nil, m, nil, nil, nil, 0) @@ -137,9 +128,8 @@ func TestNative_Compile_StagesPipeline(t *testing.T) { Steps: pipeline.ContainerSlice{ &pipeline.Container{ ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"./gradlew downloadDependencies"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, Environment: installEnv, Image: "openjdk:latest", Name: "install", @@ -155,9 +145,8 @@ func TestNative_Compile_StagesPipeline(t *testing.T) { Steps: pipeline.ContainerSlice{ &pipeline.Container{ ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"./gradlew check"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, Environment: testEnv, Image: "openjdk:latest", Name: "test", @@ -173,9 +162,8 @@ func TestNative_Compile_StagesPipeline(t *testing.T) { Steps: pipeline.ContainerSlice{ &pipeline.Container{ ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"./gradlew build"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, Environment: buildEnv, Image: "openjdk:latest", Name: "build", @@ -474,25 +462,16 @@ func TestNative_Compile_StepsPipeline(t *testing.T) { installEnv := environment(nil, m, nil, nil, nil, 0) installEnv["GRADLE_OPTS"] = "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false" installEnv["GRADLE_USER_HOME"] = ".gradle" - installEnv["HOME"] = constants.DefaultHomeDir - installEnv["SHELL"] = constants.DefaultShell - installEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew downloadDependencies"}) installEnv["HELLO"] = "Hello, Global Environment" testEnv := environment(nil, m, nil, nil, nil, 0) testEnv["GRADLE_OPTS"] = "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false" testEnv["GRADLE_USER_HOME"] = ".gradle" - testEnv["HOME"] = constants.DefaultHomeDir - testEnv["SHELL"] = constants.DefaultShell - testEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew check"}) testEnv["HELLO"] = "Hello, Global Environment" buildEnv := environment(nil, m, nil, nil, nil, 0) buildEnv["GRADLE_OPTS"] = "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false" buildEnv["GRADLE_USER_HOME"] = ".gradle" - buildEnv["HOME"] = constants.DefaultHomeDir - buildEnv["SHELL"] = constants.DefaultShell - buildEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew build"}) buildEnv["HELLO"] = "Hello, Global Environment" dockerEnv := environment(nil, m, nil, nil, nil, 0) @@ -534,9 +513,8 @@ func TestNative_Compile_StepsPipeline(t *testing.T) { }, &pipeline.Container{ ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"./gradlew downloadDependencies"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, Environment: installEnv, Image: "openjdk:latest", Name: "install", @@ -545,9 +523,8 @@ func TestNative_Compile_StepsPipeline(t *testing.T) { }, &pipeline.Container{ ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"./gradlew check"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, Environment: testEnv, Image: "openjdk:latest", Name: "test", @@ -556,9 +533,8 @@ func TestNative_Compile_StepsPipeline(t *testing.T) { }, &pipeline.Container{ ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"./gradlew build"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, Environment: buildEnv, Image: "openjdk:latest", Name: "build", @@ -690,27 +666,18 @@ func TestNative_Compile_StagesPipelineTemplate(t *testing.T) { installEnv := environment(nil, m, nil, nil, nil, 0) installEnv["GRADLE_OPTS"] = "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false" installEnv["GRADLE_USER_HOME"] = ".gradle" - installEnv["HOME"] = constants.DefaultHomeDir - installEnv["SHELL"] = constants.DefaultShell - installEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew downloadDependencies"}) installEnv["bar"] = "test4" installEnv["star"] = "test3" testEnv := environment(nil, m, nil, nil, nil, 0) testEnv["GRADLE_OPTS"] = "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false" testEnv["GRADLE_USER_HOME"] = ".gradle" - testEnv["HOME"] = constants.DefaultHomeDir - testEnv["SHELL"] = constants.DefaultShell - testEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew check"}) testEnv["bar"] = "test4" testEnv["star"] = "test3" buildEnv := environment(nil, m, nil, nil, nil, 0) buildEnv["GRADLE_OPTS"] = "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false" buildEnv["GRADLE_USER_HOME"] = ".gradle" - buildEnv["HOME"] = constants.DefaultHomeDir - buildEnv["SHELL"] = constants.DefaultShell - buildEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew build", "echo gradle"}) buildEnv["bar"] = "test4" buildEnv["star"] = "test3" @@ -772,9 +739,8 @@ func TestNative_Compile_StagesPipelineTemplate(t *testing.T) { Steps: pipeline.ContainerSlice{ &pipeline.Container{ ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"./gradlew downloadDependencies"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, Environment: installEnv, Image: "openjdk:latest", Name: "sample_install", @@ -783,9 +749,8 @@ func TestNative_Compile_StagesPipelineTemplate(t *testing.T) { }, &pipeline.Container{ ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"./gradlew check"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, Environment: testEnv, Image: "openjdk:latest", Name: "sample_test", @@ -794,9 +759,8 @@ func TestNative_Compile_StagesPipelineTemplate(t *testing.T) { }, &pipeline.Container{ ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"./gradlew build", "echo gradle"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, Environment: buildEnv, Image: "openjdk:latest", Name: "sample_build", @@ -952,27 +916,18 @@ func TestNative_Compile_StepsPipelineTemplate(t *testing.T) { installEnv := environment(nil, m, nil, nil, nil, 0) installEnv["GRADLE_OPTS"] = "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false" installEnv["GRADLE_USER_HOME"] = ".gradle" - installEnv["HOME"] = constants.DefaultHomeDir - installEnv["SHELL"] = constants.DefaultShell - installEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew downloadDependencies"}) installEnv["bar"] = "test4" installEnv["star"] = "test3" testEnv := environment(nil, m, nil, nil, nil, 0) testEnv["GRADLE_OPTS"] = "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false" testEnv["GRADLE_USER_HOME"] = ".gradle" - testEnv["HOME"] = constants.DefaultHomeDir - testEnv["SHELL"] = constants.DefaultShell - testEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew check"}) testEnv["bar"] = "test4" testEnv["star"] = "test3" buildEnv := environment(nil, m, nil, nil, nil, 0) buildEnv["GRADLE_OPTS"] = "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false" buildEnv["GRADLE_USER_HOME"] = ".gradle" - buildEnv["HOME"] = constants.DefaultHomeDir - buildEnv["SHELL"] = constants.DefaultShell - buildEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew build", "echo gradle"}) buildEnv["bar"] = "test4" buildEnv["star"] = "test3" @@ -1018,8 +973,7 @@ func TestNative_Compile_StepsPipelineTemplate(t *testing.T) { &pipeline.Container{ ID: "", Directory: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, + Commands: []string{"./gradlew downloadDependencies"}, Environment: installEnv, Image: "openjdk:latest", Name: "sample_install", @@ -1029,8 +983,7 @@ func TestNative_Compile_StepsPipelineTemplate(t *testing.T) { &pipeline.Container{ ID: "", Directory: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, + Commands: []string{"./gradlew check"}, Environment: testEnv, Image: "openjdk:latest", Name: "sample_test", @@ -1040,8 +993,7 @@ func TestNative_Compile_StepsPipelineTemplate(t *testing.T) { &pipeline.Container{ ID: "", Directory: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, + Commands: []string{"./gradlew build", "echo gradle"}, Environment: buildEnv, Image: "openjdk:latest", Name: "sample_build", @@ -1174,9 +1126,6 @@ func TestNative_Compile_StepsPipelineTemplate_VelaFunction_TemplateName(t *testi setupEnv := environment(nil, m, nil, nil, nil, 0) helloEnv := environment(nil, m, nil, nil, nil, 0) - helloEnv["HOME"] = constants.DefaultHomeDir - helloEnv["SHELL"] = constants.DefaultShell - helloEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"echo sample"}) want := &pipeline.Build{ Version: "1", @@ -1209,8 +1158,7 @@ func TestNative_Compile_StepsPipelineTemplate_VelaFunction_TemplateName(t *testi &pipeline.Container{ ID: "", Directory: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, + Commands: []string{"echo sample"}, Environment: helloEnv, Image: "sample", Name: "sample_hello", @@ -1287,9 +1235,6 @@ func TestNative_Compile_StepsPipelineTemplate_VelaFunction_TemplateName_Inline(t setupEnv := environment(nil, m, nil, nil, nil, 0) helloEnv := environment(nil, m, nil, nil, nil, 0) - helloEnv["HOME"] = constants.DefaultHomeDir - helloEnv["SHELL"] = constants.DefaultShell - helloEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"echo inline_templatename"}) want := &pipeline.Build{ Version: "1", @@ -1322,8 +1267,7 @@ func TestNative_Compile_StepsPipelineTemplate_VelaFunction_TemplateName_Inline(t &pipeline.Container{ ID: "", Directory: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, + Commands: []string{"echo inline_templatename"}, Environment: helloEnv, Image: "inline_templatename", Name: "inline_templatename_hello", @@ -1903,9 +1847,6 @@ func TestNative_Compile_StageNameCollisionPurged(t *testing.T) { initEnv := environment(build, m, nil, nil, nil, 0) stepEnv := environment(build, m, nil, nil, nil, 0) - stepEnv["HOME"] = constants.DefaultHomeDir - stepEnv["SHELL"] = constants.DefaultShell - stepEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{`echo "Building..."`}) want := &pipeline.Build{ Version: "1", @@ -1954,9 +1895,8 @@ func TestNative_Compile_StageNameCollisionPurged(t *testing.T) { Steps: pipeline.ContainerSlice{ &pipeline.Container{ ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{`echo "Building..."`}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, Environment: stepEnv, Image: "alpine", Name: "word_key_build", @@ -2086,14 +2026,8 @@ func TestNative_Compile_StepNameCollisionPurged(t *testing.T) { initEnv := environment(build, m, nil, nil, nil, 0) buildEnv := environment(build, m, nil, nil, nil, 0) - buildEnv["HOME"] = constants.DefaultHomeDir - buildEnv["SHELL"] = constants.DefaultShell - buildEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{`echo "Building..."`}) testEnv := environment(build, m, nil, nil, nil, 0) - testEnv["HOME"] = constants.DefaultHomeDir - testEnv["SHELL"] = constants.DefaultShell - testEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{`echo "Testing..."`}) want := &pipeline.Build{ Version: "1", @@ -2125,9 +2059,8 @@ func TestNative_Compile_StepNameCollisionPurged(t *testing.T) { }, &pipeline.Container{ ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{`echo "Building..."`}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, Environment: buildEnv, Image: "alpine", Name: "build", @@ -2136,9 +2069,8 @@ func TestNative_Compile_StepNameCollisionPurged(t *testing.T) { }, &pipeline.Container{ ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{`echo "Testing..."`}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, Environment: testEnv, Image: "alpine", Name: "test", @@ -2499,11 +2431,8 @@ func convertFileToGithubResponse(file string) (github.RepositoryContent, error) return content, nil } -func generateTestEnv(command string, m *internal.Metadata, pipelineType string) map[string]string { +func generateTestEnv(m *internal.Metadata, pipelineType string) map[string]string { output := environment(nil, m, nil, nil, nil, 0) - output["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{command}) - output["HOME"] = constants.DefaultHomeDir - output["SHELL"] = constants.DefaultShell output["VELA_REPO_PIPELINE_TYPE"] = pipelineType return output @@ -2632,10 +2561,9 @@ func Test_Compile_Inline(t *testing.T) { Steps: []*pipeline.Container{ { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"echo from inline"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: generateTestEnv("echo from inline", m, ""), + Environment: generateTestEnv(m, ""), Image: "alpine", Name: "test", Pull: "not_present", @@ -2650,10 +2578,9 @@ func Test_Compile_Inline(t *testing.T) { Steps: []*pipeline.Container{ { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"echo hello from foo"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: generateTestEnv("echo hello from foo", m, ""), + Environment: generateTestEnv(m, ""), Image: "alpine", Name: "starlark_build_foo", Pull: "not_present", @@ -2668,10 +2595,9 @@ func Test_Compile_Inline(t *testing.T) { Steps: []*pipeline.Container{ { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"echo hello from bar"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: generateTestEnv("echo hello from bar", m, ""), + Environment: generateTestEnv(m, ""), Image: "alpine", Name: "starlark_build_bar", Pull: "not_present", @@ -2734,10 +2660,9 @@ func Test_Compile_Inline(t *testing.T) { Steps: []*pipeline.Container{ { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"echo from inline"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: generateTestEnv("echo from inline", m, ""), + Environment: generateTestEnv(m, ""), Image: "alpine", Name: "test", Pull: "not_present", @@ -2752,10 +2677,9 @@ func Test_Compile_Inline(t *testing.T) { Steps: []*pipeline.Container{ { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"echo from inline"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: generateTestEnv("echo from inline", m, ""), + Environment: generateTestEnv(m, ""), Image: "alpine", Name: "nested_test", Pull: "not_present", @@ -2770,10 +2694,9 @@ func Test_Compile_Inline(t *testing.T) { Steps: []*pipeline.Container{ { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"echo hello from foo"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: generateTestEnv("echo hello from foo", m, ""), + Environment: generateTestEnv(m, ""), Image: "alpine", Name: "nested_starlark_build_foo", Pull: "not_present", @@ -2788,10 +2711,9 @@ func Test_Compile_Inline(t *testing.T) { Steps: []*pipeline.Container{ { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"echo hello from bar"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: generateTestEnv("echo hello from bar", m, ""), + Environment: generateTestEnv(m, ""), Image: "alpine", Name: "nested_starlark_build_bar", Pull: "not_present", @@ -2837,10 +2759,9 @@ func Test_Compile_Inline(t *testing.T) { }, { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, + Commands: []string{"echo from inline"}, Directory: "", - Environment: generateTestEnv("echo from inline", m, ""), + Environment: generateTestEnv(m, ""), Name: "test", Image: "alpine", Number: 3, @@ -2848,10 +2769,9 @@ func Test_Compile_Inline(t *testing.T) { }, { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, + Commands: []string{"echo hello from foo"}, Directory: "", - Environment: generateTestEnv("echo hello from foo", m, ""), + Environment: generateTestEnv(m, ""), Name: "golang_foo", Image: "alpine", Number: 4, @@ -2859,10 +2779,9 @@ func Test_Compile_Inline(t *testing.T) { }, { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, + Commands: []string{"echo hello from bar"}, Directory: "", - Environment: generateTestEnv("echo hello from bar", m, ""), + Environment: generateTestEnv(m, ""), Name: "golang_bar", Image: "alpine", Number: 5, @@ -2870,10 +2789,9 @@ func Test_Compile_Inline(t *testing.T) { }, { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, + Commands: []string{"echo hello from star"}, Directory: "", - Environment: generateTestEnv("echo hello from star", m, ""), + Environment: generateTestEnv(m, ""), Name: "golang_star", Image: "alpine", Number: 6, @@ -2881,10 +2799,9 @@ func Test_Compile_Inline(t *testing.T) { }, { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, + Commands: []string{"echo hello from foo"}, Directory: "", - Environment: generateTestEnv("echo hello from foo", m, ""), + Environment: generateTestEnv(m, ""), Name: "starlark_build_foo", Image: "alpine", Number: 7, @@ -2892,10 +2809,9 @@ func Test_Compile_Inline(t *testing.T) { }, { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, + Commands: []string{"echo hello from bar"}, Directory: "", - Environment: generateTestEnv("echo hello from bar", m, ""), + Environment: generateTestEnv(m, ""), Name: "starlark_build_bar", Image: "alpine", Number: 8, @@ -2954,10 +2870,9 @@ func Test_Compile_Inline(t *testing.T) { }, { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, + Commands: []string{"echo from inline"}, Directory: "", - Environment: generateTestEnv("echo from inline", m, ""), + Environment: generateTestEnv(m, ""), Name: "test", Image: "alpine", Number: 3, @@ -3059,10 +2974,9 @@ func Test_Compile_Inline(t *testing.T) { }, { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, + Commands: []string{"echo from inline"}, Directory: "", - Environment: generateTestEnv("echo from inline", m, ""), + Environment: generateTestEnv(m, ""), Name: "test", Image: "alpine", Number: 3, @@ -3197,10 +3111,9 @@ func Test_Compile_Inline(t *testing.T) { Steps: []*pipeline.Container{ { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"echo from inline foo"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: generateTestEnv("echo from inline foo", m, constants.PipelineTypeGo), + Environment: generateTestEnv(m, constants.PipelineTypeGo), Image: "alpine", Name: "foo", Pull: "not_present", @@ -3215,10 +3128,9 @@ func Test_Compile_Inline(t *testing.T) { Steps: []*pipeline.Container{ { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"echo from inline bar"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: generateTestEnv("echo from inline bar", m, constants.PipelineTypeGo), + Environment: generateTestEnv(m, constants.PipelineTypeGo), Image: "alpine", Name: "bar", Pull: "not_present", @@ -3233,10 +3145,9 @@ func Test_Compile_Inline(t *testing.T) { Steps: []*pipeline.Container{ { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"echo from inline star"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: generateTestEnv("echo from inline star", m, constants.PipelineTypeGo), + Environment: generateTestEnv(m, constants.PipelineTypeGo), Image: "alpine", Name: "star", Pull: "not_present", @@ -3251,10 +3162,9 @@ func Test_Compile_Inline(t *testing.T) { Steps: []*pipeline.Container{ { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"echo hello from foo"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: generateTestEnv("echo hello from foo", m, constants.PipelineTypeGo), + Environment: generateTestEnv(m, constants.PipelineTypeGo), Image: "alpine", Name: "starlark_build_foo", Pull: "not_present", @@ -3269,10 +3179,9 @@ func Test_Compile_Inline(t *testing.T) { Steps: []*pipeline.Container{ { ID: "", - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Commands: []string{"echo hello from bar"}, Directory: "", - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: generateTestEnv("echo hello from bar", m, constants.PipelineTypeGo), + Environment: generateTestEnv(m, constants.PipelineTypeGo), Image: "alpine", Name: "starlark_build_bar", Pull: "not_present", diff --git a/compiler/native/script.go b/compiler/native/script.go deleted file mode 100644 index 7bdcdd4e4..000000000 --- a/compiler/native/script.go +++ /dev/null @@ -1,123 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -package native - -import ( - "bytes" - "encoding/base64" - "fmt" - "strings" - - "github.com/go-vela/server/compiler/types/yaml" - "github.com/go-vela/server/constants" -) - -// ScriptStages injects the script for each step in every stage in a yaml configuration. -func (c *Client) ScriptStages(s yaml.StageSlice) (yaml.StageSlice, error) { - // iterate through all stages - for _, stage := range s { - // inject the scripts into the steps for the stage - steps, err := c.ScriptSteps(stage.Steps) - if err != nil { - return nil, err - } - - stage.Steps = steps - } - - return s, nil -} - -// ScriptSteps injects the script for each step in a yaml configuration. -func (c *Client) ScriptSteps(s yaml.StepSlice) (yaml.StepSlice, error) { - // iterate through all steps - for _, step := range s { - // skip if no commands block for the step - if len(step.Commands) == 0 { - continue - } - - // set the default home - home := constants.DefaultHomeDir - - // override the home value if user is defined - if step.User != "" { - home = fmt.Sprintf("/home/%s", step.User) - } - - // if user provides a home directory, use that - if override, ok := step.Environment["HOME"]; ok { - home = override - } - - // generate script from commands - script := generateScriptPosix(step.Commands) - - // set the entrypoint for the step - step.Entrypoint = []string{constants.DefaultShell, "-c"} - - // set the commands for the step - step.Commands = []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"} - - // set the environment variables for the step - step.Environment["VELA_BUILD_SCRIPT"] = script - step.Environment["HOME"] = home - - step.Environment["SHELL"] = constants.DefaultShell - } - - return s, nil -} - -// generateScriptPosix is a helper function that generates a build script -// for a linux container using the given commands. -func generateScriptPosix(commands []string) string { - var buf bytes.Buffer - - // iterate through each command provided - for _, command := range commands { - // safely escape entire command - escaped := fmt.Sprintf("%q", command) - - // safely escape trace character - escaped = strings.Replace(escaped, "$", `\$`, -1) - - // write escaped lines to buffer - buf.WriteString(fmt.Sprintf( - traceScript, - escaped, - command, - )) - } - - // create build script with netrc and buffer information - script := fmt.Sprintf( - setupScript, - buf.String(), - ) - - return base64.StdEncoding.EncodeToString([]byte(script)) -} - -// setupScript is a helper script this is added to the build to ensure -// a minimum set of environment variables are set correctly. -const setupScript = ` -cat < $HOME/.netrc -machine $VELA_NETRC_MACHINE -login $VELA_NETRC_USERNAME -password $VELA_NETRC_PASSWORD -EOF -chmod 0600 $HOME/.netrc -unset VELA_NETRC_MACHINE -unset VELA_NETRC_USERNAME -unset VELA_NETRC_PASSWORD -unset VELA_BUILD_SCRIPT -%s -` - -// traceScript is a helper script that is added to the build script -// to trace a command. -const traceScript = ` -echo $ %s -%s -` diff --git a/compiler/native/script_test.go b/compiler/native/script_test.go deleted file mode 100644 index e91560a0f..000000000 --- a/compiler/native/script_test.go +++ /dev/null @@ -1,326 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -package native - -import ( - "context" - "reflect" - "testing" - - "github.com/google/go-cmp/cmp" - - "github.com/go-vela/server/compiler/types/yaml" - "github.com/go-vela/server/constants" -) - -func TestNative_ScriptStages(t *testing.T) { - // setup types - baseEnv := environment(nil, nil, nil, nil, nil, 0) - - s := yaml.StageSlice{ - &yaml.Stage{ - Name: "install", - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: []string{"./gradlew downloadDependencies"}, - Environment: baseEnv, - Image: "openjdk:latest", - Name: "install", - Pull: "always", - }, - }, - }, - &yaml.Stage{ - Name: "test", - Needs: []string{"install"}, - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: []string{"./gradlew check"}, - Environment: baseEnv, - Image: "openjdk:latest", - Name: "test", - Pull: "always", - }, - }, - }, - } - - baseEnv["HOME"] = constants.DefaultHomeDir - baseEnv["SHELL"] = constants.DefaultShell - - installEnv := baseEnv - installEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew downloadDependencies"}) - testEnv := baseEnv - testEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew check"}) - - want := yaml.StageSlice{ - &yaml.Stage{ - Name: "install", - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: installEnv, - Image: "openjdk:latest", - Name: "install", - Pull: "always", - }, - }, - }, - &yaml.Stage{ - Name: "test", - Needs: []string{"install"}, - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: testEnv, - Image: "openjdk:latest", - Name: "test", - Pull: "always", - }, - }, - }, - } - - // run test - compiler, err := FromCLICommand(context.Background(), testCommand(t, "http://foo.example.com")) - if err != nil { - t.Errorf("Creating compiler returned err: %v", err) - } - - got, err := compiler.ScriptStages(s) - if err != nil { - t.Errorf("ScriptStages returned err: %v", err) - } - - if !reflect.DeepEqual(got, want) { - t.Errorf("ScriptStages is %v, want %v", got, want) - } -} - -func TestNative_ScriptSteps(t *testing.T) { - // setup types - emptyEnv := environment(nil, nil, nil, nil, nil, 0) - - baseEnv := emptyEnv - baseEnv["HOME"] = constants.DefaultHomeDir - baseEnv["SHELL"] = constants.DefaultShell - - installEnv := baseEnv - installEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew downloadDependencies"}) - - testEnv := baseEnv - testEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew check"}) - - newHomeEnv := baseEnv - newHomeEnv["HOME"] = "/usr/share/test" - - type args struct { - s yaml.StepSlice - } - - tests := []struct { - name string - args args - want yaml.StepSlice - wantErr bool - }{ - {"no user defined", args{s: yaml.StepSlice{ - &yaml.Step{ - Commands: []string{"./gradlew downloadDependencies"}, - Environment: baseEnv, - Image: "openjdk:latest", - Name: "install", - Pull: "always", - }, - &yaml.Step{ - Commands: []string{"./gradlew check"}, - Environment: baseEnv, - Image: "openjdk:latest", - Name: "test", - Pull: "always", - }, - }}, yaml.StepSlice{ - &yaml.Step{ - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: installEnv, - Image: "openjdk:latest", - Name: "install", - Pull: "always", - }, - &yaml.Step{ - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: testEnv, - Image: "openjdk:latest", - Name: "test", - Pull: "always", - }, - }, false}, - {"root user defined", args{s: yaml.StepSlice{ - &yaml.Step{ - Commands: []string{"./gradlew downloadDependencies"}, - Environment: baseEnv, - Image: "openjdk:latest", - Name: "install", - User: "root", - Pull: "always", - }, - &yaml.Step{ - Commands: []string{"./gradlew check"}, - Environment: baseEnv, - Image: "openjdk:latest", - Name: "test", - User: "root", - Pull: "always", - }, - }}, yaml.StepSlice{ - &yaml.Step{ - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: installEnv, - Image: "openjdk:latest", - Name: "install", - User: "root", - Pull: "always", - }, - &yaml.Step{ - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: testEnv, - Image: "openjdk:latest", - Name: "test", - User: "root", - Pull: "always", - }, - }, false}, - {"foo user defined", args{s: yaml.StepSlice{ - &yaml.Step{ - Commands: []string{"./gradlew downloadDependencies"}, - Environment: baseEnv, - Image: "openjdk:latest", - Name: "install", - User: "foo", - Pull: "always", - }, - &yaml.Step{ - Commands: []string{"./gradlew check"}, - Environment: baseEnv, - Image: "openjdk:latest", - Name: "test", - User: "foo", - Pull: "always", - }, - }}, yaml.StepSlice{ - &yaml.Step{ - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: installEnv, - Image: "openjdk:latest", - Name: "install", - User: "foo", - Pull: "always", - }, - &yaml.Step{ - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: testEnv, - Image: "openjdk:latest", - Name: "test", - User: "foo", - Pull: "always", - }, - }, false}, - {"user with home dir override", args{s: yaml.StepSlice{ - &yaml.Step{ - Commands: []string{"./gradlew downloadDependencies"}, - Environment: newHomeEnv, - Image: "openjdk:latest", - Name: "install", - User: "test", - Pull: "always", - }, - &yaml.Step{ - Commands: []string{"./gradlew check"}, - Environment: newHomeEnv, - Image: "openjdk:latest", - Name: "test", - User: "test", - Pull: "always", - }, - }}, yaml.StepSlice{ - &yaml.Step{ - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: newHomeEnv, - Image: "openjdk:latest", - Name: "install", - User: "test", - Pull: "always", - }, - &yaml.Step{ - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: newHomeEnv, - Image: "openjdk:latest", - Name: "test", - User: "test", - Pull: "always", - }, - }, false}, - {"empty env - no user", args{s: yaml.StepSlice{ - &yaml.Step{ - Commands: []string{"./gradlew downloadDependencies"}, - Environment: emptyEnv, - Image: "openjdk:latest", - Name: "install", - Pull: "always", - }, - &yaml.Step{ - Commands: []string{"./gradlew check"}, - Environment: emptyEnv, - Image: "openjdk:latest", - Name: "test", - Pull: "always", - }, - }}, yaml.StepSlice{ - &yaml.Step{ - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: installEnv, - Image: "openjdk:latest", - Name: "install", - Pull: "always", - }, - &yaml.Step{ - Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, - Entrypoint: []string{constants.DefaultShell, "-c"}, - Environment: testEnv, - Image: "openjdk:latest", - Name: "test", - Pull: "always", - }, - }, false}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - compiler, err := FromCLICommand(context.Background(), testCommand(t, "http://foo.example.com")) - if err != nil { - t.Errorf("Creating compiler returned err: %v", err) - } - - got, err := compiler.ScriptSteps(tt.args.s) - if (err != nil) != tt.wantErr { - t.Errorf("ScriptSteps() error = %v, wantErr %v", err, tt.wantErr) - return - } - - if diff := cmp.Diff(tt.want, got); diff != "" { - t.Errorf("ScriptSteps() mismatch (-want +got):\n%s", diff) - } - }) - } -} diff --git a/compiler/types/pipeline/container.go b/compiler/types/pipeline/container.go index f2f609fc6..2b16097ad 100644 --- a/compiler/types/pipeline/container.go +++ b/compiler/types/pipeline/container.go @@ -4,6 +4,7 @@ package pipeline import ( "bytes" + "encoding/base64" "encoding/json" "errors" "fmt" @@ -330,6 +331,70 @@ func (c *Container) Substitute() error { return nil } +func (c *Container) Script() { + if len(c.Commands) == 0 { + return + } + + // set the default home + home := constants.DefaultHomeDir + + // override the home value if user is defined + if c.User != "" { + home = fmt.Sprintf("/home/%s", c.User) + } + + // if user provides a home directory, use that + if override, ok := c.Environment["HOME"]; ok { + home = override + } + + // generate script from commands + script := generateScriptPosix(c.Commands) + + // set the entrypoint for the step + c.Entrypoint = []string{constants.DefaultShell, "-c"} + + // set the commands for the step + c.Commands = []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"} + + // set the environment variables for the step + c.Environment["VELA_BUILD_SCRIPT"] = script + c.Environment["HOME"] = home + + c.Environment["SHELL"] = constants.DefaultShell +} + +// generateScriptPosix is a helper function that generates a build script +// for a linux container using the given commands. +func generateScriptPosix(commands []string) string { + var buf bytes.Buffer + + // iterate through each command provided + for _, command := range commands { + // safely escape entire command + escaped := fmt.Sprintf("%q", command) + + // safely escape trace character + escaped = strings.Replace(escaped, "$", `\$`, -1) + + // write escaped lines to buffer + buf.WriteString(fmt.Sprintf( + traceScript, + escaped, + command, + )) + } + + // create build script with netrc and buffer information + script := fmt.Sprintf( + setupScript, + buf.String(), + ) + + return base64.StdEncoding.EncodeToString([]byte(script)) +} + // dnsSafeRandomString creates a lowercase alphanumeric string of length n. // Some kubernetes IDs must be dns-safe, so the character set and length is limited. // If an ID is too long, use this to generate a random suffix for a truncated ID. @@ -345,3 +410,26 @@ func dnsSafeRandomString(n int) string { return string(b) } + +// setupScript is a helper script this is added to the build to ensure +// a minimum set of environment variables are set correctly. +const setupScript = ` +cat < $HOME/.netrc +machine $VELA_NETRC_MACHINE +login $VELA_NETRC_USERNAME +password $VELA_NETRC_PASSWORD +EOF +chmod 0600 $HOME/.netrc +unset VELA_NETRC_MACHINE +unset VELA_NETRC_USERNAME +unset VELA_NETRC_PASSWORD +unset VELA_BUILD_SCRIPT +%s +` + +// traceScript is a helper script that is added to the build script +// to trace a command. +const traceScript = ` +echo $ %s +%s +` diff --git a/compiler/types/pipeline/container_test.go b/compiler/types/pipeline/container_test.go index dc43c480e..4ac6d2b1c 100644 --- a/compiler/types/pipeline/container_test.go +++ b/compiler/types/pipeline/container_test.go @@ -6,6 +6,8 @@ import ( "reflect" "testing" + "github.com/google/go-cmp/cmp" + "github.com/go-vela/server/constants" ) @@ -1033,6 +1035,62 @@ func TestPipeline_Container_Substitute(t *testing.T) { } } +func TestPipeline_Container_Script(t *testing.T) { + // setup types + baseEnv := make(map[string]string) + + s := &Container{ + Commands: []string{"./gradlew check"}, + Environment: baseEnv, + Image: "openjdk:latest", + Name: "test", + Pull: "always", + } + + baseEnv["HOME"] = constants.DefaultHomeDir + baseEnv["SHELL"] = constants.DefaultShell + + installEnv := baseEnv + installEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew downloadDependencies"}) + testEnv := baseEnv + testEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew check"}) + + want := &Container{ + Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"}, + Entrypoint: []string{constants.DefaultShell, "-c"}, + Environment: installEnv, + Image: "openjdk:latest", + Name: "test", + Pull: "always", + } + + s.Script() + + if diff := cmp.Diff(want, s); diff != "" { + t.Errorf("Script mismatch (-want +got):\n%s", diff) + } + + sNoCommands := &Container{ + Environment: baseEnv, + Image: "openjdk:latest", + Name: "test", + Pull: "always", + } + + wantNoCommands := &Container{ + Environment: testEnv, + Image: "openjdk:latest", + Name: "test", + Pull: "always", + } + + sNoCommands.Script() + + if diff := cmp.Diff(wantNoCommands, sNoCommands); diff != "" { + t.Errorf("Script mismatch (-want +got):\n%s", diff) + } +} + func testContainers() *ContainerSlice { return &ContainerSlice{ {