Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Job DSL to create "Pipeline" type job

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

like image 428
meallhour Avatar asked Mar 09 '16 17:03

meallhour


People also ask

How do I set up a pipeline job?

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.

What is DSL in pipeline?

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.

What is DSL in Jenkins pipeline?

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.


1 Answers

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()          }   } } 
like image 151
agabrys Avatar answered Sep 28 '22 14:09

agabrys