23

Generally, to get the artifact of the latest successful build, I do a wget on the below URL:

http://jenkins.com/job/job_name/lastSuccessfulBuild/artifact/artifact1/jenkins.txt

Is there a way, I can do a wget on lastSuccessfulBuild and get a build_id like below?

build_id=`wget http://jenkins.p2pcredit.local/job/job_name/lastSuccessfulBuild`
honk
  • 9,137
  • 11
  • 75
  • 83
Jason
  • 2,246
  • 6
  • 34
  • 53

7 Answers7

32

Yes, there is a way and it is pretty straightforward:

$ build_id=`wget -qO- jenkins_url/job/job_name/lastSuccessfulBuild/buildNumber`
$ echo $build_id
131 # that's my build number
Vitalii Elenhaupt
  • 7,146
  • 3
  • 27
  • 43
  • How does it work over https ? It keeps on giving me `Unable to establish SSL connection`. – Jason Apr 25 '16 at 04:24
  • The simplest way should be to add `--no-check-certificate` parameter to `wget`. Take a look at [HTTPS (SSL/TLS) Options](https://www.gnu.org/software/wget/manual/html_node/HTTPS-_0028SSL_002fTLS_0029-Options.html#HTTPS-_0028SSL_002fTLS_0029-Options) – Vitalii Elenhaupt Apr 26 '16 at 06:48
  • 1
    Be careful with this answer. If you want the value of `BUILD_ID` specifically, especially when using plugins that allow you to customize the build ID, this won't work, and neither will swapping out `buildNumber` for `buildID` – kayleeFrye_onDeck Jan 09 '19 at 20:52
  • Is there a URL to: get the latest build and open it in browser? – Amol Jan 09 '20 at 19:43
7

I think the best solution is using groovy with zero dependencies.

node {
    script{
        def lastSuccessfulBuildID = 0
        def build = currentBuild.previousBuild
        while (build != null) {
            if (build.result == "SUCCESS")
            {
                lastSuccessfulBuildID = build.id as Integer
                break
            }
            build = build.previousBuild
        }
        println lastSuccessfulBuildID
    }
}

You do not need specify jenkins_url or job_name etc to get last successful build id. Then you could use it easily in all Jenkinsfile in repositories without useless configurations.

Tested on Jenkins v2.164.2

networm
  • 349
  • 2
  • 4
  • Would it be possible without jenkins file? It works fine in a "dsl script" but I'm trying it with a Groovy Script, inside the job. It does not work (`import jenkins.model.Jenkins` in `Execute Groovy script ` step). I know it's very basic but I'm lost. Maybe specifying the classpath? – enagra Nov 07 '19 at 10:42
  • 1
    I answer myself with [this reference](https://wiki.jenkins.io/display/JENKINS/Groovy+plugin#Groovyplugin-GroovyScriptvsSystemGroovyScript) – enagra Nov 07 '19 at 12:11
  • This is only useful if you want the latest build of the job your in. If you need the latest successful build of another project this wont work. – user1024792 Jul 02 '21 at 02:45
5

If you want the DisplayName of the last successful job and not build number:

curl --user <username>:<tokenOrPassword> https://<url>/job/<job-name>/lastSuccessfulBuild/api/json | jq -r '.displayName'

Or in groovy

def buildName = Jenkins.instance.getItem('jobName').lastSuccessfulBuild.displayName
Michael
  • 1,577
  • 1
  • 18
  • 33
5

I find very useful querying permalinks file inside Jenkins workspace.

This allows you, to not only get the last successful build, but also other builds Jenkins considers relevant.

You can see it's content adding this line in Build section, in Execute Shell panel:

cat ../../jobs/$JOB_NAME/builds/permalinks

For example, in my case:

+ cat ../../jobs/$JOB_NAME/builds/permalinks
lastCompletedBuild 56
lastFailedBuild 56
lastStableBuild 51
lastSuccessfulBuild 51
lastUnstableBuild -1
lastUnsuccessfulBuild 56

From there, you would want to parse the number of the last successful build, or any other provided by permalinks, you can do this running:

lastSuccesfulBuildId=$(cat ../../jobs/$JOB_NAME/builds/permalinks | grep lastSuccessfulBuild | sed 's/lastSuccessfulBuild //')
Sebastian Juarez
  • 3,317
  • 2
  • 21
  • 20
2

Pipeline script solution :

import groovy.json.JsonSlurper           
def jResponse = httpRequest "https:/<yourjenkinsjoburlpath>/lastSuccessfulBuild/buildNumber"
def json = new JsonSlurper().parseText(jResponse.content)
echo "Status: ${json}"

jenkins console output:

HttpMethod: GET
URL: https://***/lastSuccessfulBuild/buildNumber
Sending request to url: https://***/lastSuccessfulBuild/buildNumber
Response Code: HTTP/1.1 200 OK
Success code from [100‥399]
[Pipeline] echo
Status: 20
karthick23
  • 1,313
  • 1
  • 8
  • 15
1

To get the last successful build number using curl:

curl --user userName:password https://url/job/jobName/api/xml?xpath=/*/lastStableBuild/number

  • You can also pass a `token` in place of password and the above curl request will work to retrieve last stable build number (not build name). Tested on jenkins 2.190.2 – Michael Nov 07 '19 at 16:07
0

to get the job build number simply do:

def build_Number = Jenkins.instance.getItem('JobName').lastSuccessfulBuild.number

cbmendes
  • 1
  • 3
  • There is an error `org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance` – v.ladynev Nov 30 '22 at 12:51
  • Go to manage jenkins > In Process Script Approval and approve that Jenkins.model.JenkinsgetInstance – cbmendes Dec 02 '22 at 21:42