Operations 5 min read

Automating Jenkins Job Creation with Hubot, Shell Scripts, and Python

This guide explains how to automate the creation of Jenkins jobs using Hubot plugins, shell scripts, and a Python‑jenkins library, detailing required parameters, installation steps, example scripts, and a help command for users.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Automating Jenkins Job Creation with Hubot, Shell Scripts, and Python

The author spends an hour each day manually creating Jenkins jobs for business needs, requiring only three parameters (Jenkins Job Name, Gitlab URL, Jenkinsfile Name) because the Gitlab webhook can be derived from the job name.

To enable Hubot to run shell commands, the hubot-script-shellcmd plugin is installed with the following commands:

cd myhubot
npm install hubot-script-shellcmd
cp -R node_modules/hubot-script-shellcmd/bash .
# Add hubot-script-shellcmd to external-scripts.json
# Start hubot
./bin/hobut

A simple Hubot shell script (without error handling) is provided to invoke a Python script that creates the Jenkins job and then echo a confirmation message:

#!/bin/bash
# Declare interpreter
python3 handlers/createjenkins.py $1 $2 $3
echo "Jenkins Job 已经创建完毕请访问https://jenkins.xxx.com 进行访问"
exit 0
# Return value

Because using raw curl commands in shell is cumbersome, the author prefers the python-jenkins library to create jobs. After installing the dependency with pip3 install python-jenkins , the following Python script defines a JenkinsMethod class to connect to Jenkins, generate job configuration XML, and create the job:

pip3 install python-jenkins
import jenkins
import sys

class JenkinsMethod(object):
    '''
    jenkins_server_url      Jenkins URL
    jenkins_username        Jenkins username
    jenkins_password        Jenkins password
    pipeline_name           Jenkins Job name
    git_url                 Git repository URL
    jenkinsfile_path        Jenkinsfile name
    '''
    def __init__(self, jenkins_server_url, jenkins_username, jenkins_password):
        self.server = jenkins.Jenkins(jenkins_server_url, username=jenkins_username, password=jenkins_password)

    def jenkins_create_job(self, git_url, jenkinsfile_path, pipeline_name, multi_pipeline=None):
        if multi_pipeline is None:
            conf_mxl = self.server.get_job_config("pipeline-template")
            conf_mxl = conf_mxl.replace("GITURL", git_url)
            conf_mxl = conf_mxl.replace("Jenkinsfile", jenkinsfile_path)
            self.server.create_job(pipeline_name, conf_mxl)
        else:
            # Multi‑branch build handling (not implemented)
            pass

if __name__ == '__main__':
    jenkins_server_url = "http://jenkins.xxx.com:8080"
    jenkins_username = "xxx"
    jenkins_password = "xxx"
    argv = sys.argv
    pipeline_name = argv[1]
    git_url = argv[2]
    jenkins_file_path = argv[3]
    client = JenkinsMethod(jenkins_server_url, jenkins_username, jenkins_password)
    client.jenkins_create_job(pipeline_name, git_url, jenkins_file_path)

To provide users with guidance, a Hubot help command is added:

robot.hear /jenkinshelp/i, (res) ->
    res.send "想要创建Jenkins请输入命令请输入:\t\n \"run createjenkins argv1 argv2 argv3\"\nargv1:需要创建Job的名字\nargv2:git的url\nargv3:Jenkinsfile的名字"

After setting up the help option, users can trigger the robot to create Jenkins jobs, with future improvements planned for error handling (e.g., duplicate job names) and automatic addition of Gitlab webhooks.

The article concludes with a promotional block recommending a DevOps pipeline course, including images and QR‑code instructions for purchase.

PythonCI/CDautomationDevOpsJenkinsHubot
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.