Reputation: 3599
I am using jenkins pipeline 2.0, and I would like to get another job's latest successful build number.
What's the pipeline syntax to use?
Upvotes: 15
Views: 36713
Reputation: 1
ls /var/jenkins_home/jobs/$PIPELINE_NAME/builds | grep -o '[0-9]\+' | sort -i -r | head -1
Upvotes: 0
Reputation: 431
To add to Vitalii's answer, this is in case you're using Multibranch Pipeline Plugin:
def buildNumber = Jenkins.instance.getItem('jobName').getItem('branchName').lastSuccessfulBuild.number
Upvotes: 12
Reputation: 995
def build_job = build job: "Build"
build_job_number = build_job.getNumber()
Upvotes: -2
Reputation: 3599
It's so annoying to get approvals in enterprise environment(a lot of request and approvals) So I am using following API way to get the latest build number.
import groovy.json.JsonSlurperClassic
httpRequest url: 'https://jenkinsurl.local/job/Build/api/json', outputFile: 'output.json'
def jsonFile = readFile(file: 'output.json')
def data = new JsonSlurperClassic().parseText(jsonFile)
latestBuildNumber = "${data.lastSuccessfulBuild.number}"
Upvotes: 9
Reputation: 10395
You can get it this way
def buildNumber = Jenkins.instance.getItem('jobName').lastSuccessfulBuild.number
If you get a RejectedAccessException
you will have to approve those methods, see In-process Script Approval
Upvotes: 28