Skip to content

Commit

Permalink
enhance(templates): inject template name as variable (#1096)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed Mar 29, 2024
1 parent 5706d0f commit 5af11c2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
8 changes: 8 additions & 0 deletions compiler/native/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ func (c *client) compileInline(p *yaml.Build, depth int) (*yaml.Build, error) {
format = constants.PipelineTypeGo
}

// initialize variable map if not parsed from config
if len(template.Variables) == 0 {
template.Variables = make(map[string]interface{})
}

// inject template name into variables
template.Variables["VELA_TEMPLATE_NAME"] = template.Name

parsed, _, err := c.Parse(bytes, format, template)
if err != nil {
return nil, err
Expand Down
44 changes: 25 additions & 19 deletions compiler/native/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ func TestNative_Compile_StagesPipelineTemplate(t *testing.T) {
buildEnv["GRADLE_USER_HOME"] = ".gradle"
buildEnv["HOME"] = "/root"
buildEnv["SHELL"] = "/bin/sh"
buildEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew build"})
buildEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew build", "echo gradle"})
buildEnv["bar"] = "test4"
buildEnv["star"] = "test3"

Expand Down Expand Up @@ -951,7 +951,7 @@ func TestNative_Compile_StepsPipelineTemplate(t *testing.T) {
buildEnv["GRADLE_USER_HOME"] = ".gradle"
buildEnv["HOME"] = "/root"
buildEnv["SHELL"] = "/bin/sh"
buildEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew build"})
buildEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew build", "echo gradle"})
buildEnv["bar"] = "test4"
buildEnv["star"] = "test3"

Expand Down Expand Up @@ -3230,17 +3230,21 @@ func Test_CompileLite(t *testing.T) {
},
Templates: []*yaml.Template{
{
Name: "golang",
Source: "github.example.com/github/octocat/golang_inline_stages.yml",
Format: "golang",
Type: "github",
Variables: map[string]any{"image": string("golang:latest")},
Name: "golang",
Source: "github.example.com/github/octocat/golang_inline_stages.yml",
Format: "golang",
Type: "github",
Variables: map[string]any{
"image": string("golang:latest"),
"VELA_TEMPLATE_NAME": string("golang"),
},
},
{
Name: "starlark",
Source: "github.example.com/github/octocat/starlark_inline_stages.star",
Format: "starlark",
Type: "github",
Name: "starlark",
Source: "github.example.com/github/octocat/starlark_inline_stages.star",
Format: "starlark",
Type: "github",
Variables: map[string]any{"VELA_TEMPLATE_NAME": string("starlark")},
},
},
Environment: raw.StringSliceMap{},
Expand Down Expand Up @@ -3375,16 +3379,18 @@ func Test_CompileLite(t *testing.T) {
Environment: raw.StringSliceMap{},
Templates: yaml.TemplateSlice{
{
Name: "golang",
Source: "github.example.com/github/octocat/golang_inline_steps.yml",
Format: "golang",
Type: "github",
Name: "golang",
Source: "github.example.com/github/octocat/golang_inline_steps.yml",
Format: "golang",
Type: "github",
Variables: map[string]any{"VELA_TEMPLATE_NAME": string("golang")},
},
{
Name: "starlark",
Source: "github.example.com/github/octocat/starlark_inline_steps.star",
Format: "starlark",
Type: "github",
Name: "starlark",
Source: "github.example.com/github/octocat/starlark_inline_steps.star",
Format: "starlark",
Type: "github",
Variables: map[string]any{"VELA_TEMPLATE_NAME": string("starlark")},
},
},
},
Expand Down
8 changes: 8 additions & 0 deletions compiler/native/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ func (c *client) ExpandSteps(s *yaml.Build, tmpls map[string]*yaml.Template, r *
return s, err
}

// initialize variable map if not parsed from config
if len(step.Template.Variables) == 0 {
step.Template.Variables = make(map[string]interface{})
}

// inject template name into variables
step.Template.Variables["VELA_TEMPLATE_NAME"] = step.Template.Name

tmplBuild, err := c.mergeTemplate(bytes, tmpl, step)
if err != nil {
return s, err
Expand Down
6 changes: 3 additions & 3 deletions compiler/native/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestNative_ExpandStages(t *testing.T) {
Pull: "always",
},
&yaml.Step{
Commands: []string{"./gradlew build"},
Commands: []string{"./gradlew build", "echo gradle"},
Environment: raw.StringSliceMap{
"GRADLE_OPTS": "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false",
"GRADLE_USER_HOME": ".gradle",
Expand Down Expand Up @@ -301,7 +301,7 @@ func TestNative_ExpandSteps(t *testing.T) {
Pull: "always",
},
&yaml.Step{
Commands: []string{"./gradlew build"},
Commands: []string{"./gradlew build", "echo gradle"},
Environment: raw.StringSliceMap{
"GRADLE_OPTS": "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false",
"GRADLE_USER_HOME": ".gradle",
Expand Down Expand Up @@ -823,7 +823,7 @@ func TestNative_ExpandSteps_TemplateCallTemplate(t *testing.T) {
Pull: "always",
},
&yaml.Step{
Commands: []string{"./gradlew build"},
Commands: []string{"./gradlew build", "echo test"},
Environment: raw.StringSliceMap{
"GRADLE_OPTS": "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false",
"GRADLE_USER_HOME": ".gradle",
Expand Down
1 change: 1 addition & 0 deletions compiler/native/testdata/long_template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ steps:
- name: build
commands:
- ./gradlew build
- echo {{ .VELA_TEMPLATE_NAME }}
environment: {{ .environment }}
image: {{ .image }}
{{ .pull_policy }}
Expand Down

0 comments on commit 5af11c2

Please sign in to comment.