<p>I am using Jenkins with the kubernetes plugin to run my jobs and I need to run a pipeline that:</p>
<ol>
<li>builds a docker image</li>
<li>submit it to the registry</li>
<li>Uses that same image in the following steps to perform the tests.</li>
</ol>
<pre><code>Container(image:A): build image B 
Container(image:B) : test image B
</code></pre>
<p>So I would like to use variables and substitute them inside the kubernetes podtemplate as here:</p>
<pre><code>pipeline {
  agent none
  stages {
    stage(&quot;Build image&quot;){
        // some script that builds the image
        steps{
            script{
                def image_name = &quot;busybox&quot;
            }
        }
    }
    stage('Run tests') {
      environment {
        image = &quot;$image_name&quot;
      }
      agent {
        kubernetes {
          yaml &quot;&quot;&quot;\
        apiVersion: v1
        kind: Pod
        metadata:
          labels:
            some-label: some-label-value
        spec:
          containers:
          - name: busybox
            image: &quot;${env.image}&quot;
            command:
            - cat
            tty: true
        &quot;&quot;&quot;.stripIndent()
        }
      }
      steps {
        container('busybox') {
          sh 'echo &quot;I am alive!!&quot;'
        }
      }
    }
  }
}
</code></pre>
<p>but the variable is empty as I get:</p>
<pre><code>[Normal][ci/test-10-g91lr-xtc20-s1ng1][Pulling] Pulling image &quot;null&quot;
[Warning][ci/test-10-g91lr-xtc20-s1ng1][Failed] Error: ErrImagePull
[Warning][ci/test-10-g91lr-xtc20-s1ng1][Failed] Failed to pull image &quot;null&quot;: rpc error: code = Unknown desc = Error response from daemon: pull access denied for null, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
</code></pre>
<p>Do you know how I can achieve a similar behaviour ?</p>

ale_tri
<p>Thank you zett42 for your answer, I was able to achieve my objective with your suggestions.</p>
<p>Basically the solution was to set in the build stage a global environment variable. I post here the full solution to help others in my same problem:</p>
<pre><code>pipeline {
  agent none
  stages {
    stage(&quot;Build image&quot;){
        // some script that builds the image
        steps{
            script{
                env.image_name = &quot;busybox&quot;
            }
        }
    }
    stage('Run tests') {
      agent {
        kubernetes {
          yaml &quot;&quot;&quot;\
        apiVersion: v1
        kind: Pod
        metadata:
          labels:
            some-label: some-label-value
        spec:
          containers:
          - name: busybox
            image: &quot;${env.image_name}&quot;
            command:
            - cat
            tty: true
        &quot;&quot;&quot;.stripIndent()
        }
      }
      steps {
        container('busybox') {
          sh 'echo &quot;I am alive!!&quot;'
        }
      }
    }
  }
}
</code></pre>
<p>To better understand it it was useful to read this article:</p>
<p><a href="https://e.printstacktrace.blog/jenkins-pipeline-environment-variables-the-definitive-guide/" rel="nofollow noreferrer">https://e.printstacktrace.blog/jenkins-pipeline-environment-variables-the-definitive-guide/</a></p>
