Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 831
martin-praqma Avatar asked May 11 '16 08:05

martin-praqma


People also ask

How do I load Groovy script in Jenkins pipeline?

In Jenkinsfile, simply use load step to load the Groovy script. After the Groovy script is loaded, the functions insides can be used where it can be referenced, as shown above.

What is init Groovy D?

The init. groovy runs automatically after initialization and configures all system, global, tools and plugins values, as well as credentials values, also creating/configuring nodes.

How do you write a pipeline script in Jenkins?

To create a simple pipeline from the Jenkins interface, perform the following steps: Click New Item on your Jenkins home page, enter a name for your (pipeline) job, select Pipeline, and click OK. In the Script text area of the configuration screen, enter your pipeline syntax.

Is Jenkins pipeline Groovy?

Within a Pipeline Project (read plugin), Jenkins introduces a domain-specific language (DSL) based on 'Groovy', which can be used to define a new pipeline as a script. The flow that would typically require many “standard” Jenkins jobs chained together, can be expressed as a single script.


1 Answers

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.

like image 71
daspilker Avatar answered Sep 21 '22 00:09

daspilker