github.com/argoproj/argo-cd@v1.8.7/docs/user-guide/resource_hooks.md (about)

     1  # Resource Hooks
     2  ## Overview
     3  
     4  Synchronization can be configured using resource hooks. Hooks are ways to run scripts before, during,
     5  and after a Sync operation. Hooks can also be run if a Sync operation fails at any point. Some use cases for hooks are:
     6  
     7  * Using a `PreSync` hook to perform a database schema migration before deploying a new version of the app.
     8  * Using a `Sync` hook to orchestrate a complex deployment requiring more sophistication than the
     9  Kubernetes rolling update strategy.
    10  * Using a `PostSync` hook to run integration and health checks after a deployment.
    11  * Using a `SyncFail` hook to run clean-up or finalizer logic if a Sync operation fails. _`SyncFail` hooks are only available starting in v1.2_
    12  
    13  ## Usage
    14  
    15  Hooks are simply Kubernetes manifests tracked in the source repository of your Argo CD Application annotated with `argocd.argoproj.io/hook`, e.g.:
    16  
    17  ```yaml
    18  apiVersion: batch/v1
    19  kind: Job
    20  metadata:
    21    generateName: schema-migrate-
    22    annotations:
    23      argocd.argoproj.io/hook: PreSync
    24  ```
    25  
    26  During a Sync operation, Argo CD will apply the resource during the appropriate phase of the
    27  deployment. Hooks can be any type of Kubernetes resource kind, but tend to be Pod,
    28  [Job](https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/)
    29  or [Argo Workflows](https://github.com/argoproj/argo). Multiple hooks can be specified as a comma
    30  separated list.
    31  
    32  The following hooks are defined:
    33  
    34  | Hook | Description |
    35  |------|-------------|
    36  | `PreSync` | Executes prior to the application of the manifests. |
    37  | `Sync`  | Executes after all `PreSync` hooks completed and were successful, at the same time as the application of the manifests. |
    38  | `Skip` | Indicates to Argo CD to skip the application of the manifest. |
    39  | `PostSync` | Executes after all `Sync` hooks completed and were successful, a successful application, and all resources in a `Healthy` state. |
    40  | `SyncFail` | Executes when the sync operation fails. _Available starting in v1.2_ |
    41  
    42  ### Generate Name
    43  
    44  Named hooks (i.e. ones with `/metadata/name`) will only be created once. If you want a hook to be re-created each time either use `BeforeHookCreation` policy (see below) or `/metadata/generateName`. 
    45  
    46  ## Selective Sync
    47  
    48  Hooks are not run during [selective sync](selective_sync.md).
    49  
    50  ## Hook Deletion Policies
    51  
    52  Hooks can be deleted in an automatic fashion using the annotation: `argocd.argoproj.io/hook-delete-policy`.
    53  
    54  ```yaml
    55  apiVersion: batch/v1
    56  kind: Job
    57  metadata:
    58    generateName: integration-test-
    59    annotations:
    60      argocd.argoproj.io/hook: PostSync
    61      argocd.argoproj.io/hook-delete-policy: HookSucceeded
    62  ```
    63  
    64  The following policies define when the hook will be deleted.
    65  
    66  | Policy | Description |
    67  |--------|-------------|
    68  | `HookSucceeded` | The hook resource is deleted after the hook succeeded (e.g. Job/Workflow completed successfully). |
    69  | `HookFailed` | The hook resource is deleted after the hook failed. |
    70  | `BeforeHookCreation` | Any existing hook resource is deleted before the new one is created (since v1.3). |
    71  
    72  As an alternative to hook deletion policies, both Jobs and Argo Workflows support the
    73  [`ttlSecondsAfterFinished`](https://kubernetes.io/docs/concepts/workloads/controllers/ttlafterfinished/)
    74  field in the spec, which let their respective controllers delete the Job/Workflow after it completes.
    75  
    76  ```yaml
    77  spec:
    78    ttlSecondsAfterFinished: 600
    79  ```
    80  
    81  ## Using A Hook To Send A Slack Message
    82  
    83  The following example uses the Slack API to send a a Slack message when sync completes or fails: 
    84  
    85  ```yaml
    86  apiVersion: batch/v1
    87  kind: Job
    88  metadata:
    89    generateName: app-slack-notification-
    90    annotations:
    91      argocd.argoproj.io/hook: PostSync
    92      argocd.argoproj.io/hook-delete-policy: HookSucceeded
    93  spec:
    94    template:
    95      spec:
    96        containers:
    97        - name: slack-notification
    98          image: curlimages/curl
    99          command:
   100            - "curl"
   101            - "-X"
   102            - "POST"
   103            - "--data-urlencode"
   104            - "payload={\"channel\": \"#somechannel\", \"username\": \"hello\", \"text\": \"App Sync succeeded\", \"icon_emoji\": \":ghost:\"}"
   105            - "https://hooks.slack.com/services/..."
   106        restartPolicy: Never
   107    backoffLimit: 2
   108  ```
   109  
   110  ```yaml
   111  apiVersion: batch/v1
   112  kind: Job
   113  metadata:
   114    generateName: app-slack-notification-fail-
   115    annotations:
   116      argocd.argoproj.io/hook: SyncFail
   117      argocd.argoproj.io/hook-delete-policy: HookSucceeded
   118  spec:
   119    template:
   120      spec:
   121        containers:
   122        - name: slack-notification
   123          image: curlimages/curl
   124          command: 
   125            - "curl"
   126            - "-X"
   127            - "POST"
   128            - "--data-urlencode"
   129            - "payload={\"channel\": \"#somechannel\", \"username\": \"hello\", \"text\": \"App Sync failed\", \"icon_emoji\": \":ghost:\"}"
   130            - "https://hooks.slack.com/services/..."
   131        restartPolicy: Never
   132    backoffLimit: 2
   133  ```