Guide demonstrating Argo CD hooks to enforce ordered PreSync, Sync, and PostSync jobs, manage sync waves and hook delete policies for migrations, notifications, and cleanup.
This guide continues the Sync Hooks demo and shows how to enforce ordering between jobs and deployments in Argo CD using hook annotations (PreSync, Sync, PostSync), sync waves, and hook-delete-policy. This prevents jobs such as database migrations from running in parallel with deployments and ensures predictable lifecycle and cleanup of hook resources.Why use hooks?
Ensure a DB migration runs before application Deployments.
Run notifications or cleanup only after a successful sync.
Control whether hook resources are retained or removed.
Hook phases act like guardrails: a PreSync hook must succeed before Sync proceeds; a failing PreSync stops the synchronization and prevents deployments.
How phases work
Happy path: PreSync → Sync → PostSync
If Sync fails, PostSync is typically not executed unless you configure alternative lifecycle behaviors.
Step 4 — Create the Argo CD Application
Create the application pointing to the folder with your hooks:
If the destination namespace does not exist, create it:
Copy
kubectl create namespace sync-hooks-2
If the app shows OutOfSync or Missing, it may indicate the namespace was absent or a previous sync is in progress. You can terminate a stuck sync and start a fresh manual sync.
What to expect during a sync
Argo CD creates the PreSync DB migration Job (hook icon shown).
Argo CD waits for the Job to finish successfully.
If the Job succeeds, Argo CD proceeds to create normal resources (Deployment, Service, etc.) — the Sync phase.
If the Sync succeeds, PostSync hooks (cleanup, notifications) execute as configured.
hook-delete-policy determines whether Jobs remain after success/failure.
During the demo:
The migration job was created and completed.
Deployments were created in the Sync phase.
A cleanup job ran afterwards and, due to the delete policy, the DB migration job was deleted.
Recommended patterns
Pattern
When to use
Notes
PreSync job + HookSucceeded
DB migrations or schema changes that must happen before deployments
Clean up after success to avoid clutter
PostSync job + HookSucceeded
Notifications (Slack, PagerDuty)
Use generateName for unique jobs
PostSync job + HookFailed
Cleanup only when sync fails
Preserve logs for debugging by keeping failed hooks
Summary
Use argocd.argoproj.io/hook: PreSync to force jobs to run before your main sync.
Use argocd.argoproj.io/hook: PostSync for notifications/cleanup after successful syncs.
Use argocd.argoproj.io/hook-delete-policy to control retention of hook resources.
Optionally use argocd.argoproj.io/sync-wave for finer-grained numeric ordering across many resources.