2
votes

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                                                                                                  
2
Are you able to run kubectl get -o yaml pod -l mypod (I'd have to check the syntax for a label without a value, or of course you can get the pod's name) to see how the podTemplate is applying in real life? I also find it super,super,super suspicious none of the k8s service env-vars are present (KUBERNETES_SERVICE_HOST, KUBERNETES_SERVICE_PORT, etc)mdaniel
Thanks Matthew. Please see output here pastebin.com/vM5PkzhcKascha Sain

2 Answers

2
votes

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

0
votes

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