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=120sGotchas
- 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 --timeoutso a stuck rollout fails the step instead of a false green.
Related guides
How to Deploy to Kubernetes from GitHub ActionsDeploy to Kubernetes from GitHub Actions - configure kubectl with a secret kubeconfig (or cloud OIDC), set th…
How to Deploy on a Tag in Bitbucket PipelinesDeploy on a tag in Bitbucket Pipelines with the tags: pipeline section and glob patterns so only matching tag…