Skip to content
Latchkey

How to Deploy to Kubernetes from Bitbucket Pipelines

Run kubectl in a deployment step with a base64 kubeconfig variable, set the image, then wait for the rollout.

Use a kubectl image, decode a secured KUBECONFIG_B64 repository variable, then kubectl set image and kubectl rollout status. Scope it to a deployment for environment tracking.

Roll out from a deployment step

The kubeconfig comes from a secured variable; rollout status gates the step on health.

bitbucket-pipelines.yml
pipelines:
  branches:
    main:
      - step:
          name: Deploy
          deployment: production
          image: bitnami/kubectl:1.30
          script:
            - echo "$KUBECONFIG_B64" | base64 -d > kubeconfig
            - export KUBECONFIG=$PWD/kubeconfig
            - kubectl set image deployment/app app=myorg/app:$BITBUCKET_COMMIT
            - kubectl rollout status deployment/app --timeout=120s

Gotchas

  • Store the kubeconfig as a secured repository/deployment variable so it is masked in logs.
  • The bitnami/kubectl image has kubectl as the entrypoint; calling it via script: works because Bitbucket overrides the entrypoint with a shell.
  • Always kubectl rollout status --timeout so a stuck rollout fails the step instead of a false green.

Related guides

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