# Registering a Scaffolder template

Cortex's Scaffolder allows you to build a fully-automated Workflow for spinning up a new project (including repositories and libraries) and adds functionality to existing services (CI scripts, infrastructure modules, etc). Using templates, you can automatically generate the boilerplate and plumbing for projects, ensuring they follow best practices from day 0.

Templating is powered by the open source [Cookiecutter](https://cookiecutter.io) project, an industry-standard tool to auto-generate boilerplate for new projects.

{% hint style="success" %}
Looking for a quick introduction to Scaffolder? Check out the [Cortex Academy "Intro to Scaffolder" course](https://academy.cortex.io/courses/introduction-to-scaffolder), available to all Cortex customers and POVs.
{% endhint %}

<figure><img src="https://826863033-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJW7pYRxS4dHS3Hv6wxve%2Fuploads%2Fgit-blob-30920b8b498df8838c3de13f42b82946a64b26d5%2Fscaffolder-page.jpg?alt=media" alt=""><figcaption></figcaption></figure>

This page describes how to get started using the Scaffolder as a block in a [Workflow](https://docs.cortex.io/streamline/workflows). For advanced usage information, see [Scaffolder advanced usage](https://docs.cortex.io/streamline/workflows/scaffolder/advanced-usage).

If you're using GitHub templates and want to convert them to Cookiecutter templates to use within a Cortex Workflow, see [Convert a repo to a Cookiecutter template](https://docs.cortex.io/streamline/workflows/scaffolder/convert-template).

## How to create a Scaffolder template

### Prerequisite

Before getting started:

* Make sure you have configured a Git integration with `write` access. The following integrations are available:
  * [GitHub](https://docs.cortex.io/ingesting-data-into-cortex/integrations/github)
    * Cortex's GitHub app is pre-configured with permissions for the Scaffolder.
  * [GitLab](https://docs.cortex.io/ingesting-data-into-cortex/integrations/gitlab)
    * Configure the GitLab access token with `write` permissions
  * [Bitbucket](https://docs.cortex.io/ingesting-data-into-cortex/integrations/bitbucket)
    * Configure the Bitbucket token with `Repositories - admin and delete` scopes.
  * [Azure DevOps](https://docs.cortex.io/ingesting-data-into-cortex/integrations/azuredevops)
    * Configure the Azure DevOps token with `Code read, write & manage` access.
* Make sure you have configured your username's [identity mappings](https://docs.cortex.io/configure/settings/managing-users/identity-mapping) for your Git provider.

### Step 1: Create a Cookiecutter template

To get started with the Scaffolder, you'll need a [Cookiecutter](https://cookiecutter.io) template. You can either use an existing template or create a new one.

{% tabs %}
{% tab title="Template" %}
To get started quickly, you can use an [existing open source template](https://www.cookiecutter.io/templates).

1. Find an open source template and clone the repo.
2. Open your `cookiecutter.json` file, located in the root of the template repository.

   * In the file, add a `version` key and enter the version. This allows Cortex to track which version of the template was used to generate a given project in the Catalog. For example:

   ```json
   {
   "project_name": "My Project",
   "database": ["postgres", "mysql"],
   "version": "0.1.2"
   }
   ```
3. Follow the [Cookiecutter instructions to generate the project](https://cookiecutter.readthedocs.io/en/stable/usage.html#keeping-your-cookiecutters-organized).

{% hint style="warning" %}
**Security restrictions for templates**

For security reasons, for templates used on Cortex Cloud, we disable Cookiecutter hooks, network access, and any syscalls. If you get an exception when using your template, ensure it's not attempting any of these restricted actions.

These features are enabled for templates when using Cortex Server (self-hosted).
{% endhint %}
{% endtab %}

{% tab title="New" %}

1. Create your own template. See Cookiecutter's [tutorial with examples in their documentation](https://cookiecutter.readthedocs.io/en/stable/tutorials/tutorial2.html).
   * Additional guides can be found in tech blogs, such as [this one describing how to create a template](https://raphael.codes/blog/create-your-own-cookiecutter-template/).
2. Follow [Cookiecutter's instructions to generate the project](https://cookiecutter.readthedocs.io/en/stable/usage.html#keeping-your-cookiecutters-organized).
   {% endtab %}
   {% endtabs %}

### Step 2: Add the template to Cortex

You must have the `Configure Scaffolder templates` permission to follow these steps.

1. In Cortex, navigate to **Workflows** from the main nav. In the upper right corner of the page, click **Register Scaffolder template**.

<div align="left"><figure><img src="https://826863033-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJW7pYRxS4dHS3Hv6wxve%2Fuploads%2Fgit-blob-de87ae836cae46309237288410810627c907bbba%2Fregister-scaffolder-template.jpg?alt=media" alt="In the upper right corner click &#x22;Register Scaffolder template.&#x22;" width="563"><figcaption></figcaption></figure></div>

2. Configure the template details:
   * **Provider**: Select which Git provider you are using.
   * **Name**: Enter a name for the template. This will be seen by developers when they create a service using the template.
   * **Description**: Add a description to help others understand the template's purpose.
   * **Tags**: Optionally, add tags to describe the template. For example, these could help developers understand whether the template is meant to be used for a specific service type, what language it uses, and more.
3. Under "Configuration," fill in the fields:
   * **Git provider configuration**: Enter the configuration, e.g., `default`
     * By default, the option **Use this configuration for all git operations** is **enabled**. When enabled, Cortex uses the selected configuration for **all** Git operations (e.g., fetching the template details, populating the form options, and creating the new repo or Pull Request). The form options will be filtered to only those organizations or repositories that the selected configuration has access to.
     * With this setting **disabled**, you can use a single Scaffolder template with multiple organizations/configurations. While the selected configuration must still have to access the template's repository, the remaining Git operations will be run dynamically based on the user's form selections. The Scaffolder form options will be populated using data from all configurations. Then, Cortex will automatically use a configuration with access to the selected organization (if scaffolding a new repository) or repository (if scaffolding a new PR) for all remaining Git operations.
   * **Git URL**: Enter the git URL where your template lives, e.g. `https://github.com/cortexapps/cookiecutter-employee`
   * **Git ref** (optional): Enter a git ref (branch name, tag, or commit SHA) to pin the template to a specific version of the repository. For example, entering v1.0.0 will always pull the template as it existed at that tag, regardless of subsequent changes to the default branch. If left blank, the template will pull from the default branch.&#x20;
   * **Subdirectory for Cookiecutter template** (optional): If your Cookiecutter template is not stored at the root of your repository, enter the subdirectory URL here.
   * **Configuration requirements**: Select one of the following:
     * **New service**. Cortex service descriptor will be generated for the service and it will be automatically added to relevant catalogs.
       * If you don't select this option, you will still have the option to create a service entity when running the Scaffolder.
     * **New pull request**. When enabled, the template can only be used to open pull requests against existing repositories.
     * **Both**. Enable both "New service" and "New pull request" configurations.
     * **Neither**. Disable both "New service" and "New pull request" configurations.
   * **Visibility**: When creating a new repository choose the default visibility of the repository. If you do not specify, the template defaults to setting a new repository as private.
     * The available options depend on the Git provider. The `Public` setting only works if the org containing the repository allows it. The `Internal` setting only works for GitHub Enterprise accounts.
     * When running a Workflow that contains a Scaffolder block, you can configure an override to change the repo visibility.
   * **Create cortex.yaml in git when creating a new service**: When enabled, a cortex.yaml will be created in git when the Scaffolder creates a new service.
     * If you don't enable this setting, the entity descriptor will be added via the API.
   * **Show README.md during Scaffolding**: When enabled, the project's README.md will be displayed when inputting the variables to use while rendering the template. If there is no README.md, nothing will be shown.
4. Click **Register Scaffolder template**.

If you see a "Failed to save template" error, ensure that you have provided a valid Git URL.

### Step 3: Create a Workflow with a Scaffolder step

After you have added a template to Cortex, you can start using the Scaffolder in a [Workflow](https://docs.cortex.io/streamline/workflows).

Follow the instructions in the [Workflows documentation](https://docs.cortex.io/streamline/workflows) for creating a Workflow and adding a Scaffolder block.

{% hint style="success" %}
If your Scaffolder template requires input variables, you can configure overrides in your [Workflow's Scaffolder block](https://docs.cortex.io/streamline/blocks#scaffolder). This allows you to use output from previous Workflow steps as the responses to the template. To streamline the process and avoid extra prompts, you can add a [User Input block](https://docs.cortex.io/streamline/blocks#user-input) before the Scaffolder to collect responses that will be used in the Scaffolder template.

See an end-to-end Scaffolder Workflow example in [Scaffolding a new service with custom data based on user input](https://app.gitbook.com/s/iu4HGhtyt7D4jcoIrCBE/production-readiness/standardize-new-service-creation).
{% endhint %}

## View Scaffolder templates

View all templates available to your organization under [**Workflows > Scaffolders**](https://app.getcortexapp.com/admin/workflows?activeTab=scaffolders).

The template's [Cortex ID (CID)](https://docs.cortex.io/ingesting-data-into-cortex/entities#cortex-id) can be found at the top of the page while editing the Scaffolder template:

<figure><img src="https://826863033-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJW7pYRxS4dHS3Hv6wxve%2Fuploads%2Fgit-blob-a432e09077ac802ec48709fadf82519613d99635%2Fscaffolder-cid.jpg?alt=media" alt="The CID is at the top of the Scaffolder template editing page."><figcaption></figcaption></figure>

## Troubleshooting and FAQ

#### When running a Scaffolder and choosing from my list of Azure DevOps projects, some projects aren't appearing as expected.

This can happen if you have enabled the option **Use this configuration for all git operations** when [configuring your template in Cortex](#step-2-add-the-template-to-cortex). Disable this setting for the ability to choose from all configurations.

#### **When running a Scaffolder Workflow that pushes template files to a newly created repository, the Workflow completes successfully but no files appear in the repository.**

This occurs when the branch name entered during the Scaffolder run does not match the default branch configured in your Git provider. For GitLab, Azure DevOps, and Bitbucket, the branch specified must match the repository's default branch (typically `main` or `master`). If it does not match, the Workflow will report successful completion but template files will not be pushed to the new repository.
