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
To add jobs to your build pipeline, edit the pipeline on the Pipelines page. Select ... to add a job. To add jobs to your release pipeline, edit the pipeline from Pipelines > Releases. View the stage tasks in the stage where you want to add your job.
Job DSL gives you a way to create other jobs according to a Groovy script. So if you want a set of X related jobs (e.g. a pipeline), you'll create one Job DSL job, write the script, run that job, and then you'll have those X jobs, plus the one to create those jobs.
Job DSL was one of the first popular plugins for Jenkins which allows managing configuration as code and many other plugins dealing with this aspect have been created since then, most notably the Jenkins Pipeline and Configuration as Code plugins.
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() } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With