From 4097e4dfde12e17bb543eb2851379617f48d912c Mon Sep 17 00:00:00 2001 From: CrabHo Date: Tue, 17 Nov 2020 15:42:42 +0800 Subject: [PATCH 1/2] feat: support matrix --- cli/drone/exec/exec.go | 43 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/cli/drone/exec/exec.go b/cli/drone/exec/exec.go index d791faa94..cdca1b0fd 100644 --- a/cli/drone/exec/exec.go +++ b/cli/drone/exec/exec.go @@ -2,7 +2,9 @@ package exec import ( "context" + "fmt" "io" + "io/ioutil" "log" "os" "path" @@ -19,6 +21,7 @@ import ( "github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/frontend/yaml" "github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/frontend/yaml/compiler" "github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/frontend/yaml/linter" + "github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/frontend/yaml/matrix" "github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/interrupt" "github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/multipart" @@ -287,6 +290,34 @@ func exec(c *cli.Context) error { file = ".drone.yml" } + dat, err := ioutil.ReadFile(file) + if err != nil { + return err + } + + axes, err := matrix.ParseString(string(dat)) + if err != nil { + return fmt.Errorf("Parse matrix fail") + } + + if len(axes) == 0 { + axes = append(axes, matrix.Axis{}) + } + for _, axis := range axes { + err := execWithAxis(c, axis) + if err != nil { + return err + } + } + return nil +} + +func execWithAxis(c *cli.Context, axis matrix.Axis) error { + file := c.Args().First() + if file == "" { + file = ".drone.yml" + } + metadata := metadataFromContext(c) environ := metadata.Environ() secrets := []compiler.Secret{} @@ -301,6 +332,16 @@ func exec(c *cli.Context) error { }) } + if len(axis) != 0 { + fmt.Println("===============================================") + fmt.Println("Execute with matrix:") + for k, v := range axis { + fmt.Printf(" %s: %s\n", k, v) + environ[k] = v + } + fmt.Println("===============================================") + } + drone_env := make(map[string]string) for _, env := range c.StringSlice("env") { envs := strings.SplitN(env, "=", 2) @@ -311,6 +352,7 @@ func exec(c *cli.Context) error { if err != nil { return err } + confstr, err := tmpl.Execute(func(name string) string { return environ[name] }) @@ -379,7 +421,6 @@ func exec(c *cli.Context) error { compiler.WithSecret(secrets...), compiler.WithEnviron(drone_env), ).Compile(conf) - engine, err := docker.NewEnv() if err != nil { return err From e491af6f9a668a6e0759a59273131baf2c5cb056 Mon Sep 17 00:00:00 2001 From: CrabHo Date: Wed, 18 Nov 2020 14:45:57 +0800 Subject: [PATCH 2/2] feat: remove debug log --- cli/drone/exec/exec.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/cli/drone/exec/exec.go b/cli/drone/exec/exec.go index cdca1b0fd..fa9856061 100644 --- a/cli/drone/exec/exec.go +++ b/cli/drone/exec/exec.go @@ -333,26 +333,21 @@ func execWithAxis(c *cli.Context, axis matrix.Axis) error { } if len(axis) != 0 { - fmt.Println("===============================================") - fmt.Println("Execute with matrix:") for k, v := range axis { - fmt.Printf(" %s: %s\n", k, v) environ[k] = v } - fmt.Println("===============================================") } - drone_env := make(map[string]string) + droneEnv := make(map[string]string) for _, env := range c.StringSlice("env") { envs := strings.SplitN(env, "=", 2) - drone_env[envs[0]] = envs[1] + droneEnv[envs[0]] = envs[1] } tmpl, err := envsubst.ParseFile(file) if err != nil { return err } - confstr, err := tmpl.Execute(func(name string) string { return environ[name] }) @@ -419,7 +414,7 @@ func execWithAxis(c *cli.Context, axis matrix.Axis) error { ), compiler.WithMetadata(metadata), compiler.WithSecret(secrets...), - compiler.WithEnviron(drone_env), + compiler.WithEnviron(droneEnv), ).Compile(conf) engine, err := docker.NewEnv() if err != nil {