8

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
        }
    }
}
1
  • you are trying to print the build number ? Commented May 6, 2019 at 13:41

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

5

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.

Comments

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')
        }
    }
}

5 Comments

trying this with withCredentials() block will give you the groovy.lang.MissingPropertyException: No such property: BUILD_NUMBER for class: groovy.lang.Binding
Try the last example, while my withCredentials block is not exactly like yours, it did work for me.
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 - 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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.