CICD

How to create a Jenkins Seed Job using Job DSL plugin

Pinterest LinkedIn Tumblr

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

Table of content

  1. Ensure that git is installed
  2. Setting up the DSL plugin
  3. Creating the seed job

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

sudo dnf -y install git

Ubuntu/Debian based systems

sudo apt install -y git

OpenSUSE

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

Citizix – Jenkins 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. Basica

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

Citizix – Seed Job new

This is the seed job config:

Citizix create seed job

Using XML

This is the job xml:

<?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:

---
- 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

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.

I am a Devops Engineer, but I would describe myself as a Tech Enthusiast who is a fan of Open Source, Linux, Automations, Cloud and Virtualization. I love learning and exploring new things so I blog in my free time about Devops related stuff, Linux, Automations and Open Source software. I can also code in Python and Golang.

Write A Comment