Skip to content
anchor

GitHub Action

Terraform Backend Action

v0.0.10 Latest version

Terraform Backend Action

anchor

Terraform Backend Action

Download or upload Terraform state file from/to GitHub repository

Installation

Copy and paste the following snippet into your .yml file.

              

- name: Terraform Backend Action

uses: Rindrics/[email protected]

Learn more about this action in Rindrics/terraform-backend-github

Choose a version

terraform-backend-github

This action enables to use GitHub repo as Terraform backend. For Terraform environment with local type backend configuration, this action downloads and uploads Terraform state file from a specified GitHub repository.

Inputs

Required

  • state_repo: Repository containing Terraform state file
  • token: GitHub Token which has read and write permission for state_repo
  • action: Action to perform, 'download' or 'upload'
  • sha: 'SHA of the state file to be overwritten by upload action (required for upload)'

Optional (defaults)

  • state_path_remote: Path to the Terraform state file in state_repo (terraform.tfstate)
  • state_path_local: Path to the Terraform state file in GitHub Actions environment (terraform.tfstate)
  • commit_message: Commit message for the upload action ('Update state file')
  • committer_name: Committer name for the upload action ('github-actions[bot]')
  • committer_email: Committer email for the upload action ('github-actions[bot]@users.noreply.github.com')

Outputs

  • sha: SHA of the original state file to use in 'upload' action

Example usage

jobs:
  terraform:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v3

      - name: Initialize Terraform workdir
        run: terraform init

      - name: Download Terraform state file
        id: terraform
        uses: your-username/terraform-backend-action@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          state_repo: your-repo/tfstates

      - name: Execute `terraform apply`
        run: |
          terraform plan -no-color -out .terraform/terraform.plan
          terraform apply -no-color .terraform/terraform.plan

      - name: Upload Terraform state file
        uses: your-username/terraform-backend-action@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          state_repo: your-repo/tfstates
          sha: ${{ steps.terraform.outputs.sha }}