# Workflows as code

Workflows allow you to run sequential actions and increase efficiency across development workflows. With Workflows as code, you can treat your Workflows like other production assets; 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](#step-1-configure-gitops-editing-for-workflows), it only affects Workflows that are backed by a YAML file. You will still be able to create and delete Workflows via the Cortex UI, and you can still edit a Workflow via the UI if it is not being managed through GitOps.

### View GitOps-managed Workflows in Cortex

In the Cortex UI, you will see a Git badge appear next to any Workflows that are GitOps-managed:

<div align="left"><figure><img src="/files/SRsjuuJ1qYTnEVaaWFoe" alt="A Git badge appears next to GitOps-managed Workflows."><figcaption></figcaption></figure></div>

You can view the Workflow in the Cortex UI, but you cannot edit it[^1] in the UI. The Cortex UI will display a link to Workflow's YAML file in your repository:

<figure><img src="/files/B7afHsf26ch8PWEPiT3O" alt="Click Repo in the upper right corner of a Workflow to view its repository."><figcaption></figcaption></figure>

### Edit GitOps-managed Workflows in Cortex

You cannot edit a GitOps-managed Workflow in the Cortex UI, but you can duplicate the Workflow and make edits to the duplicate copy.

To duplicate a Workflow:

1. In the Workflows list, click the 3 dots icon in the row containing the Workflow.
2. Click **Duplicate workflow**.\\

   <figure><img src="/files/21XX7hrut8mnMlRu855n" alt="Next to a Workflow, click the 3 dots icon, then click &#x22;Duplicate workflow&#x22;."><figcaption></figcaption></figure>

## Create a Workflow via GitOps

### Step 1: Configure GitOps editing for Workflows

1. Navigate to **Settings > GitOps** then click [the **Workflows** tab](https://app.getcortexapp.com/admin/settings/gitops?activeGitopsTab=WORKFLOWS).

   <div align="left"><figure><img src="/files/lGslR0IliwYrgBil6AZh" alt="In Settings, go to GitOps, then click the Workflows tab."><figcaption></figcaption></figure></div>
2. Toggle **on** the setting **Enable GitOps for Workflows editing**.
   * Optionally, under "Workflow GitOps repository allowlist," you can specify which repositories to import Workflows from.
3. Click the [the **Entities** tab](https://app.getcortexapp.com/admin/settings/gitops?activeGitopsTab=ENTITIES).
4. Toggle **on** the setting **Cortex YAML processing**.

   <figure><img src="/files/oYPBIwbmt8NqmKbmMXXc" alt=""><figcaption></figcaption></figure>

Note that when you enable GitOps editing for Workflows, it [only affects Workflows that are backed by a YAML file](#managing-workflows-in-both-gitops-and-the-cortex-ui). You will still be able to create and delete Workflows via the Cortex UI.

You can also [configure an allowlist](/configure/settings/gitops-settings.md#gitops-workflow-settings) to determine which repositories you import Workflows from.

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

Workflow YAML files must be 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:

1. [Create a Workflow in Cortex](/streamline/workflows/create.md).
2. At the top of the Workflow, click the 3 dots menu, then click **Export Workflow YAML**.\\

   <div align="left"><figure><img src="/files/4Ztwi2R7XiGhGM8ThKcp" alt="" width="563"><figcaption></figcaption></figure></div>
3. Add the YAML file to your Git repository within `.cortex/workflows`.

See an example YAML file below.

#### 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
```

[^1]: Should this be 'them' since it refers to the plural "Workflow details"? It would be "it" if it was referring to the singular "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/streamline/workflows/create/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.
