Jenkinsfile pipeline syntax to insert list environment variable

4/23/2018

I am having hard to trying to figure out how to add envVars to kubernet inside Jenkinsfile.

I am pretty sure the issue is in my syntax because I am getting following error

ava.lang.ClassCastException: class org.csanchez.jenkins.plugins.kubernetes.
ContainerTemplate.setEnvVars() expects java.util.List<org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar
> but received class java.lang.String

when I have it coded this way

stage("build") {
                agent {
                    kubernetes {
                        label 'kubernetes'
                        containerTemplate {
                            name 'jnlp'
                            image 'ubuntu:last'
                            ttyEnabled true
                            label 'label'
                            envVars '
                               envVar(key: "filePath", value: "/home/abcde/abc" )'
                        }  
                    }
                }

Can you guys please point me to right direction? How do I define list variable in Jenkinsfile?

My Jenkinsfile setup

pipeline {
agent any
parameters {
    string(name: 'Abc', defaultValue: 'origin', description: 'test project')
}
options {
    timestamps()
    timeout(60)
}
stages {
    stage('Build') {
        parallel {
            stage("build") {
                agent {
                    kubernetes {
                        label 'kubernetes'
                        containerTemplate {
                            name 'jnlp'
                            image 'ubuntu:latest'
                            ttyEnabled true
                            label 'label'
                            envVars 'envVar(key: "filePath", value: "/home/abcde/abc" )'
                        }
                    }
                }
                steps {
                    container('jnlp') {
                        timeout(60) {
                            // build process
                        }
                    }
                }
            }
        }
    }
}
post {
    success {
        sh "success"
    }
    failure {
        sh "failed"
    }
    unstable {
        sh "unsable"
    }
  } 
}

With above code, I will get following error

ava.lang.ClassCastException: class org.csanchez.jenkins.plugins.kubernetes.
ContainerTemplate.setEnvVars() expects java.util.List<org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar
> but received class java.lang.String
-- user3570661
jenkins
jenkins-pipeline
kubernetes

3 Answers

4/23/2018

Look at their example https://github.com/jenkinsci/kubernetes-plugin/blob/f6cff5d7e9ce9da3279660159e0cb064efac534f/examples/selenium.groovy#L18

looks like in your case it should be

stage("build") {
    agent {
        kubernetes {
            label 'kubernetes'
            containerTemplate {
                name: 'jnlp',
                image: 'ubuntu:last',
                ttyEnabled: true,
                label: 'kub_catkin_node',
                envVars: [
                    containerEnvVar(key: "filePath", value: "/home/abcde/abc" )
                ]
            }
        }
    }
}
-- Yuri G.
Source: StackOverflow

4/27/2018

This is how i got this to work. Careful with Yaml syntax. Yaml doesn't like tabs

pipeline {
agent any
parameters {
    string(name: 'Abc', defaultValue: 'origin', description: 'The Gitlab project name')
}
options {
    timestamps()
    timeout(60)
}
stages {
    stage('Build') {
        parallel {
            stage("build") {
                agent {
                     kubernetes {
                     label 'label'
                     defaultContainer 'jnlp'
                     yaml """
apiVersion: v1
kind: Pod
metadata:
  labels:
    some-label: label
spec:
  containers:
  - name: jnlp
    image: ubuntu:latest
    tty: true
    env:
    -   name: 'filePATH'
        value: 'fileValue'
"""
                        }
                        steps {
                            container('jnlp') {
                                timeout(60) {
                                    // build process
                                }
                            }
                        }
                    }
                }
            }
        }
        post {
            success {
                sh "success"
            }
            failure {
                sh "failed"
            }
            unstable {
                sh "unsable"
            }
          } 
        }
-- user3570661
Source: StackOverflow

4/25/2018

This is something supported from the UI and also from pipelines, but it might not be well supported in declarative pipelines. One solution could be to use pipeline scripts. An other could be to check if its better supported in later version (if you are not already on the latest).

-- iocanel
Source: StackOverflow