Builds
Builds
Search for a build
- builds.Builds.search()
Fetches a specific build from the build history of the job.
- Parameters:
build_number (int) – The number of the build to fetch.
kws (dict) – Additional keyword arguments to specify which build to fetch.
lastStableBuild (bool, optional) – Fetch the last stable build.
lastSuccessfulBuild (bool, optional) – Fetch the last successful build.
lastFailedBuild (bool, optional) – Fetch the last failed build.
lastUnsuccessfulBuild (bool, optional) – Fetch the last unsuccessful build.
lastCompletedBuild (bool, optional) – Fetch the last completed build.
- Returns:
The Build object representing the requested build.
- Return type:
from jenkins_pysdk.jenkins import Jenkins
j = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(j.jobs.search("folder1").builds.search(1).number)
print(j.jobs.search("folder1").builds.search(lastStableBuild=True).number)
print(j.jobs.search("folder1").builds.search(lastSuccessfulBuild=True).number)
print(j.jobs.search("folder1").builds.search(lastFailedBuild=True).number)
print(j.jobs.search("folder1").builds.search(lastUnsuccessfulBuild=True).number)
print(j.jobs.search("folder1").builds.search(lastCompletedBuild=True).number)
The above code will output:
1
55
57
56
57
57
Get the total build history of a job
- builds.Builds.total()
Get the total number of saved builds for the job.
- Returns:
The total number of saved builds.
- Return type:
- Raises:
JenkinsGeneralException – If a general exception occurs.
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
my_job = jenkins.jobs.search("new_freestyle")
print(my_job.builds.total)
The above code will output:
6
Iterate all job’s builds
- builds.Builds.iter()
Iterate over builds in the build history of the job.
- Yield:
A Build object representing each build in the build history.
- Return type:
Generator[
jenkins_pysdk.builds.Build]- Raises:
JenkinsGeneralException – If a general exception occurs.
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
my_job = jenkins.jobs.search("new_freestyle")
for build in my_job.builds.iter():
print(build.number)
The above code will output:
10
9
8
7
2
1
List all jobs’ builds
- builds.Builds.list()
Get a list of all builds in the build history of the job.
- Returns:
A list of Build objects representing each build in the build history.
- Return type:
- Raises:
JenkinsGeneralException – If a general exception occurs.
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
my_job = jenkins.jobs.search("new_freestyle")
print(my_job.builds.list())
The above code will output:
[<jenkins_pysdk.builds.Build object at 0x00000131E097FD00>, <jenkins_pysdk.builds.Build object at 0x00000131E097FAF0>, <jenkins_pysdk.builds.Build object at 0x00000131E097FE20>, <jenkins_pysdk.builds.Build object at 0x00000131E097FBB0>, <jenkins_pysdk.builds.Build object at 0x00000131E097F850>, <jenkins_pysdk.builds.Build object at 0x00000131E08BC4C0>]
Get the latest saved build
- builds.Builds.latest()
Retrieve the last build in the build history of the job.
- Returns:
The Build object representing the last build.
- Return type:
- Raises:
JenkinsGeneralException – If a general exception occurs.
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
my_job = jenkins.jobs.search("new_freestyle")
print(my_job.builds.latest.url)
The above code will output:
https://JenkinsDNS/job/new_freestyle/10/
Get the oldest saved build
- builds.Builds.oldest()
Retrieve the oldest saved build in the build history of the job.
- Returns:
The Build object representing the oldest saved build.
- Return type:
- Raises:
JenkinsNotFound – If the job has no builds.
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
my_job = jenkins.jobs.search("new_freestyle")
print(my_job.builds.latest.url)
The above code will output:
https://JenkinsDNS/job/new_freestyle/1/
Trigger a new build
- builds.Builds.build()
Trigger a new build for the job with optional parameters.
- Parameters:
- Returns:
Result of the build trigger request.
- Return type:
- Raises:
JenkinsGeneralException – If a general exception occurs.
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.jobs.search("new_freestyle").builds.build({"choises": "A", "a_bool": False}, delay=10))
The above code will output:
request=<Request object at 1874065655344> content='[201] Successfully triggered a new build.' status_code=201
Rebuild the last build
- builds.Builds.rebuild_last()
Trigger a rebuild of the last build of the job.
- Returns:
Result of the rebuild operation.
- Return type:
- Raises:
JenkinsGeneralException – If a general exception occurs.
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.jobs.search("new_freestyle").builds.rebuild_last())
The above code will output:
request=<Request object at 2881680762624> content='[200] Successfully triggered a rebuild of the last build.' status_code=200
Build
Get the build number
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
my_job_build_10 = jenkins.jobs.search("new_freestyle").builds.search(10)
print(my_job_build_10.number)
The above code will output:
10
Get the build URL
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
my_job_build_2 = jenkins.jobs.search("new_freestyle").builds.search(2)
print(my_job_build_2.url)
The above code will output:
https://JenkinsDNS/job/new_freestyle/2/
Get the build result
- builds.Build.result()
Get the result of the build.
- Returns:
The result of the build.
- Return type:
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
my_job_build_2 = jenkins.jobs.search("new_freestyle").builds.search(2)
print(my_job_build_2.result)
The above code will output:
SUCCESS
Get the build timestamp
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.jobs.search("new_freestyle").builds.latest.timestamp)
The above code will output:
1711475427971
Get the build description
- builds.Build.description()
Get the build description.
- Returns:
The build description.
- Return type:
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.jobs.search("new_freestyle").builds.latest.description)
The above code will output:
None
Check if the build is done
- builds.Build.done()
Check if the build has completed.
- Returns:
True if the build has completed, False otherwise.
- Return type:
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
my_job_build_2 = jenkins.jobs.search("new_freestyle").builds.search(2)
print(my_job_build_2.done)
The above code will output:
True
Get the build duration
- builds.Build.duration()
Get the duration of the build.
- Returns:
The duration of the build in milliseconds.
- Return type:
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
my_job_build_2 = jenkins.jobs.search("new_freestyle").builds.search(2)
print(my_job_build_2.duration)
The above code will output:
15
Get the build console logs
- builds.Build.console()
Retrieve the console output of the build.
- Parameters:
kws – Keyword arguments. - progressive (bool, optional): Whether to retrieve progressive console output. - html (bool, optional): Whether to retrieve HTML-formatted console output. - _start (int, optional): Console output bytes offset (Only works with progressive/HTML - use with caution, as you may lose output).
- Returns:
The console output of the build.
- Return type:
- Raises:
JenkinsGeneralException – If a general exception occurs.
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
my_job_build_10 = jenkins.jobs.search("new_freestyle").builds.search(10)
print(my_job_build_10.console())
The above code will output:
Started by user admin
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/new_freestyle
Finished: SUCCESS
Building with html
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
builds = jenkins.jobs.search("folder1").builds
builds.build()
for chunk in builds.latest.console(html=True):
print(chunk)
The above code will output:
Started by user <a href='/user/admin' class='jenkins-table__link model-link model-link--float'>admin</a>
Running as SYSTEM
Building on the built-in node in workspace /var/lib/jenkins/workspace/folder1
The recommended git tool is: NONE
No credentials specified
> git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/folder1/.git # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url <a href='https://github.com/KnownZero/JenkinsPythonSDK.git'>https://github.com/KnownZero/JenkinsPythonSDK.git</a> # timeout=10
Fetching upstream changes from <a href='https://github.com/KnownZero/JenkinsPythonSDK.git'>https://github.com/KnownZero/JenkinsPythonSDK.git</a>
> git --version # timeout=10
> git --version # 'git version 2.31.1'
> git fetch --tags --force --progress -- <a href='https://github.com/KnownZero/JenkinsPythonSDK.git'>https://github.com/KnownZero/JenkinsPythonSDK.git</a> +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse origin/dev^{commit} # timeout=10
Checking out Revision d525e7e6633eac266239438520cf27b37e751794 (origin/dev)
> git config core.sparsecheckout # timeout=10
> git checkout -f d525e7e6633eac266239438520cf27b37e751794 # timeout=10
Commit message: "release 1.3.5"
> git rev-list --no-walk d525e7e6633eac266239438520cf27b37e751794 # timeout=10
[Checks API] No suitable checks publisher found.
[folder1] $ /bin/sh -xe /tmp/jenkins5873370620277880285.sh
+ echo text
text
+ sleep 1
+ echo 'last line'
last line
[Checks API] No suitable checks publisher found.
Finished: SUCCESS
Delete the build
- builds.Build.delete()
Delete the build.
- Returns:
Result of the delete request.
- Return type:
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
my_job_build_10 = jenkins.jobs.search("new_freestyle").builds.search(10)
print(my_job_build_10.delete())
The above code will output:
request=<Request object at 2286772207216> content='[200] Successfully deleted build (10).' status_code=200
Get the build changes
- builds.Build.changes()
Get the changes associated with the build.
- Returns:
The changes associated with the build.
- Return type:
- Raises:
JenkinsGeneralException – If a general exception occurs.
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
my_job_build_2 = jenkins.jobs.search("new_freestyle").builds.search(2)
print(my_job_build_2.changes)
The above code will output:
<HTML output>
Get the next build (Beta - if you delete some builds then ordering will be broken)
- builds.Build.next()
Get the next build in the build queue.
- Returns:
The next build in the build queue.
- Return type:
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
my_job_build_2 = jenkins.jobs.search("new_freestyle").builds.search(2)
print(jenkins.jobs.search("new_freestyle").builds.oldest.next)
The above code will output:
<jenkins_pysdk.builds.Build object at 0x000001FE5F797D30>
Get the previous build (Beta - if you delete some builds then ordering will be broken)
- builds.Build.previous()
Get the previous build in the build history.
- Returns:
The previous build in the build history.
- Return type:
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
my_job_build_2 = jenkins.jobs.search("new_freestyle").builds.search(2)
print(jenkins.jobs.search("new_freestyle").builds.oldest.previous)
The above code will output:
<jenkins_pysdk.builds.Build object at 0x000001FE5F797D30>
Rebuild current build
- builds.Build.rebuild()
Rebuild the build.
- Returns:
The result of the rebuild operation.
- Return type:
- Raises:
JenkinsGeneralException – If a general exception occurs.
from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.jobs.search("new_freestyle").builds.search(9).rebuild())
The above code will output:
request=<Request object at 1582937378384> content='[200] Successfully triggered a rebuild of this build (9).' status_code=200