Github Actions
There are three ways to run Terrakube from GitHub Actions. Pick the one that fits how you work:
Plans on pull requests with results as comments, and applies on merge with GitHub environment approval, using OIDC.
You already run terraform plan and apply in a workflow and want the run to execute in Terrakube.
Scripting: trigger runs, and manage organizations, workspaces, variables and more.
All three can use a Personal Access Token or team token stored as a secret. To avoid storing a long-lived token, use Federated OIDC Authentication.
The Terrakube GitHub Action
terrakube-io/terrakube-action-github runs a Terrakube template from a workflow, waits for it, and reports back to GitHub with a pull request comment and a job summary. The Terraform or OpenTofu run itself still happens on the Terrakube executor, with your workspace's variables, credentials and policies.
What it does
Authenticates to Terrakube with GitHub OIDC (no stored secret) or a personal access token.
Chooses workspaces from explicit folders, or discovers which folders changed in the pull request or push.
Runs a template against an exact commit and checks that Terrakube ran that same revision.
Gives feedback: a pull request comment with the structured plan, and a job summary.
Stages deployments. It can plan, wait for GitHub environment approval, then apply that same saved plan, so what was reviewed is what is applied.
Forwards per-run variables such as
TF_VAR_*secrets without changing the workspace.
Plan on pull requests
This workflow plans a workspace for every pull request, using GitHub OIDC:
TERRAKUBE_TEMPLATE is the name of a template, here a plan-only one. To use a token instead of OIDC, set TERRAKUBE_AUTH: token and TERRAKUBE_TOKEN: ${{ secrets.TERRAKUBE_PAT }}, and drop id-token: write.
A plan template does not make the run read-only: the executor still holds the workspace's cloud credentials. Keep pull-request credentials separate from production credentials, and only run templates and code you trust with them.
Authenticate with GitHub OIDC
In the target organization, create a team such as
github-actions-planwith access to the workspace, permission to read the template, and permission to create and view plan runs.A Terrakube superuser adds a federated credential with Team name
github-actions-plan, Issuer URLhttps://token.actions.githubusercontent.com, and Audienceapi://terrakube(the same value asTERRAKUBE_OIDC_AUDIENCE).Add exact claim conditions, for example
repository=my-org/infrastructureandevent_name=pull_request. Federated trust is instance-wide, so keep conditions narrow.Use a separate credential and team for deployments, restricted to the main branch and a protected environment (for example
sub=repo:my-org/infrastructure:environment:production).
Tokens are renewed automatically before they expire. See Federated Identity for how Terrakube maps claims to teams.
Choose which workspaces run
TERRAKUBE_ROOT
Runs that folder (or several, one per line) regardless of changes. Use . for the repository root. An existing workspace needs no terrakube.json.
TERRAKUBE_WORKSPACE or TERRAKUBE_WORKSPACE_ID
The workspace to use. Needs exactly one root.
Neither
The action finds folders containing a terrakube.json and, on pull requests and pushes, runs only those with changed files. Use fetch-depth: 0 on checkout.
The workspace's repository and folder must match the workflow; the action does not rewrite workspace settings. By default missing workspaces are not created. Set CREATE_WORKSPACE: 'true' to create them from a terrakube.json in each root:
terraform (the version) is required to create a workspace; iacType is terraform (default) or tofu. Existing workspaces keep their engine and version.
Plan, approve in GitHub, then apply
For deployments, plan the merged commit, let GitHub's environment protection approve it, then apply the same saved plan:
First create a dedicated template with a plan, an approval owned by the apply team, and a saved-plan apply:
The approval step holds the run until the GitHub-approved job releases it through the API, so there is no second human approval in Terrakube. Always name a team (without one, anyone who can read the run could approve it), keep the plan identity out of that team, and keep the template unchanged while plans are pending. See Require a saved plan.
Then use two jobs in one workflow, passing the plan's JOB_ID to the apply:
TERRAKUBE_OPERATION is run (default: run a template to completion), plan (stop at the approval gate, STATUS: planned), apply (release a saved plan by TERRAKUBE_JOB_ID) or discard. The staged operations need one explicit root, an existing workspace, a full commit SHA, and the same template and commit for the plan and the apply. Plan and apply should use different identities: map the apply team to a separate federated credential restricted to the main branch and the protected environment. A plan with no changes skips the apply (HAS_CHANGES is false).
Per-run variables
Forward Terraform variables for one run, for example secrets from GitHub, without storing them on the workspace:
List the names without the TF_VAR_ prefix; only those leave the runner. This needs Terrakube 2.34 (run-specific variables), one explicit root and workspace, and Manage workspace permission for the identity. Supply them on run or plan; apply reuses the saved inputs. Mark secret variables sensitive = true in Terraform.
Pull request feedback
COMMENT_MODE controls the comment on the pull request: summary (default: status, revision, link and the structured plan), full (adds step logs) or off. The same plan is always written to the GitHub job summary. The action keeps one comment per workspace up to date and leaves other people's comments alone. SHOW_OUTPUT still works but is deprecated.
Inputs and outputs
TERRAKUBE_ENDPOINT (required)
HTTPS URL of the Terrakube API.
TERRAKUBE_ORGANIZATION (required)
Organization name.
TERRAKUBE_TEMPLATE (required)
Template name.
TERRAKUBE_AUTH
token or oidc.
token
TERRAKUBE_TOKEN
Personal access or team token, for token authentication.
TERRAKUBE_OIDC_AUDIENCE
Audience of the federated credential, for oidc.
TERRAKUBE_OPERATION
run, plan, apply or discard.
run
TERRAKUBE_JOB_ID
Existing run, for apply and discard.
TERRAKUBE_ROOT
Folder(s) to run, one per line.
Change discovery
TERRAKUBE_WORKSPACE / TERRAKUBE_WORKSPACE_ID
Workspace name or id; needs one root.
terrakube.json, else the folder name
TERRAKUBE_BRANCH
Branch to run.
PR head branch or current ref
TERRAKUBE_COMMIT
Full commit SHA to run and verify.
PR head SHA or event SHA
TERRAKUBE_VARIABLE_NAMES
Variables to forward from TF_VAR_<name>.
CREATE_WORKSPACE
Create missing workspaces from terrakube.json.
false
TERRAKUBE_WORKSPACE_BRANCH
Default branch of newly created workspaces.
Repository default
APPROVAL_MODE
wait for approval, or fail at an approval pause.
wait
COMMENT_MODE
off, summary or full.
summary
TERRAKUBE_UI_URL
UI URL, for links.
TIMEOUT_SECONDS, REQUEST_TIMEOUT_SECONDS, POLL_INTERVAL_SECONDS
Run, request and polling limits.
3600, 30, 5
GITHUB_TOKEN
Token for pull request comments.
${{ github.token }}
IGNORE_SSL_ERROR
Skip TLS verification. Prefer NODE_EXTRA_CA_CERTS for a private CA.
false
JOB_ID, JOB_IDS
The submitted run id, or a JSON array of all ids.
STATUS
running, planned, completed, failed or skipped.
HAS_CHANGES
For plan: true when a plan is ready to apply, false for no changes.
JOB_URL, JOBS
A link to the run (with TERRAKUBE_UI_URL), and a JSON array of results.
Stopping the workflow does not cancel the run in Terrakube. Keep the job ids from the outputs before retrying. The action's README has the full reference.
Upgrading from 1.x
Not tied to a Terrakube version
Requires Terrakube 2.34.0+ and runner v2.327.1+
TERRAKUBE_TOKEN required
TERRAKUBE_AUTH chooses token or oidc; the token is optional
SHOW_OUTPUT
COMMENT_MODE (SHOW_OUTPUT is deprecated)
Creates a workspace for every folder with a terrakube.json
Does not create workspaces unless CREATE_WORKSPACE: 'true'
Runs a branch
Runs and verifies an exact commit
uses: AzBuilder/terrakube-action-github@main
uses: terrakube-io/terrakube-action-github@v2.0.0
Apply happens in the template
Optional staged plan then apply with GitHub approval
Update TERRAKUBE_ENDPOINT to an HTTPS URL, switch to the new inputs, and test on a non-production workspace first.
The Terraform or OpenTofu CLI
Terrakube works as a remote backend for the Terraform and OpenTofu CLI (the CLI-driven workflow), so a normal terraform plan or apply in a workflow can execute in Terrakube. Configure the cloud block as described there, then give the CLI a token:
This keeps state, variables, policy checks and run history in Terrakube while the workflow stays in GitHub. For OpenTofu use opentofu/setup-opentofu the same way. If the registry has a different hostname from the API, add the token for both hostnames. See CLI-driven Workflow.
The Terrakube CLI
The Terrakube CLI (terrakube) manages Terrakube itself and can trigger runs. Download a release binary (releases) and configure it with environment variables:
See job create and the other commands.
Other tools
Anything that can call HTTP can use the API directly, and the Terraform provider manages Terrakube resources as code.
Last updated
Was this helpful?