Skip to content
Latchkey

How to Deploy to Kubernetes from a Jenkins Pipeline

Bind a kubeconfig credential with withKubeConfig, run kubectl set image, then wait for the rollout to finish.

Use the Kubernetes CLI plugin's withKubeConfig (or a Secret file credential) to point kubectl at your cluster, update the image, and gate the build on rollout status.

Roll out inside withKubeConfig

The credential supplies cluster access; rollout status makes the stage fail on a stuck rollout.

Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Deploy') {
      when { branch 'main' }
      steps {
        withKubeConfig([credentialsId: 'prod-kubeconfig']) {
          sh 'kubectl set image deployment/app app=myorg/app:${BUILD_NUMBER}'
          sh 'kubectl rollout status deployment/app --timeout=120s'
        }
      }
    }
  }
}

Gotchas

  • withKubeConfig comes from the Kubernetes CLI plugin; without it, fall back to a file credential bound via withCredentials.
  • Always kubectl rollout status --timeout so a stalled rollout aborts the stage instead of passing.
  • The agent must have kubectl installed (or run the stage in a container image that has it).

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →