How to Set the Image Tag Dynamically in CI With kustomize edit set image
Run kustomize edit set image inside the overlay to rewrite the image tag to the SHA you just built, then apply the result.
After building and pushing, cd into the overlay and run kustomize edit set image app=<registry>/app:<sha>. This pins the tag in kustomization.yaml before kubectl apply -k.
Steps
- Push the image tagged with
${{ github.sha }}. - In the overlay, run
kustomize edit set image app=registry/app:${{ github.sha }}. - Apply with
kubectl apply -k.
Workflow
.github/workflows/deploy.yml
steps:
- name: Pin image tag
working-directory: overlays/prod
run: |
kustomize edit set image \
app=ghcr.io/acme/app:${{ github.sha }}
- run: kubectl apply -k overlays/prod
- run: kubectl rollout status deployment/app -n prod --timeout=120sGotchas
- A plain
yqedit of.spec.template.spec.containers[0].imageworks too if you do not use Kustomize. - Prefer the image digest over a moving tag so the rollout is reproducible.
Related guides
How to Deploy With Kustomize (kubectl apply -k) From CI in GitHub ActionsDeploy an environment overlay from GitHub Actions with kubectl apply -k, letting Kustomize merge a base and o…
How to GitOps Deploy by Committing to a Config Repo From GitHub ActionsDeploy GitOps-style from GitHub Actions by bumping the image tag in a separate config repo and committing it,…