Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Jenkins Environment Variable in Pipeline SH script

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
        }
    }
}
like image 440
alex Avatar asked May 06 '19 13:05

alex


3 Answers

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
like image 85
Hieu Huynh Avatar answered Sep 27 '22 18:09

Hieu Huynh


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.

like image 20
Nagev Avatar answered Sep 27 '22 19:09

Nagev


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')
        }
    }
}
like image 22
Adam vonNieda Avatar answered Sep 27 '22 17:09

Adam vonNieda