6

I don't find a way using the BUILD_NUMBER provided by jenkins in a sh script. I read some answers to similar questions but nothing seem to help.

node {
    echo "Build number 1 $BUILD_NUMBER"
    // output ok

    stage('stage1') {
        echo "Build number 2 $BUILD_NUMBER"
        // output ok

        def BUILD_NUMBER = "$BUILD_NUMBER"

        withCredentials([sshUserPrivateKey(credentialsId: 'github-rsa-key', variable: 'RSAKEY')]) {
            echo "Build number 3 " + BUILD_NUMBER
            // output ok

            echo "Build number 4 $BUILD_NUMBER"
            // output ok

            // -----------------

            sh 'echo $BUILD_NUMBER' // NullPointer
            sh "echo $BUILD_NUMBER" // NullPointer
            sh "echo \$BUILD_NUMBER" // NullPointer
            sh "echo BUILD_NUMBER" // NullPointer
            withEnv(["BUILD_NUMBER=BUILD_NUMBER"]) {
                sh "echo $BUILD_NUMBER" // NullPointer!!
            }
            env.BUILD_NUMER = "$BUILD_NUMBER"
            sh "echo $BUILD_NUMBER" // NullPointer
            sh "echo ${env.BUILD_NUMBER}" // NullPointer
        }
    }
}
snieguu
  • 2,073
  • 2
  • 20
  • 39
alex
  • 1,999
  • 3
  • 16
  • 18

3 Answers3

7

Basic solution: wrap shell script in """ block

node {
    echo "Build number 1: $BUILD_NUMBER"
    // output ok

    stage('stage1') {
        echo "Build number 2: $BUILD_NUMBER"
        // output ok

        def BUILD_NUMBER = "$BUILD_NUMBER"


            echo "Build number 3: " + BUILD_NUMBER
            // output ok

            echo "Build number 4: $BUILD_NUMBER"
            // output ok

            // -----------------

        sh 'printenv'

        sh """ 
            echo "Build number in sh script: ${env.BUILD_NUMBER}"
            echo "Job base name: ${env.JOB_BASE_NAME}"
        """
        // output ok
    }
}

Console Output:

Running on Jenkins in /var/lib/jenkins/workspace/test-infra-env
[Pipeline] {
[Pipeline] echo
Build number 1: 5
[Pipeline] stage
[Pipeline] { (stage1)
[Pipeline] echo
Build number 2: 5
[Pipeline] echo
Build number 3: 5
[Pipeline] echo
Build number 4: 5
[Pipeline] sh
+ printenv
JENKINS_HOME=/var/lib/jenkins
MAIL=/var/mail/jenkins
USER=jenkins
...
...
JOB_BASE_NAME=test-infra-env
BUILD_NUMBER=5
...
...
[Pipeline] sh
+ echo Build number in sh script: 5
Build number in sh script: 5
+ echo Job base name: test-infra-env
Job base name: test-infra-env
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Hieu Huynh
  • 1,005
  • 11
  • 18
4

There may be a more idiomatic approach (please share if you know) but it works if you define it in an environment block first. Something like:

stage('Show Build Number') {
  environment {
    BUILD_NUMBER = "${env.BUILD_NUMBER}"
  }
  steps {
    sh '''
      echo "This is build $BUILD_NUMBER"
    '''
  }
}

There is a good post on code maven with useful examples.

Nagev
  • 10,835
  • 4
  • 58
  • 69
2

Here's a simple example that works for me. Jenkins 2.164.2

Edit to add a physical script as well: /tmp/script.sh contains..

#!/bin/bash

echo "Script: - Build number: $BUILD_NUMBER"

And the Jenkins job

node {
    echo "Node: Build number: $BUILD_NUMBER"

    stage('stage1') {
        echo "Stage: Build number: $BUILD_NUMBER"

        sh ("echo Shell: Build number: $BUILD_NUMBER")

        sh ("/tmp/script.sh")
    }
}

This example uses a "withCredentials" block. Note the single quotes, which is referenced here - https://jenkins.io/doc/pipeline/steps/credentials-binding/

node {
    echo "Build number 1 $BUILD_NUMBER"
    // output ok

    stage('stage1') {
        withCredentials([string(credentialsId: 'my_password', variable: 'TOKEN')]) {
            sh '''
               echo "Shell: Build number: $BUILD_NUMBER"
            '''
            sh ('/tmp/script.sh')
        }
    }
}
Adam vonNieda
  • 1,635
  • 2
  • 14
  • 22
  • trying this with `withCredentials()` block will give you the `groovy.lang.MissingPropertyException: No such property: BUILD_NUMBER for class: groovy.lang.Binding` – alex May 06 '19 at 14:19
  • Try the last example, while my withCredentials block is not exactly like yours, it did work for me. – Adam vonNieda May 06 '19 at 14:52
  • your answer looks exactly the same as the question code, are you just saying it works as is in a newer version of jenkins? Or can you explain the difference and solution specifically in the description because as is I can't understand how to solve the issue. – justin.m.chase Jan 04 '21 at 18:56
  • @justin.m.chase - You're just trying to reference the Jenkins job build number variable from within a shell script called by the Jenkins job? Just reference it as a variable within the shell script, that's it. All I was doing was posting simple examples that work for me, to be helpful. I just tried the first example (the first two code blocks I wrote above), which is an external shell script called by a pipeline job, and it works. – Adam vonNieda Jan 05 '21 at 19:42
  • 1
    It also works for me, false alarm. I had another issue in the code which was accidentally reseting the var in the process and it made it seem like the env var was not set. – justin.m.chase Jan 05 '21 at 21:01