diff --git a/kubectl-plugin/pipeline/create.go b/kubectl-plugin/pipeline/create.go index ebe873e..8fcba8f 100644 --- a/kubectl-plugin/pipeline/create.go +++ b/kubectl-plugin/pipeline/create.go @@ -72,7 +72,7 @@ KubeSphere supports multiple types Pipeline. Currently, this CLI only support th flags.BoolVarP(&opt.Batch, "batch", "b", false, "Create pipeline as batch mode") flags.BoolVarP(&opt.SkipCheck, "skip-check", "", false, "Skip the resources check") - _ = cmd.RegisterFlagCompletionFunc("template", common.ArrayCompletion("java", "go", "simple", "longRun", + _ = cmd.RegisterFlagCompletionFunc("template", common.ArrayCompletion("java", "go", "simple", "parameter", "longRun", "multi-branch-gitlab", "multi-branch-github", "multi-branch-git")) _ = cmd.RegisterFlagCompletionFunc("type", common.ArrayCompletion("pipeline", "multi-branch-pipeline")) _ = cmd.RegisterFlagCompletionFunc("scm-type", common.ArrayCompletion("gitlab", "github", "git")) @@ -120,7 +120,7 @@ func (o *pipelineCreateOption) wizard(_ *cobra.Command, _ []string) (err error) } if o.Template == "" { - if o.Template, err = chooseOneFromArray([]string{"java", "go", "simple", "longRun", + if o.Template, err = chooseOneFromArray([]string{"java", "go", "simple", "parameter", "longRun", "multi-branch-gitlab", "multi-branch-github", "multi-branch-git"}); err != nil { return } @@ -174,6 +174,8 @@ func (o *pipelineCreateOption) preRunE(cmd *cobra.Command, args []string) (err e o.Jenkinsfile = tpl.GetBuildGo() case "simple": o.Jenkinsfile = tpl.GetSimple() + case "parameter": + o.Jenkinsfile = tpl.GetParameter() case "longRun": o.Jenkinsfile = tpl.GetLongRunPipeline() case "multi-branch-git": diff --git a/kubectl-plugin/pipeline/run.go b/kubectl-plugin/pipeline/run.go index 02055c2..2735521 100644 --- a/kubectl-plugin/pipeline/run.go +++ b/kubectl-plugin/pipeline/run.go @@ -30,6 +30,8 @@ func newPipelineRunCmd(client dynamic.Interface) (cmd *cobra.Command) { "The Pipeline name that you want to run") flags.StringVarP(&opt.namespace, "namespace", "n", "", "The namespace of target Pipeline") + flags.StringVarP(&opt.project, "project", "", "", + "The project of target Pipeline") flags.BoolVarP(&opt.batch, "batch", "b", false, "Run pipeline as batch mode") flags.StringToStringVarP(&opt.parameters, "parameters", "P", map[string]string{}, "The parameters that you want to pass, example of single parameter: name=value") return @@ -38,6 +40,7 @@ func newPipelineRunCmd(client dynamic.Interface) (cmd *cobra.Command) { type pipelineRunOpt struct { pipeline string namespace string + project string batch bool parameters map[string]string @@ -51,6 +54,16 @@ func (o *pipelineRunOpt) preRunE(cmd *cobra.Command, args []string) (err error) o.pipeline = args[0] } + if o.project != "" { + var devopsProject *unstructured.Unstructured + if devopsProject, err = o.getDevOpsProjectByGenerateName(o.project); err == nil && devopsProject != nil { + o.namespace = devopsProject.GetName() + } else { + err = fmt.Errorf("unable to find namespace by devops project: %s, error: %v", o.project, err) + return + } + } + if err = o.wizard(cmd, args); err != nil { return } @@ -122,6 +135,22 @@ func (o *pipelineRunOpt) wizard(_ *cobra.Command, _ []string) (err error) { return } +func (o *pipelineRunOpt) getDevOpsProjectByGenerateName(name string) (result *unstructured.Unstructured, err error) { + ctx := context.TODO() + var projectList *unstructured.UnstructuredList + if projectList, err = o.Client.Resource(types.GetDevOpsProjectSchema()).List(ctx, metav1.ListOptions{}); err == nil { + for i := range projectList.Items { + item := projectList.Items[i] + + if item.GetGenerateName() == name { + result = &item + return + } + } + } + return +} + func (o *pipelineRunOpt) getPipelineNameList() (names []string, err error) { names, err = o.getUnstructuredNameListInNamespace(o.namespace, true, []string{}, types.GetPipelineSchema()) return diff --git a/kubectl-plugin/pipeline/tpl/parameter.groovy b/kubectl-plugin/pipeline/tpl/parameter.groovy new file mode 100644 index 0000000..9c26c7d --- /dev/null +++ b/kubectl-plugin/pipeline/tpl/parameter.groovy @@ -0,0 +1,65 @@ +pipeline { + agent any + + parameters { + string defaultValue: 'rick', description: 'just for testing', name: 'name', trim: true + booleanParam defaultValue: false, description: 'You can use this flag to debug your Pipeline', name: 'debug' + choice choices: ['v1.18.8', 'v1.18.9'], description: 'Please choose the target Kubernetes version', name: 'kubernetesVersion' + } + + environment { + APP_NAME = "this is a sample app" + } + + stages{ + stage('simple'){ + steps{ + echo "My name is ${params.name}." + echo "Target Kubernetes version is " + params.kubernetesVersion + echo "env " + env.APP_NAME + + script { + if ("${params.debug}" == "true") { + echo "You can put some debug info at here." + echo "Another way to use param: " + params.name + } + } + } + } + + stage('debug stage') { + when { + equals expected: true, actual: params.debug + } + steps { + echo "It's joke, there're debug info here." + + script { + result = input message: 'Please input your name!', ok: 'Confirm', + parameters: [string(defaultValue: 'rick', + description: 'This should not be your real name.', name: 'name', trim: true)] + echo result + } + } + } + + stage('parallel'){ + parallel { + stage('channel-1'){ + steps{ + input message: 'Please input your age!', ok: 'Confirm', + parameters: [string(defaultValue: '18', + description: 'Just a joke.', name: 'age', trim: true)] + } + } + stage('channel-2'){ + steps{ + input message: 'Please input your weight!', ok: 'Confirm', + parameters: [string(defaultValue: '50', + description: 'Just a joke.', name: 'weight', trim: true)] + } + } + } + } + } +} diff --git a/kubectl-plugin/pipeline/tpl/resource.go b/kubectl-plugin/pipeline/tpl/resource.go index ce3351f..47344ac 100644 --- a/kubectl-plugin/pipeline/tpl/resource.go +++ b/kubectl-plugin/pipeline/tpl/resource.go @@ -14,6 +14,8 @@ var ( buildGo string //go:embed simple.groovy simple string + //go:embed parameter.groovy + parameter string ) // GetLongRunPipeline returns the content of long run Pipeline @@ -35,3 +37,8 @@ func GetBuildGo() string { func GetSimple() string { return simple } + +// GetParameter return the content of a parameter Jenkinsfile template +func GetParameter() string { + return parameter +} diff --git a/kubectl-plugin/pipeline/tpl/resource_test.go b/kubectl-plugin/pipeline/tpl/resource_test.go index 4698b09..fbb9d88 100644 --- a/kubectl-plugin/pipeline/tpl/resource_test.go +++ b/kubectl-plugin/pipeline/tpl/resource_test.go @@ -11,4 +11,5 @@ func TestEmebdContent(t *testing.T) { assert.NotEmpty(t, tpl.GetBuildJava(), "java building Pipeline content is empty") assert.NotEmpty(t, tpl.GetBuildGo(), "go building Pipeline content is empty") assert.NotEmpty(t, tpl.GetSimple(), "simple Pipeline content is empty") + assert.NotEmpty(t, tpl.GetParameter(), "parameter Pipeline content is empty") } diff --git a/kubectl-plugin/pipeline/tpl/simple.groovy b/kubectl-plugin/pipeline/tpl/simple.groovy index 9c26c7d..7cc61b8 100644 --- a/kubectl-plugin/pipeline/tpl/simple.groovy +++ b/kubectl-plugin/pipeline/tpl/simple.groovy @@ -1,12 +1,6 @@ pipeline { agent any - parameters { - string defaultValue: 'rick', description: 'just for testing', name: 'name', trim: true - booleanParam defaultValue: false, description: 'You can use this flag to debug your Pipeline', name: 'debug' - choice choices: ['v1.18.8', 'v1.18.9'], description: 'Please choose the target Kubernetes version', name: 'kubernetesVersion' - } - environment { APP_NAME = "this is a sample app" } @@ -14,16 +8,7 @@ pipeline { stages{ stage('simple'){ steps{ - echo "My name is ${params.name}." - echo "Target Kubernetes version is " + params.kubernetesVersion echo "env " + env.APP_NAME - - script { - if ("${params.debug}" == "true") { - echo "You can put some debug info at here." - echo "Another way to use param: " + params.name - } - } } } @@ -32,14 +17,7 @@ pipeline { equals expected: true, actual: params.debug } steps { - echo "It's joke, there're debug info here." - - script { - result = input message: 'Please input your name!', ok: 'Confirm', - parameters: [string(defaultValue: 'rick', - description: 'This should not be your real name.', name: 'name', trim: true)] - echo result - } + echo "It's joke, there is debug info here." } } @@ -47,16 +25,12 @@ pipeline { parallel { stage('channel-1'){ steps{ - input message: 'Please input your age!', ok: 'Confirm', - parameters: [string(defaultValue: '18', - description: 'Just a joke.', name: 'age', trim: true)] + echo "channel-1" } } stage('channel-2'){ steps{ - input message: 'Please input your weight!', ok: 'Confirm', - parameters: [string(defaultValue: '50', - description: 'Just a joke.', name: 'weight', trim: true)] + echo "channel-2" } } }