# Template change management

We recommend source-controlling your templates as you would other any code, and automating the creation of new versions in continuous integration and continuous delivery (CI/CD) pipelines.

These pipelines will require tokens for your deployment.
To cap token lifetime on creation, [configure the control plane to set a shorter max token lifetime](/docs/@main/reference/cli/server/#--max-token-lifetime).

## coderd Terraform Provider

The [coderd Terraform provider](https://registry.terraform.io/providers/coder/coderd/latest) can be used to push new template versions, either manually, or in CI/CD pipelines.
To run the provider in a CI/CD pipeline, and to prevent drift, you'll need to store the Terraform state [remotely](https://developer.hashicorp.com/terraform/language/backend).

```tf
terraform {
  required_providers {
    coderd = {
      source = "coder/coderd"
    }
  }
  backend "gcs" {
    bucket = "example-bucket"
    prefix = "terraform/state"
  }
}

provider "coderd" {
  // Can be populated from environment variables
  url   = "https://coder.example.com"
  token = "****"
}

// Get the commit SHA of the configuration's git repository
variable "TFC_CONFIGURATION_VERSION_GIT_COMMIT_SHA" {
  type = string
}

resource "coderd_template" "kubernetes" {
  name = "kubernetes"
  description = "Develop in Kubernetes!"
  versions = [{
    directory = ".coder/templates/kubernetes"
    active    = true
    # Version name is optional
    name = var.TFC_CONFIGURATION_VERSION_GIT_COMMIT_SHA
    tf_vars = [{
      name  = "namespace"
      value = "default4"
    }]
  }]
  /* ... Additional template configuration */
}
```

For an example, see how we push our development image [with GitHub actions](https://github.com/coder/coder/blob/main/.github/workflows/dogfood.yaml).

## Coder CLI

You can [install Coder](https://coder.com/downloads) CLI to automate pushing new template versions in CI/CD pipelines.
For GitHub Actions, see our [setup-coder](https://github.com/coder/setup-coder) action.

```console
# Install the Coder CLI
curl -fsSL https://coder.com/install.sh | sh
# curl -fsSL https://coder.com/install.sh | sh -s -- --version=0.x

# To create API tokens, use `coder tokens create`.
# Without `--lifetime`, the token gets the maximum token lifetime for your role.
# By default, that is about 100 years for most users and 7 days for Owners.
# Always pass `--lifetime` for CI tokens, for example `--lifetime 7d`.
# These variables are consumed by Coder
export CODER_URL=https://coder.example.com
export CODER_SESSION_TOKEN=*****

# Template details
export CODER_TEMPLATE_NAME=kubernetes
export CODER_TEMPLATE_DIR=.coder/templates/kubernetes
export CODER_TEMPLATE_VERSION=$(git rev-parse --short HEAD)

# Push the new template version to Coder
coder templates push --yes $CODER_TEMPLATE_NAME \
    --directory $CODER_TEMPLATE_DIR \
    --name=$CODER_TEMPLATE_VERSION # Version name is optional
```

<a id="testing-and-publishing-coder-templates-in-cicd"></a>

## Test and publish Coder templates in CI/CD

See our [testing templates](/docs/@main/templates/testing-templates/) tutorial for an example of how to test and publish Coder templates in a CI/CD pipeline.

### Next steps

* [Coder CLI Reference](/docs/@main/reference/cli/templates/)
* [Coderd Terraform Provider Reference](https://registry.terraform.io/providers/coder/coderd/latest/docs)
* [Coderd API Reference](/docs/@main/reference/)
