ArgoCD Resource Hooks.

gaurav agnihotri
3 min readJan 11, 2023

--

Helped In E2E setup

Argo + Hooks

A few Use cases for hooks are:

A- Before deploying a new version of the app, use a PreSync hook to perform a database schema migration.

B- Using a Sync hook to orchestrate a complex deployment requiring more sophistication than the Kubernetes rolling update strategy.

C- After a deployment, use a PostSync hook to run integration and health checks.

D- If a Sync operation fails, use a SyncFail hook to run cleanup or finalizer logic. Hooks for SyncFail are only available in v1.2

In My use case —

I need to create an End-2-End setup, which means that after deploying a service via ArgoCD and ensuring that the pods are operational, I need to immediately run the unit test to complete my CI/CD process.

As a result, PostSync hook is appropriate for my use case.

So, in this blog, we will discuss PostSync hook configuration.

Just walk through the below file once.

1 apiVersion: batch/v1
2 kind: Job
3 metadata:
4 name: ms-echoserver-Unit-test-trigger
5 annotations:
6 argocd.argoproj.io/hook: PostSync
7 argocd.argoproj.io/hook-delete-policy: HookSucceeded
8 spec:
9 template:
10 metadata:
11 labels:
12 name: unit-test
13 spec:
14 containers:
15 - name: unit-test
16 args:
17 - /bin/sh
18 - -ec
19 - "curl --location --request POST \"https://api.buildkite.com/v2/organizations/example/pipelines/unit-test/builds\" --header \"Authorization: Bearer ${GITHUB_TOKEN}\" --header \"Content-Type: application/json\"
20 --data-raw '{
21 \"commit\": \"HEAD\",
22 \"branch\": \"main\",
23 \"message\": \"Triggered by ArgoCD post deployment of ms-echoserver :rocket:\"
24 }'"
25 env:
26 - name: GITHUB_TOKEN
27 valueFrom:
28 secretKeyRef:
29 name: ms-echoserver-secret
30 key: token
31
32 image: curlimages/curl
33 restartPolicy: Never
34 backoffLimit: 0

I will talk about the lines that you want to consider in your setup.

Line 2 — kind: Job

I’m running it as a kind:job because it’s only needed after every successful service deployment, and once it’s triggered, the job’s task is completed, so kind:job is the right kind in my case.

Line 6 : argocd.argoproj.io/hook: PostSync [Hook annotation]

Line 7: argocd.argoproj.io/hook-delete-policy: HookSucceeded

[Once the hook has been triggered and the job has been completed, it will be deleted.]

Line 19: curl command

Edit Curl commands to suit your needs. In my situation, I use the buildkite tool to start my unit test.

curl --location --request POST  \"https://api.buildkite.com/v2/organizations/example/pipelines/unit-test-pipeline/builds\" --header \"Authorization: Bearer ${GITHUB_TOKEN}\" --header \"Content-Type: application/json\"

Line 32: I am triggering the unit-test-pipeline using Curl command and for that I Created a custom image which is having curl & Jq installed .

Docker file-

FROM alpine:3.15

RUN apk add --no-cache bash curl jq

ENTRYPOINT ["/bin/bash"]

This Docker file only contains curl and jq.

Tip:- Avoid installing any software or tools that you don’t need; this will help you to maintain security.

You can view the status of your pipeline of unit tests as follows:

Click Me-

ArgoCD All-in-One Setup Guide.

ArgoCD High Availability (HA) [Production Ready].

ArgoCD Ingress Setup

ArgoCD Okta Setup.

ArgoCD Image updater

ArgoCD Slack Notification Setup

I hope this post is informative and useful for you :)

If you enjoy the blog, please give me a Clap : ) and Follow me for more such content.

Crafting these articles demands countless hours of ideation, research, and writing. This year has seen me invest over 500 hours into this craft alone. If my work has brought you joy, would you kindly consider supporting me with a coffee? Your gesture would mean the world to me. If not, thank you dearly for your readership. ❤️

Buy-me-a-coffee ❤️

--

--