GitOps with Helm and Argo CD
Argo CD Application Hooks and Pitfalls to Avoid
Application Hooks Overview and Examples
Hooks can be defined to run before or after the application's deployment. For instance, a script can be run to send a notification to a Slack channel or an email to your team.
To define a hook, a Job must be created.
ℹ️ A Job is a Kubernetes-native resource that creates one or more Pods to perform a specific task that runs to completion and then terminates.
Here is a Job that sends an email after the application has been successfully deployed. It uses the argocd.argoproj.io/hook: PostSync annotation to specify that it should run after the application's synchronization. The argocd.argoproj.io/hook-delete-policy: HookSucceeded annotation is used to delete the Job after it has successfully completed.
# Define a secret to store the SMTP password
---
apiVersion: v1
kind: Secret
metadata:
name: smtp-password
namespace: argocd
type: Opaque
stringData:
password:
# Define the Job to send an email
---
apiVersion: batch/v1
kind: Job
metadata:
generateName: email-
namespace: argocd
annotations:
argocd.argoproj.io/hook: PostSync
argocd.argoproj.io/hook-delete-policy: HookSucceeded
spec:
template:
spec:
containers:
- name: email
image: bytemark/smtp
command: ["/bin/sh"]
args:
- "-c"
- "echo 'Hello World!' | mail -s 'Hello World!' "
env:
- name: RELAY_HOST
value:
- name: RELAY_PORT
value:
- name: RELAY_USERNAME
value:
- name: RELAY_PASSWORD
valueFrom:
secretKeyRef:
name: smtp-password
key: password
# Never restart the job container if it fails
# but let the job controller handle retries
restartPolicy: Never
# Retry the job 4 times if it fails then give up
backoffLimit: 4
You should be able to see the Job after running the previous command:
kubectl get jobs -n argocd
As well as the logs of the created container:
kubectl logs -n argocd email--
As in the above example, we can create a Job that will send a notification to a Slack channel:
apiVersion: batch/v1
kind: Job
metadataHelm in Practice
Designing, Deploying, and Operating Kubernetes Applications at ScaleEnroll now to unlock current content and receive all future updates for free. Your purchase supports the author and fuels the creation of more exciting content. Act fast, as the price will rise as the course nears completion!
Hurry! This limited time offer ends in:
To redeem this offer, copy the coupon code below and apply it at checkout:
