How to pass jenkins build environment into pod using kubernetes plugin?

9/23/2017

Env: Jenkins 2.73.1 & Kubernetes plugin 1.0

Inside the container, I like to get the normal jenkins build environment variable like BUILD_NUMBER

podTemplate(label: 'mypod', containers: [ containerTemplate(name: 'python', image: 'python:2.7.8', ttyEnabled: true) ]) {

node("mypod") {
    echo sh(returnStdout: true, script: 'env')

    container('python') {
        stage('Checkout') {
            sh "env"
        }
    }
}

}

So far in the code above, inside python, it doesn't have the traditional build variable.

Any solution to get those variables inside container?

-- Larry Cai
jenkins-pipeline
jenkins-plugins
kubernetes

1 Answer

9/23/2017

You can use env.BUILD_NUMBER i.e.

node{
    echo env.BUILD_NUMBER
}

Also if you want a list of all the env vars that are available you can run

node{
    echo "${env.getEnvironment()}"
}

These are the default jenkins plugins env vars but you can also set env vars for your kubernetes plugin build pods in the pod template, for example..

envVars: [
  envVar(key: 'GOPATH', value: '/home/jenkins/go')
]),

FWIW here's that code being used https://github.com/fabric8io/fabric8-pipeline-library/blob/3834f0f/vars/goTemplate.groovy#L27

More details here

-- James Rawlings
Source: StackOverflow