FluxCD Webhook
Prerequisites
- Having a running Kubernetes cluster
- Bootstrapped fluxcd
- Knowledge on how to add charts/kubernetes resources with fluxcd
- Usage of an ingress to make the webhook accessible from outside your network
Initial Setup
-
Create a new folder called
webhooksinside theflux-systemfolder. -
Add a subfolder called
github -
Add the necessary kustomization to the
webhooksfolder for the github directory -
Next we will need 3 files inside the github folder:
receiver.yaml apiVersion: notification.toolkit.fluxcd.io/v1kind: Receivermetadata:name: github-receivernamespace: flux-systemspec:type: githubevents:- "ping"- "push"secretRef:name: github-webhook-tokenresources:- kind: GitRepositoryname: clusternamespace: flux-systemwebhook.secret.yaml apiVersion: v1kind: Secretmetadata:name: github-webhook-tokennamespace: flux-systemstringData:token: YOURSECRETKEYingress.yaml apiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: webhook-receivernamespace: flux-systemannotations:cert-manager.io/cluster-issuer: domain-1-le-prodcert-manager.io/private-key-rotation-policy: Alwaystraefik.ingress.kubernetes.io/router.entrypoints: websecuretraefik.ingress.kubernetes.io/router.middlewares: traefik-chain-basic@kubernetescrdtraefik.ingress.kubernetes.io/router.tls: "true"spec:rules:- host: flux-webhook.${DOMAIN_0}http:paths:- pathType: Prefixpath: /backend:service:name: webhook-receiverport:number: 80tls:- hosts:- flux-webhook.${DOMAIN_0}secretName: flux-webhook-tls-0 -
Add the necessary kustomization to add all 3 of those files to your cluster
Create your Webhook Token
-
Run the following command to generate your Webhook Token
Terminal window TOKEN=$(head -c 12 /dev/urandom | shasum | cut -d ' ' -f1)echo $TOKEN -
Replace
YOURSECRETKEYwith your generated Webhook Token -
Make sure to run
clustertool encryptto encrypt your files -
Push changes to your cluster before continuing
Get Receiver URL
After pushing the changes to your cluster and waiting for reconcile. Run the following command to get your receiver url:
kubectl -n flux-system get receiver/github-receiverOutput should look something like this:
NAME READY STATUSgithub-receiver True Receiver initialised with URL: /hook/bed6d00b5555b1603e1f59b94d7fdbca58089cb5663633fb83f2815dc626d92bNext you will have to add your domain and subdomain to it. Example
https://flux-webhook.mydomain.com/hook/bed6d00b5555b1603e1f59b94d7fdbca58089cb5663633fb83f2815dc626d92bMake sure the flux-webhook.mydomain.com is accessible from outside your network. Otherwise it will not work.
Adding the webhook to github
- Open your cluster repository on Github and go to settings
- Next select
Webhookson the left menu - Click
Add webhook - Add your Receiver Url into the
Payload URL - Select the
Content typetoapplication/x-www-form-urlencoded - Add your Webhook Token to the
Secretfield - Leave the rest of the options default
- Click
Add webhookto finish the creation
Finishing Steps
You should now see your Webhook in the Github Webhook Menu.
Click on it and check the Recent Deliveries if the test was successfull
If it worked. Your done and now each push to the main branch should trigger a reconcile on your cluster.