Jenkins – Job DSL to create “Pipeline” type job

Jenkinsjenkins-job-dsljenkins-pipelinejenkins-pluginsjenkins-workflow

I have installed Pipeline Plugin which used to be called as Workflow Plugin earlier.
https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Plugin

I want to know how can i use Job Dsl to create and configure a job which is of type Pipeline

enter image description here

Best Answer

You should use pipelineJob:

pipelineJob('job-name') {
  definition {
    cps {
      script('logic-here')
      sandbox()
    }
  }
}

You can define the logic by inlining it:

pipelineJob('job-name') {
  definition {
    cps {
      script('''
        pipeline {
            agent any
                stages {
                    stage('Stage 1') {
                        steps {
                            echo 'logic'
                        }
                    }
                    stage('Stage 2') {
                        steps {
                            echo 'logic'
                        }
                    }
                }
            }
        }
      '''.stripIndent())
      sandbox()     
    }
  }
}

or load it from a file located in workspace:

pipelineJob('job-name') {
  definition {
    cps {
      script(readFileFromWorkspace('file-seedjob-in-workspace.jenkinsfile'))
      sandbox()     
    }
  }
}

Example:

Seed-job file structure:

jobs
   \- productJob.groovy
logic
   \- productPipeline.jenkinsfile

then productJob.groovy content:

pipelineJob('product-job') {
  definition {
    cps {
      script(readFileFromWorkspace('logic/productPipeline.jenkinsfile'))
      sandbox()     
    }
  }
}
Related Topic