Over the last few years, there has been a push for more “* as code” with Jenkins configuration. You can now specify job config using a Jenkinsfile, allowing auditing and code reviews, as well as a backup.
Combined with the Job DSL plugin, this makes it possible to create a seed job (using another Jenkinsfile, naturally) that creates all the jobs for a specific project.
pipeline {
agent any
options {
timestamps ()
}
stages {
stage('Clean') {
steps {
deleteDir()
}
}
stage('Checkout') {
steps {
checkout scm
}
}
stage('Job DSL') {
steps {
jobDsl targets: ['jobs/*.groovy', 'views/*.groovy'].join('\n')
}
}
}
}
This will run all the groovy scripts in the jobs
& views
folders in this repo (once you’ve approved them).
For example:
pipelineJob("foo-main") {
definition {
cpsScm{
scm {
git {
remote {
github("examplecorp/foo", "ssh")
}
branch("main")
}
}
scriptPath("Jenkinsfile")
}
}
properties {
githubProjectUrl('https://github.com/examplecorp/foo')
pipelineTriggers {
triggers {
cron {
spec('@daily')
}
githubPush()
}
}
}
}
And a view, to put it in:
listView('foo') {
description('')
jobs {
regex('foo-.*')
}
columns {
status()
weather()
name()
lastSuccess()
lastFailure()
lastDuration()
buildButton()
}
}