diff --git a/action/pipeline/validate.go b/action/pipeline/validate.go index 81729aa1..641fdc65 100644 --- a/action/pipeline/validate.go +++ b/action/pipeline/validate.go @@ -107,7 +107,7 @@ func (c *Config) ValidateLocal(client compiler.Engine) error { logrus.Tracef("compiling pipeline %s", path) // compile the object into a pipeline - p, _, err := client.CompileLite(path, c.Template, false, c.TemplateFiles) + p, _, err := client.CompileLite(path, c.Template, false) if err != nil { return err } diff --git a/action/pipeline/validate_test.go b/action/pipeline/validate_test.go index 2be7011c..74c4c1f1 100644 --- a/action/pipeline/validate_test.go +++ b/action/pipeline/validate_test.go @@ -284,7 +284,7 @@ func TestPipeline_Config_ValidateLocal(t *testing.T) { for _, test := range tests { isLocal := len(test.config.TemplateFiles) > 0 - err := test.config.ValidateLocal(client.WithLocal(isLocal)) + err := test.config.ValidateLocal(client.WithLocal(isLocal).WithLocalTemplates(test.config.TemplateFiles)) if test.failure { if err == nil { diff --git a/command/pipeline/exec.go b/command/pipeline/exec.go index 1ccf6631..4cd5daee 100644 --- a/command/pipeline/exec.go +++ b/command/pipeline/exec.go @@ -11,6 +11,7 @@ import ( "github.com/go-vela/cli/action/pipeline" "github.com/go-vela/cli/internal" "github.com/go-vela/server/compiler/native" + "github.com/go-vela/server/util" "github.com/go-vela/types/constants" "github.com/urfave/cli/v2" @@ -94,6 +95,12 @@ var CommandExec = &cli.Command{ Aliases: []string{"v"}, Usage: "provide list of local volumes to mount", }, + &cli.IntFlag{ + EnvVars: []string{"VELA_MAX_TEMPLATE_DEPTH", "MAX_TEMPLATE_DEPTH"}, + Name: "max-template-depth", + Usage: "set the maximum depth for nested templates", + Value: 3, + }, // Repo Flags @@ -184,6 +191,9 @@ func exec(c *cli.Context) error { return err } + // set the max template depth using provided configuration (max of 5) + client.TemplateDepth = util.MinInt(c.Int("max-template-depth"), 5) + // execute the exec call for the pipeline configuration // // https://pkg.go.dev/github.com/go-vela/cli/action/pipeline?tab=doc#Config.Exec diff --git a/command/pipeline/validate.go b/command/pipeline/validate.go index 1741d058..40a16468 100644 --- a/command/pipeline/validate.go +++ b/command/pipeline/validate.go @@ -12,8 +12,10 @@ import ( "github.com/go-vela/cli/internal" "github.com/go-vela/cli/internal/client" "github.com/go-vela/types/constants" + "github.com/sirupsen/logrus" "github.com/go-vela/server/compiler/native" + "github.com/go-vela/server/util" "github.com/urfave/cli/v2" ) @@ -80,6 +82,12 @@ var CommandValidate = &cli.Command{ Name: "template-file", Usage: "enables using a local template file for expansion", }, + &cli.IntFlag{ + EnvVars: []string{"VELA_MAX_TEMPLATE_DEPTH", "MAX_TEMPLATE_DEPTH"}, + Name: "max-template-depth", + Usage: "set the maximum depth for nested templates", + Value: 3, + }, &cli.BoolFlag{ EnvVars: []string{"VELA_REMOTE", "PIPELINE_REMOTE"}, Name: "remote", @@ -120,6 +128,8 @@ EXAMPLES: $ {{.HelpName}} --template 8. Validate a local template pipeline with expanding steps $ {{.HelpName}} --template --template-file name:/path/to/file + 9. Validate a local, nested template pipeline with custom template depth. + $ {{.HelpName}} --template --template-file name:/path/to/file name:/path/to/file --max-template-depth 2 DOCUMENTATION: https://go-vela.github.io/docs/reference/cli/pipeline/validate/ @@ -186,6 +196,13 @@ func validate(c *cli.Context) error { // set when user is sourcing templates from local machine if len(p.TemplateFiles) != 0 { client.WithLocal(true) + client.WithLocalTemplates(p.TemplateFiles) + client.TemplateDepth = c.Int("max-template-depth") + } else { + // set max template depth to minimum of 5 and provided value if local templates are not provided. + // This prevents users from spamming SCM + client.TemplateDepth = util.MinInt(c.Int("max-template-depth"), 5) + logrus.Debugf("no local template files provided, setting max template depth to %d", client.TemplateDepth) } // execute the validate local call for the pipeline configuration diff --git a/go.mod b/go.mod index ad44f2cb..7a6aba2f 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/gin-gonic/gin v1.9.1 github.com/go-git/go-git/v5 v5.7.0 github.com/go-vela/sdk-go v0.20.0 - github.com/go-vela/server v0.20.0 + github.com/go-vela/server v0.20.1-0.20230725195029-acbb8f116c4e github.com/go-vela/types v0.20.0 github.com/go-vela/worker v0.20.0 github.com/golang-jwt/jwt/v5 v5.0.0 diff --git a/go.sum b/go.sum index 42a28ea8..6eaca274 100644 --- a/go.sum +++ b/go.sum @@ -158,8 +158,8 @@ github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QX github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I= github.com/go-vela/sdk-go v0.20.0 h1:+kM2sF/DKibzuHK/4XO0QeRkLgjorpuDkr//P95FhqQ= github.com/go-vela/sdk-go v0.20.0/go.mod h1:0cgibC9qs9/JheSsU1qHuAecY1njJDwPSE4XeD4gP7c= -github.com/go-vela/server v0.20.0 h1:niU7Hg9/uJRaRuA2VR+Obq2sAbG8iw32esPgTBawZ/g= -github.com/go-vela/server v0.20.0/go.mod h1:l6ToQGKLn9RP9Kcqwyq3JtgkFRhq1nlIKHq/mynkA+E= +github.com/go-vela/server v0.20.1-0.20230725195029-acbb8f116c4e h1:KldHo5HQICJv5G9nTcNeuuiW9DDUzAIjptRH2X+9cFU= +github.com/go-vela/server v0.20.1-0.20230725195029-acbb8f116c4e/go.mod h1:l6ToQGKLn9RP9Kcqwyq3JtgkFRhq1nlIKHq/mynkA+E= github.com/go-vela/types v0.20.0 h1:u/wHwc6ElVbIEI+q9TaVl9Iai1EoEr4Lwis6mikOte8= github.com/go-vela/types v0.20.0/go.mod h1:1ZSmKWX9MamKogwaIb53mzzRpZMV34mJFKiGfVFadFk= github.com/go-vela/worker v0.20.0 h1:hZuHTf6CGyKEJh5z9vEY2qFd9yRUxvLzMT2x0xUXG8o=