How to create a Jenkins Seed Job using Job DSL plugin

In this guide we are going to learn how to create a jenkins seed job.

A Jenkins seed job is a special job that is used to create other jobs. A jenkins seed job is a normal Jenkins job that runs the Job DSL script; in turn, the script contains instructions that create additional jobs.

Also check:

Prerequisites

  • Jenkins server installed and accessible in a URL
  • Access to jenkins
  • Ensure jenkins server has git installed

Ensure that git is installed

The dsl groovy scripts that we will execute to create the jobs will be cloned from git. Please ensure that git is installed on your jenkins server. Here is how to install git for the various operating systems.

Rocky Linux/Centos/Alma Linux/RHEL 8

1
sudo dnf -y install git

Ubuntu/Debian based systems

1
sudo apt install -y git

OpenSUSE

1
sudo zypper in git

Setting up the DSL plugin

The jenkins seed job uses the job-dsl plugin. You have to install it before proceeding.

To install the plugin, Head over to Jenkins Plugin install page here <url>/pluginManager/available then search for job-dsl

Create credential

To clone code from github, we will need to have github credentials.

In your github profile settings, go to Developer Settings -> Personal access tokens then generate new token. Save it somewhere.

Now go to Jenkins, Manage Jenkins -> Manage Credentials. Then go to global Credentials and add new Credential. Create a username and password credentials with the ID that you will use (github-token in my case).

Creating the seed job

With the dsl plugin installed, we can now set up our jenkins job.

Go to New Jenkins job -> Enter job name then click on freestyle project. Click Ok

Using XML

This is the job xml:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?xml version='1.0' encoding='UTF-8'?>
<project>
    <actions></actions>
    <description>Initial Seed Job</description>
    <keepDependencies>false</keepDependencies>
    <properties>
        <com.coravy.hudson.plugins.github.GithubProjectProperty>
        <projectUrl>https://github.com/citizix/example.git</projectUrl>
        </com.coravy.hudson.plugins.github.GithubProjectProperty>
    </properties>
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <concurrentBuild>false</concurrentBuild>
    <builders>
        <javaposse.jobdsl.plugin.ExecuteDslScripts>
        <targets>jenkins-dsl/*.groovy</targets>
        <usingScriptText>false</usingScriptText>
        <scriptText></scriptText>
        <ignoreExisting>false</ignoreExisting>
        <removedJobAction>DELETE</removedJobAction>
        <removedViewAction>DELETE</removedViewAction>
        <lookupStrategy>JENKINS_ROOT</lookupStrategy>
        <additionalClasspath></additionalClasspath>
        </javaposse.jobdsl.plugin.ExecuteDslScripts>
    </builders>
    <publishers></publishers>
    <buildWrappers>
        <hudson.plugins.ansicolor.AnsiColorBuildWrapper>
        <colorMapName>xterm</colorMapName>
        </hudson.plugins.ansicolor.AnsiColorBuildWrapper>
        <hudson.plugins.timestamper.TimestamperBuildWrapper></hudson.plugins.timestamper.TimestamperBuildWrapper>
    </buildWrappers>
    <logRotator>
        <daysToKeep>-1</daysToKeep>
        <numToKeep>30</numToKeep>
        <artifactDaysToKeep>-1</artifactDaysToKeep>
        <artifactNumToKeep>30</artifactNumToKeep>
    </logRotator>
    <scm class='hudson.plugins.git.GitSCM'>
        <userRemoteConfigs>
        <hudson.plugins.git.UserRemoteConfig>
            <name>origin</name>
            <url>https://github.com/citizix/example.git</url>
            <credentialsId>github-token</credentialsId>
        </hudson.plugins.git.UserRemoteConfig>
        </userRemoteConfigs>
        <branches>
        <hudson.plugins.git.BranchSpec>
            <name>*/main</name>
        </hudson.plugins.git.BranchSpec>
        </branches>
        <configVersion>2</configVersion>
        <disableSubmodules>false</disableSubmodules>
        <recursiveSubmodules>false</recursiveSubmodules>
        <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
        <authorOrCommitter>false</authorOrCommitter>
        <clean>false</clean>
        <wipeOutWorkspace>false</wipeOutWorkspace>
        <pruneBranches>false</pruneBranches>
        <remotePoll>false</remotePoll>
        <ignoreNotifyCommit>false</ignoreNotifyCommit>
        <gitTool>Default</gitTool>
        <skipTag>true</skipTag>
    </scm>
</project>

To create jenkins job using anible, save the xml as templates/seed-job.xml. Then use this playbook:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
---
- name: Create jenkins creds
  hosts: localhost
  connection: local
  gather_facts: False
  vars:
    jenkins_host: http://jenkins.srv.com
    username: admin
    password: SuperStrPass
    ansible_python_interpreter: /usr/local/bin/python
  tasks:
    - name: Create jenkins jobs
      community.general.jenkins_job:
        user: "{{ username }}"
        password: "{{ password }}"
        name: Seed-Job
        url: "{{ jenkins_host }}"
        config: "{{ lookup('file', 'templates/seed-job.xml') }}"

Run playbook

1
ansible-playbook -i hosts.yaml jenkins.yaml -vv

Conclusion

In this guide we managed to set up a seed job in jenkins that can create other jobs.

Last updated on Oct 14, 2024 11:46 +0300
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy