diff --git a/compiler/engine.go b/compiler/engine.go index 9addf7a79..f2ff2a33e 100644 --- a/compiler/engine.go +++ b/compiler/engine.go @@ -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 diff --git a/compiler/native/compile.go b/compiler/native/compile.go index 4b80e3365..a36ddc113 100644 --- a/compiler/native/compile.go +++ b/compiler/native/compile.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/compiler/native/expand.go b/compiler/native/expand.go index e17d03161..4256de7e8 100644 --- a/compiler/native/expand.go +++ b/compiler/native/expand.go @@ -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" @@ -22,7 +23,7 @@ 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 } @@ -30,7 +31,7 @@ func (c *client) ExpandStages(s *yaml.Build, tmpls map[string]*yaml.Template) (* // 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 } @@ -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 } @@ -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 diff --git a/compiler/native/expand_test.go b/compiler/native/expand_test.go index ae3fc32b7..a8c232a71 100644 --- a/compiler/native/expand_test.go +++ b/compiler/native/expand_test.go @@ -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" @@ -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) } @@ -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) } @@ -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{ @@ -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"}, + }, + }, }, } @@ -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) } @@ -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) }