<p>I wrote a pipeline for an Hello World web app, nothing biggy, it's a simple hello world page.
I made it so if the tests pass, it'll deploy it to a remote kubernetes cluster.</p>
<p>My problem is that if I change the html page and try to redeploy into k8s the page remains the same (the pods aren't rerolled and the image is outdated).</p>
<p>I have the <code>autopullpolicy</code> set to always. I thought of using specific tags within the deployment yaml but I have no idea how to integrate that with my jenkins (as in how do I make jenkins set the <code>BUILD_NUMBER</code> as the tag for the image in the deployment).</p>
<p>Here is my pipeline:</p>
<pre><code>pipeline {

agent any
environment 
{
    user = &quot;NAME&quot;
    repo = &quot;prework&quot;
    imagename = &quot;${user}/${repo}&quot;
    registryCreds = 'dockerhub'
    containername = &quot;${repo}-test&quot;
}

stages 
{
    stage (&quot;Build&quot;)
    {
        steps {
        // Building artifact
            sh '''
            docker build -t ${imagename} .
            docker run -p 80 --name ${containername} -dt ${imagename}
            '''
            }
    }

    stage (&quot;Test&quot;)
    {
        steps {
            sh '''

            IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${containername})
            STATUS=$(curl -sL -w &quot;%{http_code} \n&quot; $IP:80 -o /dev/null)

                if [ $STATUS -ne 200 ]; then
                echo &quot;Site is not up, test failed&quot;
                exit 1
                fi
                echo &quot;Site is up, test succeeded&quot;
            '''
        }
    }
    stage (&quot;Store Artifact&quot;)
    {
        steps {
            echo &quot;Storing artifact: ${imagename}:${BUILD_NUMBER}&quot;
            script {
                docker.withRegistry('https://registry.hub.docker.com', 'dockerhub') {
                    def customImage = docker.image(imagename)
                customImage.push(BUILD_NUMBER)
                customImage.push(&quot;latest&quot;)
                }
            }
        }

    }
    stage (&quot;Deploy to Kubernetes&quot;)
    {
        steps {

            echo &quot;Deploy to k8s&quot;
            script {
            kubernetesDeploy(configs: &quot;deployment.yaml&quot;, kubeconfigId: &quot;kubeconfig&quot;) }
            }
    }

}

post {
    always {

            echo &quot;Pipeline has ended, deleting image and containers&quot;
        sh '''
            docker stop ${containername}
            docker rm ${containername} -f
        '''
    }
}
</code></pre>
<p>}</p>
<p>EDIT:
I used <code>sed</code> to replace the latest tag with the build number every time I'm running the pipeline and it works. I'm wondering if any of you have other ideas because it seems so messy right now.
Thanks.</p>

<p>According to the information from <a href="https://github.com/jenkinsci/kubernetes-cd-plugin#configure-the-plugin" rel="nofollow noreferrer">Kubernetes Continuous Deploy Plugin</a> p.6. you can add <code>enableConfigSubstitution: true</code> to <code>kubernetesDeploy()</code> section and use <code>${BUILD_NUMBER}</code> instead of <code>latest</code> in deployment.yaml:</p>
<blockquote>
<p>By checking &quot;Enable Variable Substitution in Config&quot;, the variables
(in the form of $VARIABLE or `${VARIABLE}) in the configuration files
will be replaced with the values from corresponding environment
variables before they are fed to the Kubernetes management API. This
allows you to dynamically update the configurations according to each
Jenkins task, for example, using the Jenkins build number as the image
tag to be pulled.</p>
</blockquote>
