Operations 9 min read

Managing Jenkins with the python-jenkins API

This tutorial demonstrates how to install the python-jenkins library, explore its extensive API methods, and use Python scripts to create, copy, configure, and control Jenkins jobs, nodes, views, plugins, and builds, providing practical examples and code snippets for each operation.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Managing Jenkins with the python-jenkins API

This article introduces the python-jenkins library (available on PyPI) for interacting with Jenkins via its REST API using Python 3.7.

Features

Create, copy, delete, update, enable/disable jobs

Start builds, retrieve build information, manage build numbers

Manage nodes, views, credentials, promotions, and plugins

Control Jenkins server (quiet down, shutdown) and query version, queue, and running builds

Usage Workflow

1. Install the library: pip install python-jenkins==1.6.0 2. Open a Python interpreter and connect to Jenkins:

import jenkins
server = jenkins.Jenkins("http://127.0.0.1:8080", username="admin", password="admin")
print(server.get_whoami())

3. List available methods with dir(server) to explore the API.

Method Reference

Function

Python Method

Job operations

create_job, copy_job, delete_job, disable_job, enable_job, job_exists, upsert_job, ...

Build operations

build_job, delete_build, stop_build, get_build_info, get_build_console_output, ...

Credential operations

create_credential, delete_credential, credential_exists, get_credential_info, ...

View operations

create_view, delete_view, view_exists, get_view_config, ...

Node operations

create_node, delete_node, enable_node, disable_node, get_node_info, ...

Promotion operations

create_promotion, delete_promotion, promotion_exists, ...

Update operations

reconfig_job, reconfig_node, reconfig_view, reconfig_credential, set_next_build_number, ...

Example: Creating a New Job

Retrieve the XML configuration of an existing job and reuse it to create a new one:

config_xml = server.get_job_config("demo-test")
server.create_job("demo-test-02", config_xml)
print(server.job_exists("demo-test-02"))  # True

Example: Copying a Job

server.copy_job("demo-test-02", "demo-test-03")
print(server.job_exists("demo-test-03"))  # True

Example Script: Adding a Parameter to a New Project

The script below checks if a target job exists, copies the configuration from a template, replaces the default VCS type from svn to git, adds a srcType parameter, and creates the new job.

import jenkins

server = jenkins.Jenkins("http://127.0.0.1:8080", "admin", "admin")

defProjectName = "demo-devops-service"
newProjectName = "demo-test-service"

if not server.job_exists(newProjectName):
    config_xml = server.get_job_config(defProjectName)
    newconfig_xml = config_xml.replace("<defaultValue>svn</defaultValue>", "<defaultValue>git</defaultValue>")
    server.create_job(newProjectName, newconfig_xml)
    print("Project created")
else:
    print("Project already exists!")

Running the script produces the modified config.xml with the added srcType parameter and confirms the job creation.

Conclusion

The python-jenkins library provides a comprehensive set of functions to automate Jenkins administration tasks, making it easy to script job creation, configuration changes, and server management directly from Python.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonCI/CDautomationDevOpsAPIJenkins
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

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.