王子1986
王子1986

Reputation: 3599

How to get latest build number from another job in jenkins pipeline

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

Answers (5)

ranaraya
ranaraya

Reputation: 1

ls /var/jenkins_home/jobs/$PIPELINE_NAME/builds | grep -o '[0-9]\+' | sort -i -r | head -1

Upvotes: 0

Florian Lauck
Florian Lauck

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

hryamzik
hryamzik

Reputation: 995

def build_job = build job: "Build"
build_job_number = build_job.getNumber()

Upvotes: -2

王子1986
王子1986

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

Vitalii Vitrenko
Vitalii Vitrenko

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

Related Questions