name: 'Usage of setup-terraform GitHub Action'
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
Setup Terraform
Sets up Terraform CLI in your GitHub Actions workflow.
What is setup-terraform GitHub Action?
The hashicorp/setup-terraform
action is a JavaScript-based action designed to prepare your GitHub Actions workflow for running Terraform commands by:
- Downloading and Installing Terraform CLI: Automatically downloads a specified version of Terraform CLI and adds it to the PATH, making it readily available for use in the workflow.
- Configuring Terraform CLI: Optionally configures the Terraform CLI with a hostname and API token for HCP Terraform or Terraform Enterprise, facilitating authenticated operations.
- Installing a Wrapper Script: Installs a wrapper script that captures the STDOUT, STDERR, and exit code from Terraform commands and makes these available as outputs for subsequent steps. This feature is particularly useful for workflows that need to process or respond to the output from Terraform commands.
How to Specify a Terraform Version in GitHub Actions?
To install a specific version of Terraform, such as 1.1.7
, you can modify your GitHub Actions workflow file as follows:
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: "1.1.7"
Why Specify a Terraform Version?
- Consistency: Locking down a version prevents discrepancies between development, testing, and production environments, reducing “works on my machine” issues.
- Stability: Using a specific version helps avoid unexpected changes that might come with newer versions, ensuring that your infrastructure deployments are stable and predictable.
- Compliance: Certain projects may need to comply with regulatory standards that require version control of all software tools, including Terraform.
How to Disable Wrapper Script in setup-terraform Action?
If you decide that your workflow does not require the functionalities provided by the wrapper script, you can disable it. This might be necessary for workflows that need direct control over the Terraform CLI or when the additional outputs are not utilized.
steps:
- uses: hashicorp/setup-terraform@v3
with:
terraform_wrapper: false
How to Access Terraform Command Outputs in GitHub Actions?
The hashicorp/setup-terraform
action installs a wrapper script by default when you set up Terraform. This wrapper script makes it possible to capture and use the output from Terraform commands in subsequent steps of your workflow.
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 }}