Skip to content
4 changes: 2 additions & 2 deletions compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ type Engine interface {

// ExpandStages defines a function that injects the template
// for each templated step in every stage in a yaml configuration.
ExpandStages(*yaml.Build, map[string]*yaml.Template) (*yaml.Build, error)
ExpandStages(*yaml.Build, map[string]*yaml.Template, *pipeline.RuleData) (*yaml.Build, error)
// ExpandSteps defines a function that injects the template
// for each templated step in a yaml configuration.
ExpandSteps(*yaml.Build, map[string]*yaml.Template) (*yaml.Build, error)
ExpandSteps(*yaml.Build, map[string]*yaml.Template, *pipeline.RuleData) (*yaml.Build, error)

// Init Compiler Interface Functions

Expand Down
8 changes: 4 additions & 4 deletions compiler/native/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (c *client) CompileLite(v interface{}, template, substitute bool, localTemp
switch {
case len(p.Stages) > 0:
// inject the templates into the steps
p, err = c.ExpandStages(p, templates)
p, err = c.ExpandStages(p, templates, nil)
if err != nil {
return nil, _pipeline, err
}
Expand All @@ -169,7 +169,7 @@ func (c *client) CompileLite(v interface{}, template, substitute bool, localTemp
}
case len(p.Steps) > 0:
// inject the templates into the steps
p, err = c.ExpandSteps(p, templates)
p, err = c.ExpandSteps(p, templates, nil)
if err != nil {
return nil, _pipeline, err
}
Expand Down Expand Up @@ -307,7 +307,7 @@ func (c *client) compileSteps(p *yaml.Build, _pipeline *library.Pipeline, tmpls
}

// inject the templates into the steps
p, err = c.ExpandSteps(p, tmpls)
p, err = c.ExpandSteps(p, tmpls, r)
if err != nil {
return nil, _pipeline, err
}
Expand Down Expand Up @@ -404,7 +404,7 @@ func (c *client) compileStages(p *yaml.Build, _pipeline *library.Pipeline, tmpls
}

// inject the templates into the stages
p, err = c.ExpandStages(p, tmpls)
p, err = c.ExpandStages(p, tmpls, r)
if err != nil {
return nil, _pipeline, err
}
Expand Down
23 changes: 20 additions & 3 deletions compiler/native/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/go-vela/types/constants"
"github.com/go-vela/types/pipeline"

"github.com/go-vela/server/compiler/registry"
"github.com/go-vela/server/compiler/template/native"
Expand All @@ -22,15 +23,15 @@ import (

// ExpandStages injects the template for each
// templated step in every stage in a yaml configuration.
func (c *client) ExpandStages(s *yaml.Build, tmpls map[string]*yaml.Template) (*yaml.Build, error) {
func (c *client) ExpandStages(s *yaml.Build, tmpls map[string]*yaml.Template, r *pipeline.RuleData) (*yaml.Build, error) {
if len(tmpls) == 0 {
return s, nil
}

// iterate through all stages
for _, stage := range s.Stages {
// inject the templates into the steps for the stage
p, err := c.ExpandSteps(&yaml.Build{Steps: stage.Steps, Secrets: s.Secrets, Services: s.Services, Environment: s.Environment}, tmpls)
p, err := c.ExpandSteps(&yaml.Build{Steps: stage.Steps, Secrets: s.Secrets, Services: s.Services, Environment: s.Environment}, tmpls, r)
if err != nil {
return nil, err
}
Expand All @@ -46,7 +47,7 @@ func (c *client) ExpandStages(s *yaml.Build, tmpls map[string]*yaml.Template) (*

// ExpandSteps injects the template for each
// templated step in a yaml configuration.
func (c *client) ExpandSteps(s *yaml.Build, tmpls map[string]*yaml.Template) (*yaml.Build, error) {
func (c *client) ExpandSteps(s *yaml.Build, tmpls map[string]*yaml.Template, r *pipeline.RuleData) (*yaml.Build, error) {
if len(tmpls) == 0 {
return s, nil
}
Expand Down Expand Up @@ -75,6 +76,22 @@ func (c *client) ExpandSteps(s *yaml.Build, tmpls map[string]*yaml.Template) (*y
return s, fmt.Errorf("missing template source for template %s in pipeline for step %s", step.Template.Name, step.Name)
}

// if ruledata is nil (CompileLite), continue with expansion
if r != nil {
// form a one-step pipeline to prep for purge check
check := &yaml.StepSlice{step}
pipeline := &pipeline.Build{
Steps: *check.ToPipeline(),
}

pipeline = pipeline.Purge(r)

// if step purged, do not proceed with expansion
if len(pipeline.Steps) == 0 {
continue
}
}

// Create some default global environment inject vars
// these are used below to overwrite to an empty
// map if they should not be injected into a container
Expand Down
38 changes: 34 additions & 4 deletions compiler/native/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"

"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
"github.com/go-vela/types/raw"
"github.com/go-vela/types/yaml"
"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -145,7 +146,7 @@ func TestNative_ExpandStages(t *testing.T) {
t.Errorf("Creating new compiler returned err: %v", err)
}

build, err := compiler.ExpandStages(&yaml.Build{Stages: stages, Services: yaml.ServiceSlice{}, Environment: raw.StringSliceMap{}}, tmpls)
build, err := compiler.ExpandStages(&yaml.Build{Stages: stages, Services: yaml.ServiceSlice{}, Environment: raw.StringSliceMap{}}, tmpls, new(pipeline.RuleData))
if err != nil {
t.Errorf("ExpandStages returned err: %v", err)
}
Expand Down Expand Up @@ -321,7 +322,7 @@ func TestNative_ExpandSteps(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
build, err := compiler.ExpandSteps(&yaml.Build{Steps: steps, Services: yaml.ServiceSlice{}, Environment: globalEnvironment}, test.tmpls)
build, err := compiler.ExpandSteps(&yaml.Build{Steps: steps, Services: yaml.ServiceSlice{}, Environment: globalEnvironment}, test.tmpls, new(pipeline.RuleData))
if err != nil {
t.Errorf("ExpandSteps_Type%s returned err: %v", test.name, err)
}
Expand Down Expand Up @@ -385,6 +386,11 @@ func TestNative_ExpandStepsMulti(t *testing.T) {
Source: "github.example.com/bar/foo/maven.yml",
Type: "github",
},
"npm": {
Name: "npm",
Source: "github.example.com/foo/bar/gradle.yml",
Type: "github",
},
}

steps := yaml.StepSlice{
Expand All @@ -409,6 +415,27 @@ func TestNative_ExpandStepsMulti(t *testing.T) {
"pull_policy": "pull: true",
},
},
Ruleset: yaml.Ruleset{
If: yaml.Rules{
Branch: []string{"main"},
},
},
},
&yaml.Step{
Name: "sample",
Template: yaml.StepTemplate{
Name: "npm",
Variables: map[string]interface{}{
"image": "openjdk:latest",
"environment": "{ GRADLE_USER_HOME: .gradle, GRADLE_OPTS: -Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false }",
"pull_policy": "pull: true",
},
},
Ruleset: yaml.Ruleset{
If: yaml.Rules{
Branch: []string{"dev"},
},
},
},
}

Expand Down Expand Up @@ -557,7 +584,10 @@ func TestNative_ExpandStepsMulti(t *testing.T) {
t.Errorf("Creating new compiler returned err: %v", err)
}

build, err := compiler.ExpandSteps(&yaml.Build{Steps: steps, Services: yaml.ServiceSlice{}, Environment: raw.StringSliceMap{}}, tmpls)
ruledata := new(pipeline.RuleData)
ruledata.Branch = "main"

build, err := compiler.ExpandSteps(&yaml.Build{Steps: steps, Services: yaml.ServiceSlice{}, Environment: raw.StringSliceMap{}}, tmpls, ruledata)
if err != nil {
t.Errorf("ExpandSteps returned err: %v", err)
}
Expand Down Expand Up @@ -644,7 +674,7 @@ func TestNative_ExpandStepsStarlark(t *testing.T) {
t.Errorf("Creating new compiler returned err: %v", err)
}

build, err := compiler.ExpandSteps(&yaml.Build{Steps: steps, Secrets: yaml.SecretSlice{}, Services: yaml.ServiceSlice{}, Environment: raw.StringSliceMap{}}, tmpls)
build, err := compiler.ExpandSteps(&yaml.Build{Steps: steps, Secrets: yaml.SecretSlice{}, Services: yaml.ServiceSlice{}, Environment: raw.StringSliceMap{}}, tmpls, new(pipeline.RuleData))
if err != nil {
t.Errorf("ExpandSteps returned err: %v", err)
}
Expand Down