Webhook

Configuration

The webhook notification service allows sending a generic HTTP request using the templatized request body and URL. Using Webhook you might trigger a Jenkins job, update Github commit status.

Use the following steps to configure webhook:

1 Register webhook in argocd-notifications-cm ConfigMap:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: <config-map-name>
  5. data:
  6. service.webhook.<webhook-name>: |
  7. url: https://<hostname>/<optional-path>
  8. headers: #optional headers
  9. - name: <header-name>
  10. value: <header-value>
  11. basicAuth: #optional username password
  12. username: <username>
  13. password: <api-key>

2 Define template that customizes webhook request method, path and body:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: <config-map-name>
  5. data:
  6. template.github-commit-status: |
  7. webhook:
  8. <webhook-name>:
  9. method: POST # one of: GET, POST, PUT, PATCH. Default value: GET
  10. path: <optional-path-template>
  11. body: |
  12. <optional-body-template>
  13. trigger.<trigger-name>: |
  14. - when: app.status.operationState.phase in ['Succeeded']
  15. send: [github-commit-status]

3 Create subscription for webhook integration:

  1. apiVersion: argoproj.io/v1alpha1
  2. kind: Application
  3. metadata:
  4. annotations:
  5. notifications.argoproj.io/subscribe.<trigger-name>.<webhook-name>: ""

Examples

Set Github commit status

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: <config-map-name>
  5. data:
  6. service.webhook.github: |
  7. url: https://api.github.com
  8. headers: #optional headers
  9. - name: Authorization
  10. value: token $github-token

2 Define template that customizes webhook request method, path and body:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: <config-map-name>
  5. data:
  6. service.webhook.github: |
  7. url: https://api.github.com
  8. headers: #optional headers
  9. - name: Authorization
  10. value: token $github-token
  11. template.github-commit-status: |
  12. webhook:
  13. github:
  14. method: POST
  15. path: /repos/{{call .repo.FullNameByRepoURL .app.spec.source.repoURL}}/statuses/{{.app.status.operationState.operation.sync.revision}}
  16. body: |
  17. {
  18. {{if eq .app.status.operationState.phase "Running"}} "state": "pending"{{end}}
  19. {{if eq .app.status.operationState.phase "Succeeded"}} "state": "success"{{end}}
  20. {{if eq .app.status.operationState.phase "Error"}} "state": "error"{{end}}
  21. {{if eq .app.status.operationState.phase "Failed"}} "state": "error"{{end}},
  22. "description": "ArgoCD",
  23. "target_url": "{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
  24. "context": "continuous-delivery/{{.app.metadata.name}}"
  25. }

Start Jenkins Job

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: <config-map-name>
  5. data:
  6. service.webhook.jenkins: |
  7. url: http://<jenkins-host>/job/<job-name>/build?token=<job-secret>
  8. basicAuth:
  9. username: <username>
  10. password: <api-key>
  11. type: Opaque

Send form-data

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: <config-map-name>
  5. data:
  6. service.webhook.form: |
  7. url: https://form.example.com
  8. headers:
  9. - name: Content-Type
  10. value: application/x-www-form-urlencoded
  11. template.form-data: |
  12. webhook:
  13. form:
  14. method: POST
  15. body: key1=value1&key2=value2

Send Slack

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: <config-map-name>
  5. data:
  6. service.webhook.slack_webhook: |
  7. url: https://hooks.slack.com/services/xxxxx
  8. headers:
  9. - name: Content-Type
  10. value: application/json
  11. template.send-slack: |
  12. webhook:
  13. slack_webhook:
  14. method: POST
  15. body: |
  16. {
  17. "attachments": [{
  18. "title": "{{.app.metadata.name}}",
  19. "title_link": "{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
  20. "color": "#18be52",
  21. "fields": [{
  22. "title": "Sync Status",
  23. "value": "{{.app.status.sync.status}}",
  24. "short": true
  25. }, {
  26. "title": "Repository",
  27. "value": "{{.app.spec.source.repoURL}}",
  28. "short": true
  29. }]
  30. }]
  31. }