Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
feat: inject the init stage/step to the pipeline (#20)
Browse files Browse the repository at this point in the history
* docs(clone): update description for clone stage/step

* feat: add logic for an init stage/step

* feat: inject the init stage/step

* improvement: simplify appending stages/steps
  • Loading branch information
jbrockopp authored and wass3r committed Dec 4, 2019
1 parent 9227388 commit 26835f6
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 32 deletions.
13 changes: 11 additions & 2 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ type Engine interface {
// Clone Compiler Interface Functions

// CloneStage defines a function that injects the
// stage clone process into a yaml configuration.
// clone stage process into a yaml configuration.
CloneStage(*yaml.Build) (*yaml.Build, error)
// CloneStep defines a function that injects the
// step clone process into a yaml configuration.
// clone step process into a yaml configuration.
CloneStep(*yaml.Build) (*yaml.Build, error)

// Environment Compiler Interface Functions
Expand All @@ -55,6 +55,15 @@ type Engine interface {
// for each templated step in a yaml configuration.
ExpandSteps(yaml.StepSlice, map[string]*yaml.Template) (yaml.StepSlice, error)

// Init Compiler Interface Functions

// InitStage defines a function that injects the
// init stage process into a yaml configuration.
InitStage(*yaml.Build) (*yaml.Build, error)
// CloneStep defines a function that injects the
// init step process into a yaml configuration.
InitStep(*yaml.Build) (*yaml.Build, error)

// Script Compiler Interface Functions

// ScriptStages defines a function that injects the script
Expand Down
12 changes: 4 additions & 8 deletions compiler/native/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
cloneStepName = "clone"
)

// CloneStage injects the stage clone process into a yaml configuration.
// CloneStage injects the clone stage process into a yaml configuration.
func (c *client) CloneStage(p *yaml.Build) (*yaml.Build, error) {
stages := yaml.StageSlice{}

Expand All @@ -39,17 +39,15 @@ func (c *client) CloneStage(p *yaml.Build) (*yaml.Build, error) {
stages = append(stages, clone)

// add existing stages after clone stage
for _, stage := range p.Stages {
stages = append(stages, stage)
}
stages = append(stages, p.Stages...)

// overwrite existing stages
p.Stages = stages

return p, nil
}

// CloneStep injects the step clone process into a yaml configuration.
// CloneStep injects the clone step process into a yaml configuration.
func (c *client) CloneStep(p *yaml.Build) (*yaml.Build, error) {
steps := yaml.StepSlice{}

Expand All @@ -66,9 +64,7 @@ func (c *client) CloneStep(p *yaml.Build) (*yaml.Build, error) {
steps = append(steps, clone)

// add existing steps after clone step
for _, step := range p.Steps {
steps = append(steps, step)
}
steps = append(steps, p.Steps...)

// overwrite existing steps
p.Steps = steps
Expand Down
12 changes: 12 additions & 0 deletions compiler/native/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ func (c *client) Compile(v interface{}) (*pipeline.Build, error) {
return nil, err
}

// inject the init stage
p, err = c.InitStage(p)
if err != nil {
return nil, err
}

// inject the templates into the stages
p.Stages, err = c.ExpandStages(p.Stages, tmpls)
if err != nil {
Expand Down Expand Up @@ -72,6 +78,12 @@ func (c *client) Compile(v interface{}) (*pipeline.Build, error) {
return nil, err
}

// inject the init step
p, err = c.InitStep(p)
if err != nil {
return nil, err
}

// inject the templates into the steps
p.Steps, err = c.ExpandSteps(p.Steps, tmpls)
if err != nil {
Expand Down
94 changes: 72 additions & 22 deletions compiler/native/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ func TestNative_Compile_StagesPipeline(t *testing.T) {
Template: false,
},
Stages: pipeline.StageSlice{
&pipeline.Stage{
Name: "init",
Steps: pipeline.ContainerSlice{
&pipeline.Container{
ID: "__0_init_init",
Environment: environment(nil, nil, nil),
Image: "#init",
Name: "init",
Number: 1,
Pull: true,
},
},
},
&pipeline.Stage{
Name: "clone",
Steps: pipeline.ContainerSlice{
Expand All @@ -64,7 +77,7 @@ func TestNative_Compile_StagesPipeline(t *testing.T) {
Environment: environment(nil, nil, nil),
Image: "target/vela-git:v0.1.0",
Name: "clone",
Number: 1,
Number: 2,
Pull: true,
},
},
Expand All @@ -80,7 +93,7 @@ func TestNative_Compile_StagesPipeline(t *testing.T) {
Environment: installEnv,
Image: "openjdk:latest",
Name: "install",
Number: 2,
Number: 3,
Pull: true,
},
},
Expand All @@ -96,7 +109,7 @@ func TestNative_Compile_StagesPipeline(t *testing.T) {
Environment: testEnv,
Image: "openjdk:latest",
Name: "test",
Number: 3,
Number: 4,
Pull: true,
},
},
Expand All @@ -112,7 +125,7 @@ func TestNative_Compile_StagesPipeline(t *testing.T) {
Environment: buildEnv,
Image: "openjdk:latest",
Name: "build",
Number: 4,
Number: 5,
Pull: true,
},
},
Expand All @@ -126,7 +139,7 @@ func TestNative_Compile_StagesPipeline(t *testing.T) {
Image: "plugins/docker:18.09",
Environment: dockerEnv,
Name: "publish",
Number: 5,
Number: 6,
Pull: true,
Secrets: pipeline.StepSecretSlice{
&pipeline.StepSecret{
Expand Down Expand Up @@ -217,12 +230,20 @@ func TestNative_Compile_StepsPipeline(t *testing.T) {
Template: false,
},
Steps: pipeline.ContainerSlice{
&pipeline.Container{
ID: "step___0_init",
Environment: environment(nil, nil, nil),
Image: "#init",
Name: "init",
Number: 1,
Pull: true,
},
&pipeline.Container{
ID: "step___0_clone",
Environment: environment(nil, nil, nil),
Image: "target/vela-git:v0.1.0",
Name: "clone",
Number: 1,
Number: 2,
Pull: true,
},
&pipeline.Container{
Expand All @@ -232,7 +253,7 @@ func TestNative_Compile_StepsPipeline(t *testing.T) {
Environment: installEnv,
Image: "openjdk:latest",
Name: "install",
Number: 2,
Number: 3,
Pull: true,
},
&pipeline.Container{
Expand All @@ -242,7 +263,7 @@ func TestNative_Compile_StepsPipeline(t *testing.T) {
Environment: testEnv,
Image: "openjdk:latest",
Name: "test",
Number: 3,
Number: 4,
Pull: true,
},
&pipeline.Container{
Expand All @@ -252,15 +273,15 @@ func TestNative_Compile_StepsPipeline(t *testing.T) {
Environment: buildEnv,
Image: "openjdk:latest",
Name: "build",
Number: 4,
Number: 5,
Pull: true,
},
&pipeline.Container{
ID: "step___0_publish",
Image: "plugins/docker:18.09",
Environment: dockerEnv,
Name: "publish",
Number: 5,
Number: 6,
Pull: true,
Secrets: pipeline.StepSecretSlice{
&pipeline.StepSecret{
Expand Down Expand Up @@ -366,6 +387,19 @@ func TestNative_Compile_StagesPipelineTemplate(t *testing.T) {
Template: false,
},
Stages: pipeline.StageSlice{
&pipeline.Stage{
Name: "init",
Steps: pipeline.ContainerSlice{
&pipeline.Container{
ID: "__0_init_init",
Environment: environment(nil, nil, nil),
Image: "#init",
Name: "init",
Number: 1,
Pull: true,
},
},
},
&pipeline.Stage{
Name: "clone",
Steps: pipeline.ContainerSlice{
Expand All @@ -374,7 +408,7 @@ func TestNative_Compile_StagesPipelineTemplate(t *testing.T) {
Environment: environment(nil, nil, nil),
Image: "target/vela-git:v0.1.0",
Name: "clone",
Number: 1,
Number: 2,
Pull: true,
},
},
Expand All @@ -390,7 +424,7 @@ func TestNative_Compile_StagesPipelineTemplate(t *testing.T) {
Environment: installEnv,
Image: "openjdk:latest",
Name: "sample_install",
Number: 2,
Number: 3,
Pull: true,
},
&pipeline.Container{
Expand All @@ -400,7 +434,7 @@ func TestNative_Compile_StagesPipelineTemplate(t *testing.T) {
Environment: testEnv,
Image: "openjdk:latest",
Name: "sample_test",
Number: 3,
Number: 4,
Pull: true,
},
&pipeline.Container{
Expand All @@ -410,7 +444,7 @@ func TestNative_Compile_StagesPipelineTemplate(t *testing.T) {
Environment: buildEnv,
Image: "openjdk:latest",
Name: "sample_build",
Number: 4,
Number: 5,
Pull: true,
},
},
Expand All @@ -424,7 +458,7 @@ func TestNative_Compile_StagesPipelineTemplate(t *testing.T) {
Image: "plugins/docker:18.09",
Environment: dockerEnv,
Name: "publish",
Number: 5,
Number: 6,
Pull: true,
Secrets: pipeline.StepSecretSlice{
&pipeline.StepSecret{
Expand Down Expand Up @@ -532,12 +566,20 @@ func TestNative_Compile_StepsPipelineTemplate(t *testing.T) {
Template: false,
},
Steps: pipeline.ContainerSlice{
&pipeline.Container{
ID: "step___0_init",
Environment: environment(nil, nil, nil),
Image: "#init",
Name: "init",
Number: 1,
Pull: true,
},
&pipeline.Container{
ID: "step___0_clone",
Environment: environment(nil, nil, nil),
Image: "target/vela-git:v0.1.0",
Name: "clone",
Number: 1,
Number: 2,
Pull: true,
},
&pipeline.Container{
Expand All @@ -547,7 +589,7 @@ func TestNative_Compile_StepsPipelineTemplate(t *testing.T) {
Environment: installEnv,
Image: "openjdk:latest",
Name: "sample_install",
Number: 2,
Number: 3,
Pull: true,
},
&pipeline.Container{
Expand All @@ -557,7 +599,7 @@ func TestNative_Compile_StepsPipelineTemplate(t *testing.T) {
Environment: testEnv,
Image: "openjdk:latest",
Name: "sample_test",
Number: 3,
Number: 4,
Pull: true,
},
&pipeline.Container{
Expand All @@ -567,15 +609,15 @@ func TestNative_Compile_StepsPipelineTemplate(t *testing.T) {
Environment: buildEnv,
Image: "openjdk:latest",
Name: "sample_build",
Number: 4,
Number: 5,
Pull: true,
},
&pipeline.Container{
ID: "step___0_docker",
Image: "plugins/docker:18.09",
Environment: dockerEnv,
Name: "docker",
Number: 5,
Number: 6,
Pull: true,
Secrets: pipeline.StepSecretSlice{
&pipeline.StepSecret{
Expand Down Expand Up @@ -664,20 +706,28 @@ func TestNative_Compile_InvalidType(t *testing.T) {
Template: false,
},
Steps: pipeline.ContainerSlice{
&pipeline.Container{
ID: "step___0_init",
Environment: environment(nil, nil, nil),
Image: "#init",
Name: "init",
Number: 1,
Pull: true,
},
&pipeline.Container{
ID: "step___0_clone",
Environment: environment(nil, nil, nil),
Image: "target/vela-git:v0.1.0",
Name: "clone",
Number: 1,
Number: 2,
Pull: true,
},
&pipeline.Container{
ID: "step___0_docker",
Image: "plugins/docker:18.09",
Environment: dockerEnv,
Name: "docker",
Number: 2,
Number: 3,
Pull: true,
Secrets: pipeline.StepSecretSlice{
&pipeline.StepSecret{
Expand Down
Loading

0 comments on commit 26835f6

Please sign in to comment.