github.com/argoproj/argo-cd/v2@v2.10.5/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.
    12  * Using a `PostDelete` hook to run clean-up or finalizer logic after all Application resources are deleted. Please note that
    13    `PostDelete` hooks are only deleted if the delete policy matches the aggregated deletion hooks status and not garbage collected after the application is deleted. 
    14  
    15  ## Usage
    16  
    17  Hooks are simply Kubernetes manifests tracked in the source repository of your Argo CD Application annotated with `argocd.argoproj.io/hook`, e.g.:
    18  
    19  ```yaml
    20  apiVersion: batch/v1
    21  kind: Job
    22  metadata:
    23    generateName: schema-migrate-
    24    annotations:
    25      argocd.argoproj.io/hook: PreSync
    26  ```
    27  
    28  During a Sync operation, Argo CD will apply the resource during the appropriate phase of the
    29  deployment. Hooks can be any type of Kubernetes resource kind, but tend to be Pod,
    30  [Job](https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/)
    31  or [Argo Workflows](https://github.com/argoproj/argo). Multiple hooks can be specified as a comma
    32  separated list.
    33  
    34  The following hooks are defined:
    35  
    36  | Hook | Description |
    37  |------|-------------|
    38  | `PreSync` | Executes prior to the application of the manifests. |
    39  | `Sync`  | Executes after all `PreSync` hooks completed and were successful, at the same time as the application of the manifests. |
    40  | `Skip` | Indicates to Argo CD to skip the application of the manifest. |
    41  | `PostSync` | Executes after all `Sync` hooks completed and were successful, a successful application, and all resources in a `Healthy` state. |
    42  | `SyncFail` | Executes when the sync operation fails. |
    43  | `PostDelete` | Executes after all Application resources are deleted. _Available starting in v2.10._ |
    44  
    45  ### Generate Name
    46  
    47  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`. 
    48  
    49  ## Selective Sync
    50  
    51  Hooks are not run during [selective sync](selective_sync.md).
    52  
    53  ## Hook Deletion Policies
    54  
    55  Hooks can be deleted in an automatic fashion using the annotation: `argocd.argoproj.io/hook-delete-policy`.
    56  
    57  ```yaml
    58  apiVersion: batch/v1
    59  kind: Job
    60  metadata:
    61    generateName: integration-test-
    62    annotations:
    63      argocd.argoproj.io/hook: PostSync
    64      argocd.argoproj.io/hook-delete-policy: HookSucceeded
    65  ```
    66  
    67  The following policies define when the hook will be deleted.
    68  
    69  | Policy | Description |
    70  |--------|-------------|
    71  | `HookSucceeded` | The hook resource is deleted after the hook succeeded (e.g. Job/Workflow completed successfully). |
    72  | `HookFailed` | The hook resource is deleted after the hook failed. |
    73  | `BeforeHookCreation` | Any existing hook resource is deleted before the new one is created (since v1.3). It is meant to be used with `/metadata/name`. |
    74  
    75  Note that if no deletion policy is specified, Argo CD will automatically assume `BeforeHookCreation` rules.
    76  
    77  ### Sync Status with Jobs/Workflows with Time to Live (ttl)
    78  
    79  Jobs support the [`ttlSecondsAfterFinished`](https://kubernetes.io/docs/concepts/workloads/controllers/ttlafterfinished/)
    80  field in the spec, which let their respective controllers delete the Job after it completes. Argo Workflows support a 
    81  [`ttlStrategy`](https://argoproj.github.io/argo-workflows/fields/#ttlstrategy) property that also allow a Workflow to be 
    82  cleaned up depending on the ttl strategy chosen.
    83  
    84  Using either of the properties above can lead to Applications being OutOfSync. This is because Argo CD will detect a difference 
    85  between the Job or Workflow defined in the git repository and what's on the cluster since the ttl properties cause deletion of the resource after completion.
    86  
    87  However, using deletion hooks instead of the ttl approaches mentioned above will prevent Applications from having a status of 
    88  OutOfSync even though the Job or Workflow was deleted after completion.
    89  
    90  ## Using A Hook To Send A Slack Message
    91  
    92  The following example uses the Slack API to send a Slack message when sync completes or fails:
    93  
    94  ```yaml
    95  apiVersion: batch/v1
    96  kind: Job
    97  metadata:
    98    generateName: app-slack-notification-
    99    annotations:
   100      argocd.argoproj.io/hook: PostSync
   101      argocd.argoproj.io/hook-delete-policy: HookSucceeded
   102  spec:
   103    template:
   104      spec:
   105        containers:
   106        - name: slack-notification
   107          image: curlimages/curl
   108          command:
   109            - "curl"
   110            - "-X"
   111            - "POST"
   112            - "--data-urlencode"
   113            - "payload={\"channel\": \"#somechannel\", \"username\": \"hello\", \"text\": \"App Sync succeeded\", \"icon_emoji\": \":ghost:\"}"
   114            - "https://hooks.slack.com/services/..."
   115        restartPolicy: Never
   116    backoffLimit: 2
   117  ```
   118  
   119  ```yaml
   120  apiVersion: batch/v1
   121  kind: Job
   122  metadata:
   123    generateName: app-slack-notification-fail-
   124    annotations:
   125      argocd.argoproj.io/hook: SyncFail
   126      argocd.argoproj.io/hook-delete-policy: HookSucceeded
   127  spec:
   128    template:
   129      spec:
   130        containers:
   131        - name: slack-notification
   132          image: curlimages/curl
   133          command: 
   134            - "curl"
   135            - "-X"
   136            - "POST"
   137            - "--data-urlencode"
   138            - "payload={\"channel\": \"#somechannel\", \"username\": \"hello\", \"text\": \"App Sync failed\", \"icon_emoji\": \":ghost:\"}"
   139            - "https://hooks.slack.com/services/..."
   140        restartPolicy: Never
   141    backoffLimit: 2
   142  ```