Skip to content

Commit

Permalink
support project pipelinerun (#197)
Browse files Browse the repository at this point in the history
* Support trigger a pipelinerun via project name

* Add very simple jenkinsfile template
  • Loading branch information
LinuxSuRen committed Sep 8, 2021
1 parent c8bf19c commit 294fd2f
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 31 deletions.
6 changes: 4 additions & 2 deletions kubectl-plugin/pipeline/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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":
Expand Down
29 changes: 29 additions & 0 deletions kubectl-plugin/pipeline/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
65 changes: 65 additions & 0 deletions kubectl-plugin/pipeline/tpl/parameter.groovy
Original file line number Diff line number Diff line change
@@ -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)]
}
}
}
}
}
}
7 changes: 7 additions & 0 deletions kubectl-plugin/pipeline/tpl/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
1 change: 1 addition & 0 deletions kubectl-plugin/pipeline/tpl/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
32 changes: 3 additions & 29 deletions kubectl-plugin/pipeline/tpl/simple.groovy
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
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
}
}
}
}

Expand All @@ -32,31 +17,20 @@ 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."
}
}

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)]
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"
}
}
}
Expand Down

0 comments on commit 294fd2f

Please sign in to comment.