<p>To clarify, this is not a question about running Jenkins in Kubernetes, this is about deploying to Kubernetess from Jenkins.</p>

<p>I have recently settled on using Jenkins (and the workflow/pipeline plugin) to orchestrate our delivery process. Currently, I'm using the imperative style to deploy as per below:</p>

<pre><code>stage 'Deploy to Integ'
  // Clean up old releases
  sh "kubectl delete svc,deployment ${serviceName} || true"
    def cmd = """kubectl run ${serviceName} --image=${dockerRegistry}/${serviceName}:${env.BUILD_NUMBER} --replicas=2 --port=${containerPort} --expose --service-overrides='{ "spec": { "type": "LoadBalancer"  }}' """
    // execute shell for the command above
  sh cmd
</code></pre>

<p>This works well because the <code>${env.BUILD_NUMBER}</code> persists through the pipeline, making it easy for me to ensure the version I deploy is the same all the way through. The problem I have is that I would like to use the declarative approach as this isn't scalable, and I would like the definition in VCS.</p>

<p>Unfortunately, the declarative approach comes with the adverse effect of needing to explicitly state the version of the image (to be deployed) in the yaml. One way around this might be to use the <code>latest</code> tag, however this comes with its own risks. For example, lets take the scenario where I'm about to deploy <code>latest</code> to production and a new version gets tagged <code>latest</code>. The new <code>latest</code> may not have gone through testing.</p>

<p>I could get into changing the file programmatically, but that feels rather clunky, and doesn't help developers who have the file checked out to understand what is latest.</p>

<p>What have you done to solve this issue? Am I missing something obvious? What workflow are you using?</p>

<p>In my yaml file (server.origin.yml), I set my image as <code>image-name:$BUILD_NUMBER</code></p>

<p>Then I run: <code>envsubst &lt; ./server.origin.yml &gt; ./server.yml</code><br>
This command will replace the string <code>$BUILD_NUMBER</code> by the value of the environment variable</p>
