# Workflows as code

Workflows allow you to run sequential actions and increase efficiency across development workflows. With Workflows as code, you can treat Workflows like other production asset; this feature allows you to manage Workflows through [GitOps](/configure/gitops.md). Each Workflow has its own YAML file.

When GitOps is enabled, changes made to Workflows will appear in [GitOps logs](/configure/gitops/gitops-logs.md).

## Managing Workflows in both GitOps and the Cortex UI

When you enable GitOps editing for Workflows, it only affects Workflows that are backed by a YAML file. You can still create and delete Workflows via the Cortex UI, and you can still edit a Workflow via the UI if it's not managed via GitOps.

### Step 1: Configuring GitOps editing for Workflows

Users with the `Edit Workflows` permission can configure GitOps editing in Cortex.

{% hint style="warning" %}
Enabling GitOps editing for Workflows only affects Workflows backed by a YAML file. You can still create and delete Workflows via the Cortex UI.
{% endhint %}

1. From the main sidebar, click your avatar in the bottom-left corner.
2. Click **Settings**.
3. From the **Settings** menu, locate the **Workspace** section, then click **GitOps**.
4. Select the **Workflows** tab.
5. Toggle on **Enable GitOps for Workflows editing**.
6. Optionally, under **Workflow GitOps repository allowlist**, specify which repositories to import Workflows from. See [configuring an allowlist](/configure/settings/gitops-settings.md#gitops-workflow-settings) for more information.
7. Select the the **Entities** tab.
8. Under **Processing**, toggle on **Cortex YAML processing**.

### Step 2: Creating a YAML file for the Workflow

**Prerequisites**

1. You must have the `Edit Workflows` permission.
2. An existing Workflow. If you haven't yet created a Workflow, refer to [Creating a Workflow](/streamline/workflows/create.md) and [Configuring Workflow settings](/streamline/workflows/configuring-workflow-settings.md).

Workflow YAML files must be stored in their own repository, separate from catalog entities, at the repository's root directly within `.cortex/workflows`.

The Workflow YAML uses the same data structure as [Cortex's Workflow API](/api/readme/workflows.md).

You can create a Workflow configuration from scratch, or you can start in the Cortex UI and export the Workflow to a YAML file.

**To export a Workflow to a YAML file in Cortex**:

1. From the main sidebar, select **Workflows**.
2. Locate the Workflow, then click the **Overflow menu** next to it.<br>

   <div align="left" data-with-frame="true"><figure><img src="/files/j7DJECnmRCQEoHuSGdW1" alt="The &#x27;Overflow menu&#x27; icon next to a Workflow." width="375"><figcaption></figcaption></figure></div>
3. Select **Edit Workflow**.
4. At the top of the Workflow, click the **Overflow menu.**
5. Select **Export Workflow YAML**.<br>

   <div align="left" data-with-frame="true"><figure><img src="/files/NQCHTg7iGD0OIiqHpor2" alt="The &#x27;Export Workflow YAML&#x27; option." width="375"><figcaption></figcaption></figure></div>
6. The YAML is downloaded and saved to your computer.
7. Add the YAML file to your Git repository within `.cortex/workflows`.

#### Example Workflow YAML

```yaml
name: New Workflow
tag: new-workflow
description: null
isDraft: true
filter:
  type: GLOBAL
runResponseTemplate: null
actions:
- name: List branches
  slug: list-branches
  schema:
    inputs:
      repo: myorg/backend-app
      per_page: 30
      protected: false
    integrationAlias: cortex
    actionIdentifier: github.listBranches
    type: ADVANCED_HTTP_REQUEST
  outgoingActions:
  - javascript
  isRootAction: true
- name: JavaScript
  slug: javascript
  schema:
    script: |-
      return fetch('https://api.ipify.org?format=json')
        .then(response => {
          return response.json();
        })
        .then(data => {
          return(data);
        });
    type: JAVASCRIPT
  outgoingActions:
  - http-request
  isRootAction: false
- name: HTTP request
  slug: http-request
  schema:
    headers:
      Content-Type: application/json
    httpMethod: POST
    payload: "{\"content\": {{{actions.javascript.outputs.return}}}}"
    url: https://echo.free.beeceptor.com
    type: HTTP_REQUEST
  outgoingActions: []
  isRootAction: false
runRestrictionPolicies: []
iconTag: null
```

## Editing GitOps-managed Workflows in Cortex

Users with the `Edit Workflows` permission can duplicate and edit Workflows in Cortex.

GitOps-managed Workflows are read-only in the Cortex UI. To modify one, create a duplicate, edit the copy, and use it as your primary Workflow going forward.

The UI displays a link to the Workflow's YAML file in your repository:

<div align="left" data-with-frame="true"><figure><img src="/files/B7afHsf26ch8PWEPiT3O" alt="The &#x27;Repo&#x27; icon in the upper-right corner of a Workflow." width="563"><figcaption></figcaption></figure></div>

1. From the main sidebar, select **Workflows**.
2. Locate the Workflow, then click the **Overflow menu** next to it.
3. Select **Duplicate Workflow**.
4. Click **Duplicate workflow**.\
   The Workflow is duplicated and a copy is added to the main Workflows page.
5. To edit the new Workflow, do one of the following:
   1. Click **Edit Workflow** in the toast notification that appears in the bottom-right corner.
   2. Locate the Workflow, click the **Overflow menu** next to it, then select **Edit Workflow**.
6. Make any necessary changes.
7. Save the Workflow.

A **Git icon** next to a Workflow's name indicates that it's managed via GitOps:

<div align="left" data-with-frame="true"><figure><img src="/files/SRsjuuJ1qYTnEVaaWFoe" alt="The &#x27;Git&#x27; icon next to a Workflow&#x27;s name." width="275"><figcaption></figcaption></figure></div>


---

# 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/streamline/workflows/workflows-as-code.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.
