Skip to content

Commit

Permalink
Support to create a parallel pipeline (kubesphere-sigs#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Sep 27, 2021
1 parent 012a354 commit 0b61bfb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
18 changes: 10 additions & 8 deletions kubectl-plugin/pipeline/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func newPipelineCreateCmd(client dynamic.Interface) (cmd *cobra.Command) {
Long: `Create a Pipeline in the KubeSphere cluster
You can create a Pipeline with a java, go template. Before you do that, please make sure the workspace exists.
KubeSphere supports multiple types Pipeline. Currently, this CLI only support the simple one with Jenkinsfile inside.'`,
Example: "ks pip create --ws simple --template java --name java --project test",
Example: "ks pip create --ws simple --project test --template simple --name simple",
PreRunE: opt.preRunE,
RunE: opt.runE,
}
Expand All @@ -72,8 +72,8 @@ 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", "parameter", "longRun",
"multi-branch-gitlab", "multi-branch-github", "multi-branch-git"))
_ = cmd.RegisterFlagCompletionFunc("template",
common.ArrayCompletion(tpl.GetAllTemplates()...))
_ = cmd.RegisterFlagCompletionFunc("type", common.ArrayCompletion("pipeline", "multi-branch-pipeline"))
_ = cmd.RegisterFlagCompletionFunc("scm-type", common.ArrayCompletion("gitlab", "github", "git"))

Expand Down Expand Up @@ -120,14 +120,14 @@ func (o *pipelineCreateOption) wizard(_ *cobra.Command, _ []string) (err error)
}

if o.Template == "" {
if o.Template, err = chooseOneFromArray([]string{"java", "go", "simple", "parameter", "longRun",
"multi-branch-gitlab", "multi-branch-github", "multi-branch-git"}); err != nil {
if o.Template, err = chooseOneFromArray(tpl.GetAllTemplates()); err != nil {
return
}
}

if o.Name == "" {
if o.Name, err = getInput("Please input the Pipeline name"); err != nil {
defaultVal := fmt.Sprintf("%s-%s", o.Template, strings.ToLower(randomdata.SillyName()))
if o.Name, err = getInput("Please input the Pipeline name", defaultVal); err != nil {
return
}
}
Expand All @@ -148,10 +148,10 @@ func chooseOneFromArray(options []string) (result string, err error) {
return
}

func getInput(title string) (result string, err error) {
func getInput(title, defaultVal string) (result string, err error) {
prompt := &survey.Input{
Message: title,
Default: strings.ToLower(randomdata.SillyName()),
Default: defaultVal,
}
err = survey.AskOne(prompt, &result)
return
Expand All @@ -178,6 +178,8 @@ func (o *pipelineCreateOption) preRunE(cmd *cobra.Command, args []string) (err e
o.Jenkinsfile = tpl.GetParameter()
case "longRun":
o.Jenkinsfile = tpl.GetLongRunPipeline()
case "parallel":
o.Jenkinsfile = tpl.GetParallel()
case "multi-branch-git":
o.Type = "multi-branch-pipeline"
o.SCMType = "git"
Expand Down
24 changes: 24 additions & 0 deletions kubectl-plugin/pipeline/tpl/parallel.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pipeline {
agent any
stages {
stage('stage-1') {
parallel {
stage('stage-1-1') {
steps {
echo 'stage-1-1'
}
}
stage('stage-1-2') {
steps {
echo 'stage-1-2'
}
}
}
}
stage('stage-2') {
steps {
echo 'stage-2'
}
}
}
}
13 changes: 13 additions & 0 deletions kubectl-plugin/pipeline/tpl/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ var (
simple string
//go:embed parameter.groovy
parameter string
//go:embed parallel.groovy
parallel string
)

// GetLongRunPipeline returns the content of long run Pipeline
Expand All @@ -42,3 +44,14 @@ func GetSimple() string {
func GetParameter() string {
return parameter
}

// GetParallel returns the content of a paralleled Jenkins pipeline template
func GetParallel() string {
return parallel
}

// GetAllTemplates returns all Pipeline templates
func GetAllTemplates() []string {
return []string{"java", "go", "simple", "parameter", "longRun", "parallel",
"multi-branch-gitlab", "multi-branch-github", "multi-branch-git"}
}
2 changes: 2 additions & 0 deletions kubectl-plugin/pipeline/tpl/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ func TestEmebdContent(t *testing.T) {
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")
assert.NotEmpty(t, tpl.GetParallel(), "parallel Pipeline content is empty")
assert.Equal(t, len(tpl.GetAllTemplates()), 9)
}

0 comments on commit 0b61bfb

Please sign in to comment.