Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
ft(pipeline): Add variables flag for pipeline run
Browse files Browse the repository at this point in the history
This adds flag for running pipelines with custom variables passed to it.
They all default to `env_var`.
  • Loading branch information
zemzale committed Dec 4, 2020
1 parent f4b9338 commit d64c12f
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions commands/pipeline/run/pipeline_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package run

import (
"fmt"
"regexp"
"strings"

"github.com/profclems/glab/commands/cmdutils"
"github.com/profclems/glab/internal/git"
Expand All @@ -12,6 +14,10 @@ import (
"github.com/xanzy/go-gitlab"
)

const keyValuePair = ".+:.+"

var re = regexp.MustCompile(keyValuePair)

func getDefaultBranch(f *cmdutils.Factory) string {
repo, err := f.BaseRepo()
if err != nil {
Expand Down Expand Up @@ -57,17 +63,30 @@ func NewCmdRun(f *cmdutils.Factory) *cobra.Command {
return err
}

// TODO: support setting pipeline variables via cli.
v := []*gitlab.PipelineVariable{
pipelineVars := []*gitlab.PipelineVariable{
{
Key: "GLAB_CLI_KEY",
Value: "GLAB_CLI_VAL",
VariableType: "env_var",
},
}

if customPipelineVars, _ := cmd.Flags().GetStringSlice("variables"); len(customPipelineVars) > 0 {
for _, v := range customPipelineVars {
if !re.MatchString(v) {
return fmt.Errorf("Bad pipeline variable : \"%s\" should be of format KEY:VALUE", v)
}
s := strings.Split(v, ":")
pipelineVars = append(pipelineVars, &gitlab.PipelineVariable{
Key: s[0],
Value: s[1],
VariableType: "env_var",
})
}
}

c := &gitlab.CreatePipelineOptions{
Variables: v,
Variables: pipelineVars,
}

if m, _ := cmd.Flags().GetString("branch"); m != "" {
Expand All @@ -86,6 +105,7 @@ func NewCmdRun(f *cmdutils.Factory) *cobra.Command {
},
}
pipelineRunCmd.Flags().StringP("branch", "b", "", "Create pipeline on branch/ref <string>")
pipelineRunCmd.Flags().StringSliceP("variables", "", []string{}, "Pass variables to pipeline")

return pipelineRunCmd
}

0 comments on commit d64c12f

Please sign in to comment.