Creating Jenkins pipelines with Ansible, part 2
The job-dsl and Pipeline plugins
This is a continuation of the previous post. For each project there are only two things we want to do.
- Check out the source code.
- Run the pipeline in the Jenkinsfile at the root of the repository.
This can be accomplished with the job-dsl plugin. It takes a definition of Jenkins jobs we want, and creates or updates them, as necessary. Somewhat confusingly, job-dsl itself needs to be run from a Jenkins job, known as a seed job.
The only reason for this job to exist is so that job-dsl can run, and every other job should be created with job-dsl. Yes, it is a bit mind bending, but if you don't think of it as a job but as a script which you run from Jenkins it gets easier.
In our case, every other job will look the same. They'll all be Pipeline jobs which run a Jenkinsfile after checking out a git repository from the same git host.
{% for repository in jenkins_git_repositories %}
pipelineJob('{{ repository }}') {
definition {
cpsScm {
scm {
git {
remote {
url('{{ jenkins_git_user }}@{{ jenkins_git_host }}:' +
'{{ jenkins_git_path }}/{{ repository }}.git')
}
branch('master')
}
}
scriptPath('Jenkinsfile')
}
}
}
{% endfor %}