Triggers

The trigger defines the condition when the notification should be sent. The definition includes name, condition and notification templates reference. The condition is a predicate expression that returns true if the notification should be sent. The trigger condition evaluation is powered by antonmedv/expr. The condition language syntax is described at Language-Definition.md.

The trigger is configured in argocd-notifications-cm ConfigMap. For example the following trigger sends a notification when application sync status changes to Unknown using the app-sync-status template:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: argocd-notifications-cm
  5. data:
  6. trigger.on-sync-status-unknown: |
  7. - when: app.status.sync.status == 'Unknown' # trigger condition
  8. send: [app-sync-status, github-commit-status] # template names

Each condition might use several templates. Typically each template is responsible for generating a service-specific notification part. In the example above app-sync-status template “knows” how to create email and slack notification and github-commit-status knows how to generate payload for Github webhook.

Conditions Bundles

Triggers are typically managed by administrators and encapsulate information about when and which notification should be sent. The end users just need to subscribe to the trigger and specify the notification destination. In order to improve user experience triggers might include multiple conditions with a different set of templates for each condition. For example, the following trigger covers all stages of sync status operation and use a different template for different cases:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: argocd-notifications-cm
  5. data:
  6. trigger.sync-operation-change: |
  7. - when: app.status.operationState.phase in ['Succeeded']
  8. send: [github-commit-status]
  9. - when: app.status.operationState.phase in ['Running']
  10. send: [github-commit-status]
  11. - when: app.status.operationState.phase in ['Error', 'Failed']
  12. send: [app-sync-failed, github-commit-status]

Avoid Sending Same Notification Too Often

In some cases, the trigger condition might be “flapping”. The example below illustrates the problem. The trigger is supposed to generate a notification once when Argo CD application is successfully synchronized and healthy. However, the application health status might intermittently switch to Progressing and then back to Healthy so the trigger might unnecessarily generate multiple notifications. The oncePer field configures triggers to generate the notification only when the corresponding application field changes. The on-deployed trigger from the example below sends the notification only once per observed Git revision of the deployment repository.

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: argocd-notifications-cm
  5. data:
  6. # Optional 'oncePer' property ensure that notification is sent only once per specified field value
  7. # E.g. following is triggered once per sync revision
  8. trigger.on-deployed: |
  9. when: app.status.operationState.phase in ['Succeeded'] and app.status.health.status == 'Healthy'
  10. oncePer: app.status.sync.revision
  11. send: [app-sync-succeeded]

oncePer

The oncePer filed is supported like as follows.

  1. apiVersion: argoproj.io/v1alpha1
  2. kind: Application
  3. metadata:
  4. annotations:
  5. example.com/version: v0.1
  1. oncePer: app.metadata.annotations["example.com/version"]

Default Triggers

You can use defaultTriggers field instead of specifying individual triggers to the annotations.

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: argocd-notifications-cm
  5. data:
  6. # Holds list of triggers that are used by default if trigger is not specified explicitly in the subscription
  7. defaultTriggers: |
  8. - on-sync-status-unknown
  9. defaultTriggers.mattermost: |
  10. - on-sync-running
  11. - on-sync-succeeded

Specify the annotations as follows to use defaultTriggers. In this example, slack sends when on-sync-status-unknown, and mattermost sends when on-sync-running and on-sync-succeeded.

  1. apiVersion: argoproj.io/v1alpha1
  2. kind: Application
  3. metadata:
  4. annotations:
  5. notifications.argoproj.io/subscribe.slack: my-channel
  6. notifications.argoproj.io/subscribe.mattermost: my-mattermost-channel

Functions

Triggers have access to the set of built-in functions.

Example:

  1. when: time.Now().Sub(time.Parse(app.status.operationState.startedAt)).Minutes() >= 5

time

Time related functions.


time.Now() Time

Executes function built-in Golang time.Now function. Returns an instance of Golang Time.


time.Parse(val string) Time

Parses specified string using RFC3339 layout. Returns an instance of Golang Time.

strings

String related functions.


strings.ReplaceAll() string

Executes function built-in Golang strings.ReplaceAll function.


strings.ToUpper() string

Executes function built-in Golang strings.ToUpper function.


strings.ToLower() string

Executes function built-in Golang strings.ToLower function.

sync


sync.GetInfoItem(app map, name string) string Returns the info item value by given name stored in the Argo CD App sync operation.

repo

Functions that provide additional information about Application source repository.


repo.RepoURLToHTTPS(url string) string

Transforms given GIT URL into HTTPs format.


repo.FullNameByRepoURL(url string) string

Returns repository URL full name (<owner>/<repoName>). Currently supports only Github, GitLab and Bitbucket.


repo.GetCommitMetadata(sha string) CommitMetadata

Returns commit metadata. The commit must belong to the application source repository. CommitMetadata fields:

  • Message string commit message
  • Author string - commit author
  • Date time.Time - commit creation date
  • Tags []string - Associated tags

repo.GetAppDetails() AppDetail

Returns application details. AppDetail fields:

  • Type string - AppDetail type
  • Helm HelmAppSpec - Helm details
  • Fields :
    • Name string
    • ValueFiles []string
    • Parameters []*v1alpha1.HelmParameter
    • Values string
    • FileParameters []*v1alpha1.HelmFileParameter
  • Methods :
    • GetParameterValueByName(Name string) Retrieve value by name in Parameters field
    • GetFileParameterPathByName(Name string) Retrieve path by name in FileParameters field *
  • Kustomize *apiclient.KustomizeAppSpec - Kustomize details
  • Directory *apiclient.DirectoryAppSpec - Directory details