TIL - ArgoCD PR Previews: Use the Commit SHA, Not the PR Number
I was setting up PR preview environments with ArgoCD. The idea: open a pull request, and ArgoCD automatically deploys that branch so you can test it at a unique URL.
It worked on the first push. But when I pushed more commits to the same PR, nothing happened. The preview was stuck on my first commit.
What went wrong
My CI pipeline tagged Docker images with the PR number — my-app:pr-42. ArgoCD’s config also referenced pr-42. So when I pushed a second commit, CI built a new image and pushed it as my-app:pr-42 again (overwriting the old one). But ArgoCD’s config still said pr-42 — same as before. ArgoCD compared the old config to the new config, saw no difference, and did nothing.
# The problem: this tag never changes between pushes
images:
- my-app=my-registry/my-app:pr-{{number}}
The fix
ArgoCD’s pullRequest generator has a variable called head_short_sha_7 — it’s the first 7 characters of the latest commit on the PR branch. Every new push changes this value.
# Now each push produces a different tag
images:
- my-app=my-registry/my-app:sha-{{head_short_sha_7}}
CI just needs to tag images with the commit SHA instead of the PR number:
docker tag my-app my-registry/my-app:sha-$(git rev-parse --short=7 HEAD)
Now the flow works:
- I push commit
abc1234— CI buildsmy-app:sha-abc1234, ArgoCD deploys it - I push commit
def5678— CI buildsmy-app:sha-def5678, ArgoCD sees the tag changed and re-deploys
Every push gets its own unique image tag, so ArgoCD always picks up the change.