> For the complete documentation index, see [llms.txt](https://docs.terrakube.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.terrakube.io/user-guide/ci-cd-integration/github-actions.md).

# Github Actions

Integrate Terrakube with GitHub Actions is easy and you can handle your workspace from GitHub.

{% hint style="info" %}
The examples on this page use a Terrakube Personal Access Token. To avoid storing a long-lived PAT, see [Federated OIDC Authentication](/user-guide/ci-cd-integration/federated-oidc-authentication.md).
{% endhint %}

The GIT repository will represent a Terrakube Organization and each folder inside the repository will be a new workspace.

There is an example available in the following [link](https://github.com/AzBuilder/terraform-sample-repository)

### Configuration

Add the following snippet to the script section of your github actions file:

#### Pull Request example

To run a terraform plan using Terrakube templates use the following example:

> File: .github/workflows/pull\_request.yml

```
name: Terrakube Plan

on:
  pull_request:
    types: [opened, synchronize, reopened]

jobs:
  build:
    runs-on: ubuntu-latest
    name: "Running Terrakube Plan"
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - uses: AzBuilder/terrakube-action-github@main
        with:
          TERRAKUBE_TOKEN:  ${{ secrets.TERRAKUBE_PAT }} 
          TERRAKUBE_TEMPLATE: "Terraform-Plan"
          TERRAKUBE_ENDPOINT: ${{ secrets.TERRAKUBE_ENDPOINT }}  
          TERRAKUBE_BRANCH: ${{ github.head_ref }}
          TERRAKUBE_ORGANIZATION: "terrakube_organization_name"
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SHOW_OUTPUT: true
```

> A new workspace will be created for each folder with the file "terrakube.json". For each PR only new folders or folders that has been updated will be evaluated inside Terrakube.

#### Push Main branch

To run a terraform apply using Terrakube templates use the following example:

> File: .github/workflows/push\_main.yml

```
name: Terrakube Apply

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    name: "Running Terrakube Apply"
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - uses: AzBuilder/terrakube-action-github@main
        with:
          TERRAKUBE_TOKEN:  ${{ secrets.TERRAKUBE_PAT }} 
          TERRAKUBE_TEMPLATE: "Terraform-Plan/Apply"
          TERRAKUBE_ENDPOINT: ${{ secrets.TERRAKUBE_GITPOD }}  
          TERRAKUBE_BRANCH: "main"
          TERRAKUBE_ORGANIZATION: "terrakube_organization_name"
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SHOW_OUTPUT: false
```

### Terrakube Variables

To define terrakube variables to connect to cloud providers it is recommended to use Global Variables inside the organization using the UI. Terraform variables could be define inside a terraform.tfvars inside each folder or you can define inside the Terrakube UI after the workspace creation.

### GitHub Action Inputs

| Variable                     | Usage                                                                                |
| ---------------------------- | ------------------------------------------------------------------------------------ |
| TERRAKUBE\_TOKEN (\*)        | Terrakube Personal Access Token                                                      |
| TERRAKUBE\_TEMPLATE (\*)     | Terrakube template name                                                              |
| TERRAKUBE\_ENDPOINT (\*)     | Terrakbue api endpoint                                                               |
| TERRAKUBE\_ORGANIZATION (\*) | Terrakbue organization                                                               |
| TERRAKUBE\_BRANCH (\*)       | Github Branch when running a job                                                     |
| TERRAKUBE\_SSH\_KEY\_NAME    | ssh key name define in the terrakube organization to connect to private repositories |
| GITHUB\_TOKEN (\*)           | Github Token                                                                         |
| SHOW\_OUTPUT (\*)            | Show terrakube logs inside PR comments                                               |

*(\*) = required variable.*

### Terraform Workspace Configuration

Create a file called "terrakube.json"; includes workspace name and the terraform version that will be used for the job execution.

```
{
    "workspace": "workspaceA"
    "terraform": "1.2.9"
}
```

### Build GitHub Action

To build the github action in your local machine use the following.

```
git clone https://github.com/AzBuilder/terrakube-action-github.git
yarn install
yarn build
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.terrakube.io/user-guide/ci-cd-integration/github-actions.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
