Skip to content

Commit 441e1bf

Browse files
authored
feat(local exec): add ability to skip steps (#584)
* feat(local exec): add ability to skip steps * addressing linter errors * addressing linter errors * address feedback * addressing feedback * remove troubleshooting reference * move new code to new func skipSteps * move calling of skipSteps * use logrus to print to user
1 parent 55c73d4 commit 441e1bf

File tree

3 files changed

+82
-6
lines changed

3 files changed

+82
-6
lines changed

action/pipeline/exec.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"os/signal"
1010
"path/filepath"
11+
"slices"
1112
"strings"
1213
"syscall"
1314

@@ -16,6 +17,7 @@ import (
1617
"github.com/go-vela/cli/version"
1718
api "github.com/go-vela/server/api/types"
1819
"github.com/go-vela/server/compiler"
20+
"github.com/go-vela/server/compiler/types/pipeline"
1921
"github.com/go-vela/server/constants"
2022
"github.com/go-vela/worker/executor"
2123
"github.com/go-vela/worker/runtime"
@@ -114,6 +116,20 @@ func (c *Config) Exec(client compiler.Engine) error {
114116
return err
115117
}
116118

119+
// create a slice for steps to be removed
120+
stepsToRemove := c.SkipSteps
121+
122+
// print and remove steps
123+
if len(stepsToRemove) > 0 {
124+
for _, stepName := range stepsToRemove {
125+
logrus.Info("skipping step: ", stepName)
126+
}
127+
128+
if err := skipSteps(_pipeline, stepsToRemove); err != nil {
129+
return err
130+
}
131+
}
132+
117133
// create current directory path for local mount
118134
mount := fmt.Sprintf("%s:%s:rw", base, constants.WorkspaceDefault)
119135

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

215231
return nil
216232
}
233+
234+
// skipSteps filters out steps to be removed from the pipeline.
235+
func skipSteps(_pipeline *pipeline.Build, stepsToRemove []string) error {
236+
// filter out steps to be removed
237+
if len(_pipeline.Stages) > 0 {
238+
// counter for total steps to run
239+
totalSteps := 0
240+
241+
for i, stage := range _pipeline.Stages {
242+
filteredStageSteps := stage.Steps[:0]
243+
244+
for _, step := range stage.Steps {
245+
if !slices.Contains(stepsToRemove, step.Name) {
246+
filteredStageSteps = append(filteredStageSteps, step)
247+
totalSteps++
248+
}
249+
}
250+
251+
_pipeline.Stages[i].Steps = filteredStageSteps
252+
}
253+
254+
// check if any steps are left to run, excluding "init" step
255+
if totalSteps <= 1 {
256+
return fmt.Errorf("no steps left to run after removing skipped steps")
257+
}
258+
} else {
259+
// if not using stages
260+
filteredSteps := _pipeline.Steps[:0]
261+
262+
for _, step := range _pipeline.Steps {
263+
if !slices.Contains(stepsToRemove, step.Name) {
264+
filteredSteps = append(filteredSteps, step)
265+
}
266+
}
267+
268+
_pipeline.Steps = filteredSteps
269+
270+
// check if any steps are left to run, excluding "init" step
271+
if len(_pipeline.Steps) <= 1 {
272+
return fmt.Errorf("no steps left to run after removing skipped steps")
273+
}
274+
}
275+
276+
return nil
277+
}

action/pipeline/pipeline.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Config struct {
1616
Target string
1717
Org string
1818
Repo string
19+
SkipSteps []string
1920
Ref string
2021
File string
2122
FileChangeset []string

command/pipeline/exec.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ var CommandExec = &cli.Command{
134134
Value: constants.PipelineTypeYAML,
135135
},
136136

137+
// Step Flags
138+
139+
&cli.StringSliceFlag{
140+
EnvVars: []string{"VELA_SKIP_STEP", "SKIP_STEP"},
141+
Name: "skip-step",
142+
Aliases: []string{"sk", "skip"},
143+
Usage: "skip a step in the pipeline",
144+
},
145+
137146
// Compiler Template Flags
138147

139148
&cli.StringFlag{
@@ -151,7 +160,7 @@ var CommandExec = &cli.Command{
151160
&cli.StringSliceFlag{
152161
EnvVars: []string{"VELA_TEMPLATE_FILE", "PIPELINE_TEMPLATE_FILE"},
153162
Name: "template-file",
154-
Aliases: []string{"tf, tfs, template-files"},
163+
Aliases: []string{"tf", "tfs", "template-files"},
155164
Usage: "enables using a local template file for expansion in the form <name>:<path>",
156165
},
157166
&cli.IntFlag{
@@ -218,17 +227,21 @@ EXAMPLES:
218227
$ {{.HelpName}} --volume /tmp/bar.txt:/tmp/bar.txt:rw
219228
7. Execute a local Vela pipeline with type of go
220229
$ {{.HelpName}} --pipeline-type go
221-
8. Execute a local Vela pipeline with local templates
230+
8. Execute a local Vela pipeline with specific step skipped
231+
$ {{.HelpName}} --skip-step echo_hello --skip-step 'echo goodbye'
232+
9. Execute a local Vela pipeline with specific template step skipped
233+
$ {{.HelpName}} --skip-step <template name>_echo_hello --skip-step '<template name>_echo goodbye'
234+
10. Execute a local Vela pipeline with local templates
222235
$ {{.HelpName}} --template-file <template_name>:<path_to_template>
223-
9. Execute a local Vela pipeline with specific environment variables
236+
11. Execute a local Vela pipeline with specific environment variables
224237
$ {{.HelpName}} --env KEY1=VAL1,KEY2=VAL2
225-
10. Execute a local Vela pipeline with your existing local environment loaded into pipeline
238+
12. Execute a local Vela pipeline with your existing local environment loaded into pipeline
226239
$ {{.HelpName}} --local-env
227-
11. Execute a local Vela pipeline with an environment file loaded in
240+
13. Execute a local Vela pipeline with an environment file loaded in
228241
$ {{.HelpName}} --env-file (uses .env by default)
229242
OR
230243
$ {{.HelpName}} --env-file-path <path_to_file>
231-
12. Execute a local Vela pipeline using remote templates
244+
14. Execute a local Vela pipeline using remote templates
232245
$ {{.HelpName}} --compiler.github.token <GITHUB_PAT> --compiler.github.url <GITHUB_URL>
233246
234247
DOCUMENTATION:
@@ -296,6 +309,7 @@ func exec(c *cli.Context) error {
296309
Target: c.String("target"),
297310
Org: c.String(internal.FlagOrg),
298311
Repo: c.String(internal.FlagRepo),
312+
SkipSteps: c.StringSlice("skip-step"),
299313
File: c.String("file"),
300314
FileChangeset: c.StringSlice("file-changeset"),
301315
TemplateFiles: c.StringSlice("template-file"),

0 commit comments

Comments
 (0)