Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(local exec): add ability to skip steps #584

Merged
merged 9 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 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 All @@ -16,6 +17,7 @@ import (
"github.com/go-vela/cli/version"
api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/compiler"
"github.com/go-vela/server/compiler/types/pipeline"
"github.com/go-vela/server/constants"
"github.com/go-vela/worker/executor"
"github.com/go-vela/worker/runtime"
Expand Down Expand Up @@ -114,6 +116,20 @@ func (c *Config) Exec(client compiler.Engine) error {
return err
}

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

// print and remove steps
if len(stepsToRemove) > 0 {
for _, stepName := range stepsToRemove {
fmt.Println("skip step: ", stepName)
KellyMerrick marked this conversation as resolved.
Show resolved Hide resolved
}

if err := skipSteps(_pipeline, stepsToRemove); err != nil {
return err
}
}

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

Expand Down Expand Up @@ -214,3 +230,48 @@ func (c *Config) Exec(client compiler.Engine) error {

return nil
}

// skipSteps filters out steps to be removed from the pipeline.
func skipSteps(_pipeline *pipeline.Build, stepsToRemove []string) error {
// filter out steps to be removed
if len(_pipeline.Stages) > 0 {
// counter for total steps to run
totalSteps := 0

for i, stage := range _pipeline.Stages {
filteredStageSteps := stage.Steps[:0]

for _, step := range stage.Steps {
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 {
// 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 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")
}
}

return nil
}
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
SkipSteps []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),
SkipSteps: c.StringSlice("skip-step"),
File: c.String("file"),
FileChangeset: c.StringSlice("file-changeset"),
TemplateFiles: c.StringSlice("template-file"),
Expand Down
Loading