How to re-write a Jenkins DSL file as a Jenkins pipeline jenkinsfile? How to re-write a Jenkins DSL file as a Jenkins pipeline jenkinsfile? jenkins jenkins

How to re-write a Jenkins DSL file as a Jenkins pipeline jenkinsfile?


First, have a good look at Jenkins pipeline documentation, it is a great start and it is providing a whole bunch of information such as Build Parameters usage or parallel steps.

Here are a few more hints for you to explore :

Parameters

Just use the parameter name as a variable such as :

if (BUILD_SNAPSHOT) {    ...}

Call other jobs

You can also use build step such as :

build job: '1-company-worker-build-snaphsot', parameters: [stringParam(name: 'WORKER_NAME', value: "sharding-worker")]

Use functions

Instead of calling downstream jobs using build steps each time, you might want to consider using pipeline functions from another Groovy script, either from your current project or even from an external, checked out Groovy script.

As an example, you could replace your second job call from :

build("1-company-worker-build-snaphsot", WORKER_NAME: "sharding-worker")

to :

git 'http://urlToYourGit/projectContainingYourScript'pipeline = load 'global-functions.groovy'pipeline.buildSnapshot("sharding-worker")

...of course the init phase (Git checkout and pipeline loading) is only needed once before you can call all your external scripts functions.

In short

To sum it up a little bit, your code could be converted to something along these lines :

node {    git 'http://urlToYourGit/projectContainingYourScript'    pipeline = load 'global-functions.groovy'    if(BUILD_SNAPSHOT) {        parallel (            phase1: { pipeline.buildMainSnapshot() },            phase2: { pipeline.buildWorkerSnapshot("sharding-worker") }        )    }    parallel (        phase1: { pipeline.phase1(params...) },        phase2: { pipeline.phase2(params...) },        phase3: { pipeline.phase3(params...) },        phase4: { pipeline.phase4(params...) }    )    if (REST_TEST) {        pipeline.finalStep()    }}