<p>I'm setting up a Jenkins pipeline with Kubernetes, there is an option to set environment variables for a container in <a href="https://github.com/jenkinsci/kubernetes-plugin#container-configuration" rel="nofollow noreferrer">containerTemplate</a>. Is there some option to override those values in <code>container</code> i.e.:</p>

<pre><code>container(
  name: 'my-container',
  envVars: [
    envVar(key: $KEY, value: $VALUE)
 ]) {
   ...
 }
</code></pre>

<p>because some variables are derived during build stages and cannot be set up in <code>podTemplate</code>. The example above unfortunately does not work.</p>

Sobik
<p>Note that as of this writing as per the <a href="https://github.com/jenkinsci/kubernetes-plugin#container-group-support" rel="nofollow noreferrer">docs</a>:</p>

<blockquote>
  <p>The container statement allows to execute commands directly into each container. This feature is considered ALPHA as there are still some problems with concurrent execution and pipeline resumption</p>
</blockquote>

<p>I believe there is not an option. However, you can try setting the variables in the <code>sh</code> command. For example:</p>

<pre><code>def label = "mypod-${UUID.randomUUID().toString()}"
podTemplate(label: label, containers: [
    containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: 'cat'),
    containerTemplate(name: 'golang', image: 'golang:1.8.0', ttyEnabled: true, command: 'cat')
  ]) {

    node(label) {
        stage('Get a Maven project') {
            git 'https://github.com/jenkinsci/kubernetes-plugin.git'
            container('maven') {
                stage('Build a Maven project') {
                    sh 'MYENV1=value1 MYEVN2=value2 mvn -B clean install'
                }
            }
        }

        stage('Get a Golang project') {
            git url: 'https://github.com/hashicorp/terraform.git'
            container('golang') {
                stage('Build a Go project') {
                    sh """
                    mkdir -p /go/src/github.com/hashicorp
                    ln -s `pwd` /go/src/github.com/hashicorp/terraform
                    MYENV1=value1 MYEVN2=value2 cd /go/src/github.com/hashicorp/terraform &amp;&amp; make core-dev
                    """
                }
            }
        }

    }
}
</code></pre>
