Skip to content

Commit

Permalink
feat(local exec): add ability to skip steps
Browse files Browse the repository at this point in the history
  • Loading branch information
KellyMerrick committed Nov 7, 2024
1 parent 55c73d4 commit a51e7aa
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
56 changes: 56 additions & 0 deletions action/pipeline/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/signal"
"path/filepath"
"slices"
"strings"
"syscall"

Expand Down Expand Up @@ -114,6 +115,61 @@ func (c *Config) Exec(client compiler.Engine) error {
return err
}

// create a slice for steps to be removed
stepsToRemove := c.Steps

// print steps to be removed to the user
if len(stepsToRemove) > 0 {
for _, stepName := range stepsToRemove {
fmt.Println("skip step: ", stepName)
}
}

// filter out steps to be removed
if len(_pipeline.Stages) > 0 {

Check failure on line 129 in action/pipeline/exec.go

View workflow job for this annotation

GitHub Actions / diff-review

block should not start with a whitespace (wsl)

Check failure on line 129 in action/pipeline/exec.go

View workflow job for this annotation

GitHub Actions / full-review

block should not start with a whitespace (wsl)

Check failure on line 129 in action/pipeline/exec.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/pipeline/exec.go#L129

block should not start with a whitespace (wsl)
Raw output
action/pipeline/exec.go:129:32: block should not start with a whitespace (wsl)
	if len(_pipeline.Stages) > 0 {
	                              ^
// if using stages

// counter for total steps to run
totalSteps := 0

for i, stage := range _pipeline.Stages {

Check failure on line 135 in action/pipeline/exec.go

View workflow job for this annotation

GitHub Actions / diff-review

unnecessary leading newline (whitespace)

Check failure on line 135 in action/pipeline/exec.go

View workflow job for this annotation

GitHub Actions / full-review

unnecessary leading newline (whitespace)

Check failure on line 135 in action/pipeline/exec.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/pipeline/exec.go#L135

unnecessary leading newline (whitespace)
Raw output
action/pipeline/exec.go:135:43: unnecessary leading newline (whitespace)

^

filteredStageSteps := stage.Steps[:0]

for _, step := range stage.Steps {
// if c.steps contains step.Name
if !slices.Contains(stepsToRemove, step.Name) {
filteredStageSteps = append(filteredStageSteps, step)
totalSteps++
}

_pipeline.Stages[i].Steps = filteredStageSteps
}
}

// check if any steps are left to run, excluding "init" step
if totalSteps <= 1 {
return fmt.Errorf("no steps left to run after removing skipped steps")
}

} else {

Check failure on line 155 in action/pipeline/exec.go

View workflow job for this annotation

GitHub Actions / diff-review

unnecessary trailing newline (whitespace)

Check failure on line 155 in action/pipeline/exec.go

View workflow job for this annotation

GitHub Actions / full-review

unnecessary trailing newline (whitespace)

Check failure on line 155 in action/pipeline/exec.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/pipeline/exec.go#L155

unnecessary trailing newline (whitespace)
Raw output
action/pipeline/exec.go:155:2: unnecessary trailing newline (whitespace)

^
// if not using stages

filteredSteps := _pipeline.Steps[:0]

for _, step := range _pipeline.Steps {
if !slices.Contains(stepsToRemove, step.Name) {
filteredSteps = append(filteredSteps, step)
}
}
_pipeline.Steps = filteredSteps

Check failure on line 165 in action/pipeline/exec.go

View workflow job for this annotation

GitHub Actions / diff-review

assignments should only be cuddled with other assignments (wsl)

Check failure on line 165 in action/pipeline/exec.go

View workflow job for this annotation

GitHub Actions / full-review

assignments should only be cuddled with other assignments (wsl)

Check failure on line 165 in action/pipeline/exec.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/pipeline/exec.go#L165

assignments should only be cuddled with other assignments (wsl)
Raw output
action/pipeline/exec.go:165:3: assignments should only be cuddled with other assignments (wsl)
		_pipeline.Steps = filteredSteps
		^

// check if any steps are left to run, excluding "init" step
if len(_pipeline.Steps) <= 1 {
return fmt.Errorf("no steps left to run after removing skipped steps")
}
}

// create current directory path for local mount
mount := fmt.Sprintf("%s:%s:rw", base, constants.WorkspaceDefault)

Expand Down
1 change: 1 addition & 0 deletions action/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Config struct {
Target string
Org string
Repo string
Steps []string
Ref string
File string
FileChangeset []string
Expand Down
26 changes: 20 additions & 6 deletions command/pipeline/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ var CommandExec = &cli.Command{
Value: constants.PipelineTypeYAML,
},

// Step Flags

&cli.StringSliceFlag{
EnvVars: []string{"VELA_SKIP_STEP", "SKIP_STEP"},
Name: "skip-step",
Aliases: []string{"sk", "skip"},
Usage: "skip a step in the pipeline",
},

// Compiler Template Flags

&cli.StringFlag{
Expand All @@ -151,7 +160,7 @@ var CommandExec = &cli.Command{
&cli.StringSliceFlag{
EnvVars: []string{"VELA_TEMPLATE_FILE", "PIPELINE_TEMPLATE_FILE"},
Name: "template-file",
Aliases: []string{"tf, tfs, template-files"},
Aliases: []string{"tf", "tfs", "template-files"},
Usage: "enables using a local template file for expansion in the form <name>:<path>",
},
&cli.IntFlag{
Expand Down Expand Up @@ -218,17 +227,21 @@ EXAMPLES:
$ {{.HelpName}} --volume /tmp/bar.txt:/tmp/bar.txt:rw
7. Execute a local Vela pipeline with type of go
$ {{.HelpName}} --pipeline-type go
8. Execute a local Vela pipeline with local templates
8. Execute a local Vela pipeline with specific step skipped
$ {{.HelpName}} --skip-step echo_hello --skip-step 'echo goodbye'
9. Execute a local Vela pipeline with specific template step skipped
$ {{.HelpName}} --skip-step <template name>_echo_hello --skip-step '<template name>_echo goodbye'
10. Execute a local Vela pipeline with local templates
$ {{.HelpName}} --template-file <template_name>:<path_to_template>
9. Execute a local Vela pipeline with specific environment variables
11. Execute a local Vela pipeline with specific environment variables
$ {{.HelpName}} --env KEY1=VAL1,KEY2=VAL2
10. Execute a local Vela pipeline with your existing local environment loaded into pipeline
12. Execute a local Vela pipeline with your existing local environment loaded into pipeline
$ {{.HelpName}} --local-env
11. Execute a local Vela pipeline with an environment file loaded in
13. Execute a local Vela pipeline with an environment file loaded in
$ {{.HelpName}} --env-file (uses .env by default)
OR
$ {{.HelpName}} --env-file-path <path_to_file>
12. Execute a local Vela pipeline using remote templates
14. Execute a local Vela pipeline using remote templates
$ {{.HelpName}} --compiler.github.token <GITHUB_PAT> --compiler.github.url <GITHUB_URL>
DOCUMENTATION:
Expand Down Expand Up @@ -296,6 +309,7 @@ func exec(c *cli.Context) error {
Target: c.String("target"),
Org: c.String(internal.FlagOrg),
Repo: c.String(internal.FlagRepo),
Steps: c.StringSlice("skip-step"),
File: c.String("file"),
FileChangeset: c.StringSlice("file-changeset"),
TemplateFiles: c.StringSlice("template-file"),
Expand Down

0 comments on commit a51e7aa

Please sign in to comment.