# Automate new dev environment

This example automates the creation of a GitHub repository, infrastructure provisioning with Terraform, and generates a PR for setting up an RDS instance.

### Step 1: Register the Scaffolder templates

This Workflow uses two Scaffolder templates:

* [Hello Org Template](https://github.com/cortex-workshops/hello-org-template)
* [Terraform RDS Template](https://github.com/cortex-workshops/terraform-rds-template)&#x20;

Follow the instructions to [register the Scaffolder templates in Cortex](/streamline/workflows/scaffolder.md).&#x20;

### Step 2: Start creating the Workflow

Follow the steps in the documentation to [create a Workflow](/streamline/workflows/create.md) and [configure its basic settings](/streamline/workflows/create.md#step-2-configure-your-workflow-settings).

### Step 3: Add blocks to the Workflow

The instructions on this page describe how to create this Workflow in the Cortex UI, but it is also possible to copy the Workflow YAML and add it to your workspace via the [Cortex CLI](https://pypi.org/project/cortexapps-cli/). This allows you to quickly set up the example configuration then iterate on it for your own use case. Expand the tile below to learn more.

<details>

<summary>Workflow YAML instructions</summary>

To upload the Workflow example YAML into your workspace:

1. Save the Workflow example YAML file below:

```yaml
name: "Dev Onboarding: Environment Setup"
tag: dev-onboarding-env-setup
description: "This workflow automates the creation of a GitHub repository, infrastructure\
  \ provisioning with Terraform, and generates a PR for setting up an RDS instance."
isDraft: false
filter:
  type: GLOBAL
runResponseTemplate: null
failedRunResponseTemplate: null
restrictActionCompletionToRunnerUser: false
actions:
- name: Transform User Info
  slug: transform-user-info
  schema:
    expression: .context.initiatedBy.name + "-repo"
    type: JQ
  outgoingActions:
  - scaffold-hello-org
  isRootAction: true
- name: Scaffold Hello Org Repo
  slug: scaffold-hello-org
  schema:
    scaffolderTemplateId: st363f187d3e1a826e
    createNewRepository: true
    createService: null
    inputOverrides:
    - inputKey: repo_name
      outputVariable: actions.transform-user-info.outputs.result
      editable: false
      type: VALUE
    type: SCAFFOLDER
  outgoingActions:
  - trigger-workflow
  isRootAction: false
- name: Terraform Infrastructure Provisioning
  slug: trigger-workflow
  schema:
    inputs:
      ref: main
      repo: cortex-workshops/terraform
      inputs: "{\"user\": \"{{context.initiatedBy.name}}\"}"
      workflow_id: .github/workflows/main.yaml
    integrationAlias: cortex
    actionIdentifier: github.createWorkflowDispatchEvent
    type: ADVANCED_HTTP_REQUEST
  outgoingActions:
  - scaffold-rds-terraform
  isRootAction: false
- name: Open PR with RDS terraform
  slug: scaffold-rds-terraform
  schema:
    scaffolderTemplateId: st363f198d56473124
    createNewRepository: false
    createService: null
    inputOverrides:
    - inputKey: publisherRepoFullName
      outputVariable: actions.scaffold-hello-org.outputs.response.gitURL
      editable: false
      type: VALUE
    type: SCAFFOLDER
  outgoingActions:
  - notify-developer
  isRootAction: false
- name: Notify Developer
  slug: notify-developer
  schema:
    channel: all-es-testing
    message: "Hi, {{context.initiatedBy.name}}! Your new test environment is ready\
      \ to go!"
    type: SLACK
  outgoingActions: []
  isRootAction: false
runRestrictionPolicies: []
iconTag: Keyboard
variables: []
```

2. Use the [Cortex CLI](https://pypi.org/project/cortexapps-cli/) to run this command, using the path to your Workflow YAML file:\
   `cortex workflows create -f <path-to-your-workflow.yaml>`

</details>

Expand the tiles below to learn about each block in this Workflow and how to configure them in the Cortex UI:

<details>

<summary>Data transformation</summary>

Create a data transformation to take the Workflow initiator's name and append `-repo` to it:

1. Click **+** in the center of the page. In the block library modal, choose **Data transformation**.
2. In the block configuration side panel, enter a name and unique slug for this block.
   1. In this example, we use the name `Transform user info` and the slug `transform-user-info`.
3. Add a jq expression to take the initiator's name and add `-repo` to it:

```jq
.context.initiatedBy.name + "-repo"
```

4. At the bottom of the side panel, click **Save**.

</details>

<details>

<summary>Scaffolder</summary>

This block creates a new repository in your connected Git integration, using templating to apply the output of the previous block to the Scaffolder configuration.

1. Click **+** in the center of the page. In the block library modal, select the **Scaffolder** block.
2. In the side panel, enter a name and unique slug for the block.
   1. In this example, we use the name `Scaffold Hello Org Repo` and the slug `scaffold-hello-org`.
3. Under **Scaffolder template**, select your template. In this example, we use `Hello Org Template`.
4. Select the option to **Create a new repo**.
5. Add override variables. The **Variable** dropdown pulls in all the variables that are able to be overridden in the Git provider and in the Scaffolder template. In this example, we apply the following override:
   * **repo\_name**: `actions.transform-user-info.outputs.result`
     * This overrides the Repo Name field with the output of the "Transform user info" block earlier in the Workflow.
6. At the bottom of the side panel, click **Save**.

</details>

<details>

<summary>Trigger GitHub workflow</summary>

This block triggers Terraform infrastructure in GitHub.

1. Click **+** in the center of the page. In the block library modal, select the **GitHub > Trigger workflow** block.
2. In the side panel, enter a name and unique slug for the block.
   * In this example, we use the name `Terraform Infrastructure Provisioning` and the slug `trigger-workflow`.
3. Configure the block:
   * **Repository**: Enter your repository name.
   * **Ref**: Enter the Git reference to trigger the workflow on, e.g. `main`.
   * **Workflow ID or file name**: Enter the ID of the GitHub workflow or the file name of the workflow file.&#x20;
     * In our example, we added `main.yaml`. Note that this should be just the file name and not the path to the file.
   * **Inputs**: Optionally enter key-value pairs to pass to the workflow.&#x20;
     * In our example, we added: `{"user": "{{context.initiatedBy.name}}"}`
4. At the bottom of the side panel, click **Save**.

</details>

<details>

<summary>Scaffolder</summary>

This block opens a Pull Request to set up an RDS instance, using templating to apply the output of the previous block to the Scaffolder configuration.

1. Click **+** in the center of the page. In the block library modal, select the **Scaffolder** block.
2. In the side panel, enter a name and unique slug for the block.
   1. In this example, we use the name `Open PR with RDS terraform` and the slug `scaffold-rds-terraform`.
3. Under **Scaffolder template**, select your template. In this example, we use `Terraform RDS Template`.
4. Select the option to **Open a pull request**.
5. Add override variables. The **Variable** dropdown pulls in all the variables that are able to be overridden in the Git provider and in the Scaffolder template. In this example, we apply the following override:
   * **publisherRepoFullName**: `actions.scaffold-hello-org.outputs.response.gitURL`
     * This overrides the Repo Name field with the output of the "Scaffold Hello Org Repo" block earlier in the Workflow.
6. At the bottom of the side panel, click **Save**.

</details>

<details>

<summary>Slack message</summary>

In this step, we send a templated message via Slack informing the user that their test environment is ready.

1. Click **+** in the center of the page. In the block library modal, select the **Slack** block.
2. In the side panel, enter a name and unique slug for the block.&#x20;
   * In this example we use the name `Notify developer` and the slug `notify-developer`.
3. Select a Slack channel name.
4. Enter text for the message that will be sent via Slack. In our example, we use the following:

```
Hi, {{context.initiatedBy.name}}! Your new test environment is ready to go!
```

5. Save the block.

</details>

### Step 4: Run the Workflow

When you run the Workflow, the following events happen:

* The Data transformation block runs, which produces a repository name for the user based on your specified naming convention.
* The Scaffolder runs, creating a new repository. The repo's name is based on the output of the previous block.
* The GitHub "Trigger a workflow" block runs, which provisions Terraform infrastructure.
* The second Scaffolder block runs, which opens a Pull Request to set up an RDS instance.
* The Slack block runs, which sends a Slack message to the user to let them know that their test environment is ready.


---

# 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/dev-onboarding/env-setup-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.
