Jenkins kubernetes plugin not passing environment variables with Pipeline

7/28/2017

Jenkins ver. 2.60.1 (running in container on kubernetes)

Kubernetes Plugin ver. 0.11 (https://github.com/jenkinsci/kubernetes-plugin)

Pipeline test:

podTemplate(
  label: 'mypod',
  volumes: [
    persistentVolumeClaim(claimName: 'nfs-maven', mountPath: '/mnt/', readOnly: false)],
    envVars: [
      containerEnvVar(key: 'FOO', value: 'BAR'),
    ],
  containers: [
    containerTemplate(name: 'golang',
    image: 'golang',

    ttyEnabled: true, 
    command: 'cat',

    )]
)
{
  node('mypod') {
    stage('test env') {

        container('golang') {
            stage('build') {
                sh 'echo $FOO'
                sh 'sleep 3600'
            }
        }
    }
  }
}

The vars are not passed into the containers. The echo echoes nothing. echo $FOO or echo \$FOO I have tried on the pod level and container level.

When i describe the created pod i only get the following environment vars:

Environment:                                                                                                                               
  JENKINS_LOCATION_URL:     http://ldn1-kube1:31000/                                                                                       
  JENKINS_SECRET:           107cb696a8792f998fd41b6ccacf833ea74941fc9a95c39c4b2a1cde4c008b35                                               
  JENKINS_JNLP_URL:         http://10.233.60.248:8080/computer/kubernetes-57beb710bfb44cea8f964d63049b2942-355760c790d6b/slave-agent.jnlp  
  JENKINS_TUNNEL:           10.233.60.248:50000                                                                                            
  JENKINS_NAME:             kubernetes-57beb710bfb44cea8f964d63049b2942-355760c790d6b                                                      
  JENKINS_URL:              http://10.233.60.248:8080                                                                                      
  HOME:                     /home/jenkins                                                                                                  
-- Kascha Sain
environment-variables
jenkins
jenkins-pipeline
kubernetes

2 Answers

8/2/2017

I'm guessing a little, but I don't think envVars in podTemplate is fully functional. Like you, I haven't had luck with the podTemplate, but I've had no problem using envVars at the containerTemplate level. The easy fix is to add your envVars there instead.

podTemplate(
  label: 'mypod',
  volumes: [
    persistentVolumeClaim(claimName: 'nfs-maven', mountPath: '/mnt/', readOnly: false)],
  containers: [
    containerTemplate(
      name: 'golang',
      image: 'golang',
      ttyEnabled: true, 
      command: 'cat',
      envVars: [containerEnvVar(key: 'FOO', value: 'BAR')]
    )
  ]
)
{
  node('mypod') {
    stage('test env') {

        container('golang') {
            stage('build') {
                sh 'echo $FOO'
                sh 'sleep 3600'
            }
        }
    }
  }
}
-- coreypobrien
Source: StackOverflow

8/2/2017

Upgrading the kubernetes-plugin to 0.12 (29/07/2017) and restarting jenkins has fixed the issue!

-- Kascha Sain
Source: StackOverflow