from jenkinsapi.jenkins import Jenkins # Configuration jenkins_url = "http://your-jenkins-server:8080" user = "your_username" token = "your_api_token" job_name = "your_job_name" # Initialize Jenkins Instance server = Jenkins(jenkins_url, username=user, password=token) # Get the job and its latest successful build job = server.get_job(job_name) build = job.get_last_successful_build() # Download all artifacts from this build artifacts = build.get_artifact_dict() for name, artifact in artifacts.items(): print(f"Downloading {name}...") artifact.save_to_dir('/path/to/save/directory') Use code with caution.
: This library handles the complex JSON parsing of build data for you. Method 2: Using requests for Direct API Calls
In Jenkins, artifacts are files (like JARs, logs, or reports) generated during a build that are preserved for later use. Downloading these via Python is typically done using the , either through direct HTTP requests or dedicated wrapper libraries like jenkinsapi . Prerequisites: Authentication python download jenkins artifacts
The jenkinsapi library provides an object-oriented way to interact with Jenkins, making it easy to iterate through builds and download specific files. pip install jenkinsapi Use code with caution. Python Script:
http://[jenkins_url]/job/[job_name]/lastSuccessfulBuild/artifact/[path_to_file] Python Script: Remote Access API - Jenkins from jenkinsapi
To download artifacts programmatically, you must authenticate. Using an is the recommended method over a standard password.
: Go to your user profile in Jenkins → Configure → API Token → Add new token . Downloading these via Python is typically done using
: Provide this token alongside your username in the auth header of your Python script. Method 1: Using the jenkinsapi Library (Recommended)