Job DSL to create "Pipeline" type job

35,784

Solution 1

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()     
    }
  }
}

Solution 2

I believe this question is asking something how to use the Job DSL to create a pipeline job which references the Jenkinsfile for the project, and doesn't combine the job creation with the detailed step definitions as has been given in the answers to date. This makes sense: the Jenkins job creation and metadata configuration (description, triggers, etc) could belong to Jenkins admins, but the dev team should have control over what the job actually does.

@meallhour, is the below what you're after? (works as at Job DSL 1.64)

pipelineJob('DSL_Pipeline') {

  def repo = 'https://github.com/path/to/your/repo.git'

  triggers {
    scm('H/5 * * * *')
  }
  description("Pipeline for $repo")

  definition {
    cpsScm {
      scm {
        git {
          remote { url(repo) }
          branches('master', '**/feature*')
          scriptPath('misc/Jenkinsfile.v2')
          extensions { }  // required as otherwise it may try to tag the repo, which you may not want
        }

        // the single line below also works, but it
        // only covers the 'master' branch and may not give you
        // enough control.
        // git(repo, 'master', { node -> node / 'extensions' << '' } )
      }
    }
  }
}

Ref the Job DSL pipelineJob: https://jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob, and hack away at it on http://job-dsl.herokuapp.com/ to see the generated config.


This example worked for me. Here's another example based on what worked for me:

pipelineJob('Your App Pipeline') { 

  def repo = 'https://github.com/user/yourApp.git' 
  def sshRepo = '[email protected]:user/yourApp.git' 

  description("Your App Pipeline") 
  keepDependencies(false) 

  properties{ 

    githubProjectUrl (repo) 
    rebuild { 
      autoRebuild(false) 
    } 
  } 

  definition { 

    cpsScm { 
      scm { 
        git { 
          remote { url(sshRepo) } 
          branches('master') 
          scriptPath('Jenkinsfile') 
          extensions { }  // required as otherwise it may try to tag the repo, which you may not want 
        } 
      } 
    } 
  }

If you build the pipeline first through the UI, you can use the config.xml file and the Jenkins documentation https://jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob to create your pipeline job.

Solution 3

In Job DSL, pipeline is still called workflow, see workflowJob.

The next Job DSL release will contain some enhancements for pipelines, e.g. JENKINS-32678.

Solution 4

First you need to install Job DSL plugin and then create a freestyle project in jenkins and select Process job DSLs from the dropdown in the build section.

Select Use the provided DSL script and provide following script.

pipelineJob('job-name') {
  definition {
    cps {
      script('''
        pipeline {
          agent any
          stages {
            stage('Stage name 1') {
              steps {
                // your logic here
              }
            }
            stage('Stage name 2') {
              steps {
                // your logic here
              }
            }
          }
        }
      }
    ''')   
    }
  }
}

Or you can create your job by pointing the jenkinsfile located in remote git repository.

pipelineJob("job-name") {
  definition {
    cpsScm {
      scm {
        git {
          remote {
            url("<REPO_URL>")
            credentials("<CREDENTIAL_ID>")
          }
          branch('<BRANCH>')
        }
      }
      scriptPath("<JENKINS_FILE_PATH>")
    }
  }
}
Share:
35,784
meallhour
Author by

meallhour

Updated on June 27, 2020

Comments

  • meallhour
    meallhour almost 4 years

    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

  • Diasiare
    Diasiare almost 6 years
    -1 Do not run build only stages inside a node declaration. This will allocate an entire executor for the pipeline which will just wait for the triggered builds to finish. At best it's wasteful, at worst it can cause deadlocks. You can just skip the node block and a flyweight executor will be allocated on the fly without consuming a full on executor.
  • agabrys
    agabrys almost 6 years
    @Diasiare Yes, you are right. I updated the answer. Thank you :)
  • jacderida
    jacderida over 5 years
    Thanks! In my opinion this should be the correct answer. It was exactly what I was looking for anyway. I only want to use the Job DSL plugin to define the job itself, then have the actual code for the pipeline in the Jenkinsfile. I don't see what the point is in using the DSL plugin to define a pipeline.
  • Arghya
    Arghya about 4 years
    What if I'm generating the code of the pipeline job but want to check it in to some repo and want the Pipeline job to read its Jenkinsfile from that repo? How can I do the check-in? Is that possible?