Sets up Terraform CLI in your GitHub Actions workflow.
Find a file
dependabot[bot] 071811a162
Some checks are pending
Continuous Integration / Check dist/ directory (push) Waiting to run
Continuous Integration / Test (push) Waiting to run
setup-terraform tests / Terraform Versions (push) Waiting to run
setup-terraform tests / Terraform Versions-1 (push) Waiting to run
setup-terraform tests / Terraform Versions-2 (push) Waiting to run
setup-terraform tests / Terraform Versions-3 (push) Waiting to run
setup-terraform tests / Terraform Versions-4 (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints-4 (push) Waiting to run
setup-terraform tests / Terraform Versions-5 (push) Waiting to run
setup-terraform tests / Terraform Versions No Wrapper (push) Waiting to run
setup-terraform tests / Terraform Versions No Wrapper-1 (push) Waiting to run
setup-terraform tests / Terraform Versions No Wrapper-2 (push) Waiting to run
setup-terraform tests / Terraform Versions No Wrapper-3 (push) Waiting to run
setup-terraform tests / Terraform Versions No Wrapper-4 (push) Waiting to run
setup-terraform tests / Terraform Versions No Wrapper-5 (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints-1 (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints-2 (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints-3 (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints-5 (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints-6 (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints-7 (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints-8 (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints No Wrapper (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints No Wrapper-1 (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints No Wrapper-2 (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints No Wrapper-3 (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints No Wrapper-4 (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints No Wrapper-5 (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints No Wrapper-6 (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints No Wrapper-7 (push) Waiting to run
setup-terraform tests / Terraform Versions Constraints No Wrapper-8 (push) Waiting to run
setup-terraform tests / HCP Terraform Credentials (push) Waiting to run
setup-terraform tests / HCP Terraform Credentials-1 (push) Waiting to run
setup-terraform tests / HCP Terraform Credentials-2 (push) Waiting to run
setup-terraform tests / Terraform Enterprise Credentials (push) Waiting to run
setup-terraform tests / Terraform Enterprise Credentials-1 (push) Waiting to run
setup-terraform tests / Terraform Enterprise Credentials-2 (push) Waiting to run
setup-terraform tests / Terraform No Credentials (push) Waiting to run
setup-terraform tests / Terraform No Credentials-1 (push) Waiting to run
setup-terraform tests / Terraform No Credentials-2 (push) Waiting to run
setup-terraform tests / Terraform Arguments (push) Waiting to run
setup-terraform tests / Terraform Arguments-1 (push) Waiting to run
setup-terraform tests / Terraform Arguments-2 (push) Waiting to run
setup-terraform tests / Terraform Arguments No Wrapper (push) Waiting to run
setup-terraform tests / Terraform Arguments No Wrapper-1 (push) Waiting to run
setup-terraform tests / Terraform Arguments No Wrapper-2 (push) Waiting to run
setup-terraform tests / Terraform Run Local (push) Waiting to run
setup-terraform tests / Terraform Run Local-1 (push) Waiting to run
setup-terraform tests / Terraform Run Local-2 (push) Waiting to run
setup-terraform tests / Terraform Run Local No Wrapper (push) Waiting to run
setup-terraform tests / Terraform Run Local No Wrapper-1 (push) Waiting to run
setup-terraform tests / Terraform Run Local No Wrapper-2 (push) Waiting to run
setup-terraform tests / Terraform STDOUT (push) Waiting to run
setup-terraform tests / Terraform STDOUT-1 (push) Waiting to run
setup-terraform tests / Terraform STDOUT-2 (push) Waiting to run
setup-terraform tests / Terraform STDOUT No Wrapper (push) Waiting to run
setup-terraform tests / Terraform STDOUT No Wrapper-1 (push) Waiting to run
setup-terraform tests / Terraform STDOUT No Wrapper-2 (push) Waiting to run
setup-terraform tests / Terraform Delayed Apply (push) Waiting to run
setup-terraform tests / Terraform Delayed Apply-1 (push) Waiting to run
setup-terraform tests / Terraform Delayed Apply-2 (push) Waiting to run
Bump the github-actions group with 2 updates (#517)
2025-12-11 13:31:17 -05:00
.changes Update changelog 2024-08-19 16:09:37 +00:00
.github Bump the github-actions group with 2 updates (#517) 2025-12-11 13:31:17 -05:00
dist Bump @actions/io from 1.1.3 to 2.0.0 (#513) 2025-11-04 10:27:01 +01:00
lib Automatically fallback to darwin/amd64 for Terraform versions before 1.0.2 (#409) 2024-04-23 10:01:38 -04:00
META.d Tidy META.d (#480) 2025-05-13 09:12:25 -04:00
test [COMPLIANCE] Add copyright and license headers (#327) 2023-06-05 11:20:41 -04:00
wrapper Bump jest from 29.7.0 to 30.0.3 (#493) 2025-06-30 23:34:03 -04:00
.changie.yaml Switch to Changie for changelog automation 2023-02-08 15:32:40 -05:00
.copywrite.hcl Add catalog metadata (META.d) (#479) 2025-05-13 08:19:52 -04:00
.gitignore Git-ignoring Jetbrains IDEs files 2022-05-19 15:54:08 +01:00
action.yml Replacing "Terraform Cloud" with "HCP Terraform" (#413) 2024-05-08 14:05:15 +01:00
CHANGELOG.md Update changelog 2024-08-19 16:09:37 +00:00
CODE_OF_CONDUCT.md Initial commit 2020-04-25 19:46:30 -04:00
index.js [COMPLIANCE] Add copyright and license headers (#327) 2023-06-05 11:20:41 -04:00
LICENSE [COMPLIANCE] Update MPL 2.0 LICENSE 2022-10-12 20:57:36 +00:00
package-lock.json Bump actions/checkout from 5.0.0 to 6.0.0 in the github-actions group (#515) 2025-11-24 15:56:54 +01:00
package.json Bump @actions/io from 1.1.3 to 2.0.0 (#513) 2025-11-04 10:27:01 +01:00
README.md add "-input=false" (#440) 2025-02-10 11:31:15 -05:00

setup-terraform

Continuous Integration Setup Terraform

The hashicorp/setup-terraform action is a JavaScript action that sets up Terraform CLI in your GitHub Actions workflow by:

  • Downloading a specific version of Terraform CLI and adding it to the PATH.
  • Configuring the Terraform CLI configuration file with a HCP Terraform/Terraform Enterprise hostname and API token.
  • Installing a wrapper script to wrap subsequent calls of the terraform binary and expose its STDOUT, STDERR, and exit code as outputs named stdout, stderr, and exitcode respectively. (This can be optionally skipped if subsequent steps in the same job do not need to access the results of Terraform commands.)

After you've used the action, subsequent steps in the same job can run arbitrary Terraform commands using the GitHub Actions run syntax. This allows most Terraform commands to work exactly like they do on your local command line.

Usage

This action can be run on ubuntu-latest, windows-latest, and macos-latest GitHub Actions runners. When running on windows-latest the shell should be set to Bash. When running on self-hosted GitHub Actions runners, NodeJS must be previously installed with the version specified in the action.yml.

The default configuration installs the latest version of Terraform CLI and installs the wrapper script to wrap subsequent calls to the terraform binary:

steps:
- uses: hashicorp/setup-terraform@v3

A specific version of Terraform CLI can be installed:

steps:
- uses: hashicorp/setup-terraform@v3
  with:
    terraform_version: "1.1.7"

Credentials for HCP Terraform (app.terraform.io) can be configured:

steps:
- uses: hashicorp/setup-terraform@v3
  with:
    cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}

Credentials for Terraform Enterprise (TFE) can be configured:

steps:
- uses: hashicorp/setup-terraform@v3
  with:
    cli_config_credentials_hostname: 'terraform.example.com'
    cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}

The wrapper script installation can be skipped by setting the terraform_wrapper variable to false:

steps:
- uses: hashicorp/setup-terraform@v3
  with:
    terraform_wrapper: false

Subsequent steps can access outputs when the wrapper script is installed:

steps:
- uses: hashicorp/setup-terraform@v3

- run: terraform init

- id: plan
  run: terraform plan -no-color

- run: echo ${{ steps.plan.outputs.stdout }}
- run: echo ${{ steps.plan.outputs.stderr }}
- run: echo ${{ steps.plan.outputs.exitcode }}

Outputs can be used in subsequent steps to comment on the pull request:

Notice: There's a limit to the number of characters inside a GitHub comment (65535).

Due to that limitation, you might end up with a failed workflow run even if the plan succeeded.

Another approach is to append your plan into the $GITHUB_STEP_SUMMARY environment variable which supports markdown.

defaults:
  run:
    working-directory: ${{ env.tf_actions_working_dir }}
permissions:
  pull-requests: write
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3

- name: Terraform fmt
  id: fmt
  run: terraform fmt -check
  continue-on-error: true

- name: Terraform Init
  id: init
  run: terraform init -input=false

- name: Terraform Validate
  id: validate
  run: terraform validate -no-color

- name: Terraform Plan
  id: plan
  run: terraform plan -no-color -input=false
  continue-on-error: true

- uses: actions/github-script@v7
  if: github.event_name == 'pull_request'
  env:
    PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
  with:
    github-token: ${{ secrets.GITHUB_TOKEN }}
    script: |
      const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
      #### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
      #### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
      <details><summary>Validation Output</summary>

      \`\`\`\n
      ${{ steps.validate.outputs.stdout }}
      \`\`\`

      </details>

      #### Terraform Plan 📖\`${{ steps.plan.outcome }}\`

      <details><summary>Show Plan</summary>

      \`\`\`\n
      ${process.env.PLAN}
      \`\`\`

      </details>

      *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.tf_actions_working_dir }}\`, Workflow: \`${{ github.workflow }}\`*`;

      github.rest.issues.createComment({
        issue_number: context.issue.number,
        owner: context.repo.owner,
        repo: context.repo.repo,
        body: output
      })

Instead of creating a new comment each time, you can also update an existing one:

defaults:
  run:
    working-directory: ${{ env.tf_actions_working_dir }}
permissions:
  pull-requests: write
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3

- name: Terraform fmt
  id: fmt
  run: terraform fmt -check
  continue-on-error: true

- name: Terraform Init
  id: init
  run: terraform init -input=false

- name: Terraform Validate
  id: validate
  run: terraform validate -no-color

- name: Terraform Plan
  id: plan
  run: terraform plan -no-color -input=false
  continue-on-error: true

- uses: actions/github-script@v7
  if: github.event_name == 'pull_request'
  env:
    PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
  with:
    github-token: ${{ secrets.GITHUB_TOKEN }}
    script: |
      // 1. Retrieve existing bot comments for the PR
      const { data: comments } = await github.rest.issues.listComments({
        owner: context.repo.owner,
        repo: context.repo.repo,
        issue_number: context.issue.number,
      })
      const botComment = comments.find(comment => {
        return comment.user.type === 'Bot' && comment.body.includes('Terraform Format and Style')
      })

      // 2. Prepare format of the comment
      const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
      #### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
      #### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
      <details><summary>Validation Output</summary>

      \`\`\`\n
      ${{ steps.validate.outputs.stdout }}
      \`\`\`

      </details>

      #### Terraform Plan 📖\`${{ steps.plan.outcome }}\`

      <details><summary>Show Plan</summary>

      \`\`\`\n
      ${process.env.PLAN}
      \`\`\`

      </details>

      *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.tf_actions_working_dir }}\`, Workflow: \`${{ github.workflow }}\`*`;

      // 3. If we have a comment, update it, otherwise create a new one
      if (botComment) {
        github.rest.issues.updateComment({
          owner: context.repo.owner,
          repo: context.repo.repo,
          comment_id: botComment.id,
          body: output
        })
      } else {
        github.rest.issues.createComment({
          issue_number: context.issue.number,
          owner: context.repo.owner,
          repo: context.repo.repo,
          body: output
        })
      }

Inputs

The action supports the following inputs:

  • cli_config_credentials_hostname - (optional) The hostname of a HCP Terraform/Terraform Enterprise instance to place within the credentials block of the Terraform CLI configuration file. Defaults to app.terraform.io.
  • cli_config_credentials_token - (optional) The API token for a HCP Terraform/Terraform Enterprise instance to place within the credentials block of the Terraform CLI configuration file.
  • terraform_version - (optional) The version of Terraform CLI to install. Instead of a full version string, you can also specify a constraint string (see Semver Ranges for available range specifications). Examples are: "<1.2.0", "~1.1.0", "1.1.7" (all three installing the latest available 1.1 version). Prerelease versions can be specified and a range will stay within the given tag such as beta or rc. If no version is given, it will default to latest.
  • terraform_wrapper - (optional) Whether to install a wrapper to wrap subsequent calls of the terraform binary and expose its STDOUT, STDERR, and exit code as outputs named stdout, stderr, and exitcode respectively. Defaults to true.

Outputs

This action does not configure any outputs directly. However, when you set the terraform_wrapper input to true, the following outputs are available for subsequent steps that call the terraform binary:

  • stdout - The STDOUT stream of the call to the terraform binary.
  • stderr - The STDERR stream of the call to the terraform binary.
  • exitcode - The exit code of the call to the terraform binary.

License

Mozilla Public License v2.0

Code of Conduct

Code of Conduct

Experimental Status

By using the software in this repository (the "Software"), you acknowledge that: (1) the Software is still in development, may change, and has not been released as a commercial product by HashiCorp and is not currently supported in any way by HashiCorp; (2) the Software is provided on an "as-is" basis, and may include bugs, errors, or other issues; (3) the Software is NOT INTENDED FOR PRODUCTION USE, use of the Software may result in unexpected results, loss of data, or other unexpected results, and HashiCorp disclaims any and all liability resulting from use of the Software; and (4) HashiCorp reserves all rights to make all decisions about the features, functionality and commercial release (or non-release) of the Software, at any time and without any obligation or liability whatsoever.

Contributing

License Headers

All source code files (excluding autogenerated files like package.json, prose, and files excluded in .copywrite.hcl) must have a license header at the top.

This can be autogenerated by installing the HashiCorp copywrite tool and running copywrite headers in the root of the repository.