# Automate a sync in ArgoCD

After integrating Cortex with [ArgoCD](/ingesting-data-into-cortex/integrations/argocd.md), you can use [Workflows](/streamline/workflows.md) to streamline and automate daily tasks. Rather than manually performing a sync through the ArgoCD UI, you can automate syncs by leveraging the ArgoCD API in a Workflow.

This guide walks through a Workflow that will sync the ArgoCD application to the same cluster that ArgoCD is running, but to a different namespace, and will allow the user to choose options for the sync.

## How to automate an ArgoCD sync with a Workflow in Cortex

### Prerequisites

Before getting started:

* Create an [API token in ArgoCD](https://argo-cd.readthedocs.io/en/stable/).
  * In ArgoCD, navigate to your account settings then click **Tokens > Generate New**. Copy the token and store it in a secure location, as you will need it in the next steps.\
    ![argocd actions 1](/files/kl5nlNgBs33d2MqGvTZE)
* Create a secret in Cortex.
  * Give the secret the name `argocd`.
  * For the secret value, enter the value of your ArgoCD API token.

### Step 1: Create a Workflow

1. In the main nav of Cortex, click **Workflows**.
2. Follow the steps of the [Workflows documentation](/streamline/workflows/create.md) to start creating a Workflow from scratch and configure its settings.
   * Give the Workflow a descriptive name such as "Create and sync application."
   * In the **Response template** field you can pass a blank response body or use [Mustache templating](https://mustache.github.io/) to create messages depending on what the user enters in each field.

     * For example, you may want to configure a response to confirm whether a sync was successful when the workflow is run. You could add the following to check the `sync.status` field. This example assumes you add an HTTP request action with the slug `http-request` in your Workflow:

     ```
     {{#actions.http-request.outputs.body.status.sync.status}}
     Successfully synced application!
     {{/actions.http-request.outputs.body.status.sync.status}}
     {{^actions.http-request.outputs.body.status.sync.status}}
     Unable to sync application. Are you sure this application exists in ArgoCD?
     {{/actions.http-request.outputs.body.status.sync.status}}
     ```

### Step 2: Add blocks to the Workflow

In your Workflow, you will add an HTTP request block to call the ArgoCD sync via the [ArgoCD API](https://argoproj.github.io/argo-events/sensors/triggers/http-trigger/), and a user input block where you define inputs that map to the JSON body of the block. The inputs should be the same sync options shown under **Sync** in your ArgoCD instance. The instructions below map `REVISION`, `DRY RUN`, and `PRUNE` as the sync options.

See the [Workflows documentation](/streamline/workflows/blocks.md) for more information on configuring blocks.

1. In the Workflow, click **+** to add a block. Select **User input**. Configure the block:
   * **Block name**: Enter a name for the block (e.g., `User input`).
   * **Slug**: The slug field is automatically populated based on the block name.
   * Under "User input fields," click **Add input**. Configure the input:
     * **Type**: Select `Text`.
     * **Name**: Enter `Revision`.
     * **Placeholder**: Enter `HEAD`.
     * At the bottom of the modal, click **Add input field**.
   * Under "User input fields," click **Add input**. Configure another input:
     * **Type**: Select `Toggle`.
     * **Name**: Enter `Dry run`.
     * At the bottom of the modal, click **Add input field**.
   * Under "User input fields," click **Add input**. Configure another input:
     * **Type**: Select `Toggle`.
     * **Name**: Enter `Prune`.
     * At the bottom of the modal, click **Add input field**.
2. Click **Save**.
3. In the Workflow, click **+** to add another block. Select **HTTP request**. Configure the block:
   * **Block name**: Enter a name for the block (e.g., `HTTP request`).
   * **Slug**: The slug field is automatically populated based on the block name.
   * **HTTP method**: Select `POST`.
   * **URL**: Enter the target URL.
   * **Headers**: Enter the following key/value pairs:
     * **Header Key**: `Authorization`, **Header Value**: `Bearer {{{context.secrets.argocd}}}`
       * If you used a different name for your secret, replace `argocd` with your secret's name.
   * **Payload**: Enter the payload referencing the output of the "User input" block. The payload that you supply can be templated and can reference an entity that the Workflow is being run with.

     * In this example, you could enter:

     ```json
     {
       "prune": {{context.actions.user-input.outputs.prune}},
       "dryRun": {{context.actions.user-input.outputs.dry-run}},
       "revision": "{{context.actions.user-input.outputs.revision}}"
     }
     ```
4. Click **Save**.
5. Click **Save Workflow**.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cortex.io/guides/operational-readiness/argocd-workflow.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
