JenkinsAPI is a Python library that provides an object-oriented interface for interacting with a Jenkins continuous integration server. It wraps the Jenkins REST API, making it easier to query the server's state, manage jobs, builds, nodes, and artifacts, and automate various Jenkins tasks. The library is actively maintained, with regular updates addressing bug fixes and new features, and is currently at version 0.3.22.
pip install jenkinsapiVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to connect to a Jenkins server using the `Jenkins` class and retrieve basic information like the server version and the count of configured jobs. It uses environment variables for Jenkins URL, username, and API token for secure credential management.
Navigate to 'Manage Jenkins' > 'Configure Global Security' and uncheck 'Prevent Cross Site Request Forgery exploits'. For more secure setups, consider proxying requests or implementing a custom CSRF handling mechanism.
In Jenkins, go to 'Manage Jenkins' > 'Configure System' and ensure 'Jenkins Location' (Jenkins URL) is set to the correct publicly accessible URL of your Jenkins instance.
Generate an API token for your user in Jenkins (User > Configure > API Token) and use this token as the `password` argument when initializing the `Jenkins` object.
While `jenkinsapi` itself doesn't offer a 'lazy' loading option at the top level for all objects, be mindful of operations that iterate over `server.jobs` or similar collections. Consider fetching specific jobs or using filters if available, or exploring alternative Jenkins Python clients (like `python-jenkins` or `api4jenkins`) if this becomes a persistent performance bottleneck.
Implement robust retry mechanisms and careful state tracking when interacting with the build queue. Be aware of potential race conditions and consider using Jenkins' built-in mechanisms for build notifications or webhooks if available and suitable for your use case, or poll build status with appropriate delays and error handling.
Install the library using pip: `pip install jenkinsapi`
Rename your Python script if it's named `jenkins.py` or `jenkinsapi.py` to something unique (e.g., `my_jenkins_script.py`). Ensure you are importing the `Jenkins` class correctly: `from jenkinsapi.jenkins import Jenkins`.
Verify the username and ensure you are using a Jenkins API token as the password for programmatic access. The API token can be found/generated in your Jenkins user's 'Configure' page (e.g., `http://your-jenkins-server/user/<username>/configure`).
Check the detailed error message accompanying the exception for specific clues. Common solutions include: ensuring the job exists and its name is correct, verifying the user has sufficient permissions for the requested operation, and, if applicable, correctly handling Jenkins's CSRF protection by using `jenkinsapi.utils.crumb_requester.CrumbRequester` when initializing the Jenkins object.
No dependency data recorded yet.