Initializing Jenkins 2.0 with pipeline in init.groovy.d script

martin-praqma picture martin-praqma · May 11, 2016 · Viewed 7k times · Source

For automation, I would like to initialize a Jenkins 2.0 instance with a pipeline job. I want to create a Groovy script that is copied to the /usr/share/jenkins/ref/init.groovy.d/ folder on startup. The script should create a Jenkins 2.0 Pipeline job for processing a Jenkinsfile from SCM.

I cannot find the relevant Javadoc for the 2.0 pipeline classes or examples of how to do this.

Previously, using Job DSL to create a pipeline, I used a Groovy script to create a FreeStyleProject with an ExecuteDslScripts builder. That job would then be the Job DSL seed job.

One option is to use an init script to create a Job DSL seed job to create a Jenkins 2.0 pipeline. It just seems unnecessarily complex.

I am experimenting in this repo: https://github.com/martinmosegaard/vigilant-sniffle

Answer

daspilker picture daspilker · May 17, 2016

With Job DSL 1.47 (to be released soon) you can use the Job DSL API directly from the init script without the need to create a seed job.

import javaposse.jobdsl.dsl.DslScriptLoader
import javaposse.jobdsl.plugin.JenkinsJobManagement

def jobDslScript = new File('jobs.groovy')
def workspace = new File('.')

def jobManagement = new JenkinsJobManagement(System.out, [:], workspace)

new DslScriptLoader(jobManagement).runScript(jobDslScript.text)

See PR #837 for details.