Using the Jenkins API with Python (jenkinsapi)
This guide explains how to configure the Python environment and use the jenkinsapi library to interact with Jenkins, detailing class constructors, method signatures, parameters, and common operations such as retrieving job information, managing builds, and controlling jobs.
Environment Configuration
Required Python packages: jenkinsapi, python-jenkins, pbr, multi-key-dict.
API References
Documentation links: https://blog.csdn.net/seeeees and https://python-jenkins.readthedocs.io/en/latest/api.html .
Jenkins class
class jenkins.Jenkins(server_url, username, password, timeout=None)Parameters:
Parameter
Description
server_url
Jenkins server address
username
Jenkins user name
password
Jenkins password
timeout
Optional timeout value
Method: get_job_info get_job_info(name, depth=0, fetch_all_builds=False) Retrieves information about a specific job.
Parameter
Description
name
Job name
depth
Depth of information (optional)
fetch_all_builds
Whether to fetch all builds (optional)
Method: get_job_name get_job_name(name) Checks whether a job exists; returns the job name if it exists, otherwise None.
Method: debug_job_info debug_job_info() Utility to test and display job information.
Method: get_build_info get_build_info(name, number, depth=0) Retrieves build information for a given job and build number.
Parameter
Description
name
Job name
number
Build number (e.g., get_job_info(job_name)['lastBuild']['number'])
depth
Depth of information (optional)
Method: get_whoami get_whoami() Returns the current authenticated user.
Method: get_version get_version() Returns the Jenkins server version.
Method: get_jobs get_jobs() Returns a list of all jobs on the server.
Job Management Methods copy_job(fromname, toname) – Copies an existing job. rename_job(fromname, toname) – Renames a job. delete_job(name) – Deletes a job. enable_job(name) – Enables (starts) a job. disable_job(name) – Disables (stops) a job. jobs_count() – Returns the total number of jobs. jobs_exists(name) – Checks existence; returns True if exists, otherwise None. assert_job_exist(name) – Asserts that a job exists; raises an exception if not.
Method: create_job create_job(name, config_xml) Creates a new job with the given name and configuration XML.
Parameter
Description
name
Job name
config_xml
XML configuration (use jenkins.EMPTY_CONFIG_XML for an empty config)
Method: get_job_config get_job_config(name) Retrieves the XML configuration of the specified job.
Method: reconfig_job reconfig_job(name) Reconfigures the job with a new XML configuration.
Method: build_job_url build_job_url(name, parameters=None, token=None) Constructs the URL used to trigger a build for the specified job.
Parameter
Description
name
Job name
parameters
Build parameters (optional)
token
User token for authentication (optional)
Method: build_job build_job(name, parameters=None, token=None) Triggers a build of the specified job with optional parameters and token.
Example usage to get build information: server.get_build_info(job_name, last_build_number) Typical keys returned in the build information dictionary include:
['mavenVersionUsed', 'number', 'actions', 'result', 'duration', 'id', 'builtOn', 'artifacts', 'mavenArtifacts', 'keepLog', '_class', 'description', 'changeSet', 'timestamp', 'culprits', 'estimatedDuration', 'building', 'queueId', 'displayName', 'url', 'executor', 'fullDisplayName'].
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
