- Download and run Jenkins
- Download Jenkins.
- Open up a terminal in the download directory.
- Run
java -jar jenkins.war --httpPort=8080. - Browse to
http://localhost:8080. - Follow the instructions to complete the installation.
- Familiarity with Jenkins Pipeline.
Now that the prerequisites are out of the way, the first thing we are going to do is set up a simple proof of concept Freestyle project (seedJob) in Jenkins. This job will be used to generate other jobs you want to on-board into Jenkins.
graph LR
A[seedJob] -- generates --> B{simple}
Navigate to http://localhost:8080/ in your browser and login to Jenkins with the credentials you set up. We need to configure Jenkins to use the Jenkins Job DSL API.
- Navigate to
Dashboard>Manage Jenkins>Manage Plugins> selectAvailabletab > search forJob DSLand install.
We will set up our seedJob the same way any other job is created.
- On the left hand side of the page, select
New Item - In the text box for
Enter an item name, enterseedJob> select theFreestyle project> selectOK.
Now we are going to store the code for our seedJob in a github repository called hola-jenkins. Since we are using the hola-jenkins repository to load seed.groovy, we need to configure seedJob to use this repository.
- Navigate to
Dashboard> selectseedJob> selectConfigure. - Scroll to the
Source Code Managementsection > selectGit. - In the
Repository URLfield, enterhttps://github.com/hereischen/hola-jenkins.
Now that we have configured Jenkins to use our hola-jenkins repository, we need to set up seedJob to load seed.groovy from our hola-jenkins repository. This is necessary for us to start using the Jenkins Job DSL API functionality.
Since we will be using our hola-jenkins repository, we will need to add some additional configuration to the seedJob to get this working.
- Navigate to
Dashboard> selectseedJob> selectConfigure. - Scroll to the
Buildsection > selectAdd Build step> SelectProcess Job DSLs. - Select
Look on Filesystem. - In the
DSL Scriptsinput field, enterdsl/seed.groovy(this is the path to theseed.groovyfile we will be setting up later).
We will give our job the name when we plan to build (simpleJob). In order to do this we will need to add a String parameter to the seedJob that will be used inside of seed.groovy.
- Navigate to
Dashboard> selectseedJob> selectConfigure. - Select
This project is parameterized> selectAdd Parameter> selectString Parameter. - Enter
jobNameinNamefield. - In the
Descriptionfield, enterThe name of your job(typo in the image).
We are using the hola-jenkins github repository. This repository will be used to store our seed code. Later, we will include our Shared Library code in this repository.
- Inside of the
hola-jenkinsrepository, we have created a directorydslwithseed.groovyinside. - Below are the contents of
seed.groovy.- We create a simple Freestyle Job and use the
String ParamnamedjobNamefromseedJobto name our Freestyle job.
- We create a simple Freestyle Job and use the
job(jobName) {
description("A simple Freestyle Job generated from seed.groovy")
}Now that we have our seedJob setup to read in seed.groovy from our github hola-jenkins repository, we are ready to trigger our seedJob to create a Freestyle job with jobName
- Navigate to
Dashboard> selectseedJob> selectBuild Now. - Under
Build History, select the top red circle. - This will take you to the
Console Output.- The job failed.
- Due to Script Security, this will happen every time you change
seed.groovy. The Script Security Plugin is integrated with theJob DSLplugin and the Script Security Plugin is set up with a set of scripts that are pre approved for use. Since this is a new script, it will require an admin approval to use.
We need to tell Jenkins it is ok to use this script.
- Navigate to
Dashboard>Manage Jenkins>In-process Script Approval. - Select
Approvefor theseed.groovyscript.
Note:
We can apply configuration-as-code-plugin to by pass this behavior by adding a hola-jenkins/jenkins.yaml. in our hola-jenkins repository.
security:
globalJobDslSecurityConfiguration:
# Otherwise Job DSL either requires script approval or having all config whitelisted
# https://github.com/jenkinsci/job-dsl-plugin/wiki/Script-Security
useScriptSecurity: falseConsult JCasC for more information.
Now that we have approved seed.groovy, we are ready for our seedJob to run (and succeed).
- Navigate to
Dashboard> selectseedJob> selectBuild Now. - Under
Build History, select the top blue circle. - Inside of
Console Output, you will seeGeneratedJob{name='freestyle'}.- Jenkins has created a new job called
simplefromseed.groovy.
- Jenkins has created a new job called
In this second part, we will be setting up a Jenkins Shared library to execute our Jenkins jobs. As the complexity and number of jobs you maintain grow, the use of Shared Libraries provides the ability to share and reuse code across jobs for CI and CD processes.
In order to set up a practical application with our seed, we will modify seed.groovy to build a Pipeline job (composeTrigger) from a file named composeTrigger in Shared Libraries.
graph LR
A[seed] -- generates --> B{composeTrigger}
A[seed] -- generates --> C{acceptanceTrigger}
A[seed] -- generates --> D{otherJobN}
Since we will be using a Shared library, Jenkins needs to be set up to use our Shared Library.
- Navigate to
Dashboard> selectManage Jenkins> selectConfigure System> scroll down toGlobal Pipeline Libraries> selectAdd - Enter
hola-jenkinsin theNamefield. - Enter
new-impinDefault Version.- This tells jenkins which branch of our Shared Library we plan to use by default.
- Under
Source Code Management, selectGit. - In the
Project Repositoryfield, enterhttps://github.com/hereischen/hola-jenkins> selectSave.
Create a new job DSL named seed, just like what we did in session 1.2. Under Source Code Management, change the Branch Specifier to */new-imp

- Create a new branch
new-impinhola-jenkins. - In the
new-impbranch, remove the original code inseed.groovyand paste in fromseed.groovy. This script loads jobs from/vars/jobsand builds each job using its file name as the job name.- For a better understanding of the
pipelineJob, make sure to go back and check the Jenkins Job DSL API.
- For a better understanding of the
#!/usr/bin/env groovy
import groovy.io.FileType
String jobDir = "${WORKSPACE}/vars/jobs"
def retrieveJobs (String jobDir) {
def list = []
def dir = new File(jobDir)
dir.eachFileRecurse (FileType.FILES) { file ->
list << file.getName()
}
return list
}
def buildPipelineJobs(String jobName){
String repoUrl = "https://github.com/hereischen/hola-jenkins.git"
println "Seeding job ${jobName}."
pipelineJob(jobName) {
definition {
cpsScm {
scm {
git {
remote {
url(repoUrl)
}
branches('new-imp')
extensions {
cleanBeforeCheckout()
}
}
}
scriptPath("vars/jobs/${jobName}")
}
}
// build jobs to make their properties effective
queue(jobName)
}
}
def jobList = retrieveJobs (jobDir)
jobList.each {
buildPipelineJobs(it)
}- Create a new file named
composeTrigger(This will be the job name) undervars/jobs
#!/usr/bin/env groovy
// Configure using hola-jenkins
@Library("hola-jenkins@new-imp") _
properties([
parameters([
string(name: 'example_param1', defaultValue: 'hello word')
]),
])
node {
stage('Checkout') {
checkout scm
}
stage("Package artifact") {
sh "echo this is it"
println("properties:${params.example_param1}")
}
}
All of our configuration is set up and our repository is ready to use. We will now run our seed to create our composeTrigger based on our seed.groovy set up.




