2020-04-23 12:57:44 -04:00
# setup-terraform
2022-09-17 02:01:55 +10:00
[](https://github.com/hashicorp/setup-terraform/actions/workflows/continuous-integration.yml)
[](https://github.com/hashicorp/setup-terraform/actions/workflows/setup-terraform.yml)
2020-04-23 12:57:44 -04:00
2020-05-05 19:46:33 -04:00
The `hashicorp/setup-terraform` action is a JavaScript action that sets up Terraform CLI in your GitHub Actions workflow by:
2020-04-23 12:57:44 -04:00
- Downloading a specific version of Terraform CLI and adding it to the `PATH` .
2024-05-08 15:05:15 +02:00
- Configuring the [Terraform CLI configuration file ](https://www.terraform.io/docs/commands/cli-config.html ) with a HCP Terraform/Terraform Enterprise hostname and API token.
2020-05-05 19:46:33 -04:00
- 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.)
2022-10-12 23:20:02 +11:00
After you've used the action, subsequent steps in the same job can run arbitrary Terraform commands using [the GitHub Actions `run` syntax ](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun ). This allows most Terraform commands to work exactly like they do on your local command line.
2020-04-23 12:57:44 -04:00
## Usage
2023-11-29 21:25:11 +01:00
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` ](https://github.com/hashicorp/setup-terraform/blob/main/action.yml ).
2020-04-23 12:57:44 -04:00
2022-09-17 02:01:55 +10:00
The default configuration installs the latest version of Terraform CLI and installs the wrapper script to wrap subsequent calls to the `terraform` binary:
2020-04-23 12:57:44 -04:00
```yaml
steps:
2023-11-27 08:07:44 +01:00
- uses: hashicorp/setup-terraform@v3
2020-04-23 12:57:44 -04:00
```
2022-09-17 02:01:55 +10:00
A specific version of Terraform CLI can be installed:
2020-04-23 12:57:44 -04:00
```yaml
steps:
2023-11-27 08:07:44 +01:00
- uses: hashicorp/setup-terraform@v3
2020-04-23 12:57:44 -04:00
with:
2023-10-17 14:20:19 +03:00
terraform_version: "1.1.7"
2020-04-23 12:57:44 -04:00
```
2024-05-08 15:05:15 +02:00
Credentials for HCP Terraform ([app.terraform.io ](https://app.terraform.io/ )) can be configured:
2020-04-23 12:57:44 -04:00
```yaml
steps:
2023-11-27 08:07:44 +01:00
- uses: hashicorp/setup-terraform@v3
2020-04-23 12:57:44 -04:00
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
```
2022-04-08 14:25:40 +02:00
Credentials for Terraform Enterprise (TFE) can be configured:
2020-04-23 12:57:44 -04:00
```yaml
steps:
2023-11-27 08:07:44 +01:00
- uses: hashicorp/setup-terraform@v3
2020-04-23 12:57:44 -04:00
with:
cli_config_credentials_hostname: 'terraform.example.com'
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
```
2022-04-08 14:25:40 +02:00
The wrapper script installation can be skipped by setting the `terraform_wrapper` variable to `false` :
2020-04-23 12:57:44 -04:00
```yaml
steps:
2023-11-27 08:07:44 +01:00
- uses: hashicorp/setup-terraform@v3
2020-04-23 12:57:44 -04:00
with:
terraform_wrapper: false
```
2022-09-17 02:01:55 +10:00
Subsequent steps can access outputs when the wrapper script is installed:
2020-04-23 12:57:44 -04:00
```yaml
steps:
2023-11-27 08:07:44 +01:00
- uses: hashicorp/setup-terraform@v3
2020-04-23 12:57:44 -04:00
- 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 }}
```
2020-05-05 19:46:33 -04:00
Outputs can be used in subsequent steps to comment on the pull request:
2020-04-23 12:57:44 -04:00
2022-09-16 18:03:51 +02:00
> **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.
>
2023-05-03 15:13:24 -04:00
> Another approach is to append your plan into the $GITHUB_STEP_SUMMARY environment variable which supports markdown.
2022-09-16 18:03:51 +02:00
2020-04-23 12:57:44 -04:00
```yaml
2020-05-21 10:36:02 -04:00
defaults:
run:
working-directory: ${{ env.tf_actions_working_dir }}
2022-10-12 14:49:47 +03:00
permissions:
pull-requests: write
2020-04-23 12:57:44 -04:00
steps:
2024-01-29 16:35:07 +09:00
- uses: actions/checkout@v4
2023-11-27 08:07:44 +01:00
- uses: hashicorp/setup-terraform@v3
2020-04-23 12:57:44 -04:00
2020-05-21 10:36:02 -04:00
- name: Terraform fmt
id: fmt
2020-09-09 02:50:49 +09:00
run: terraform fmt -check
2020-05-21 10:36:02 -04:00
continue-on-error: true
2020-04-23 12:57:44 -04:00
2020-05-21 10:36:02 -04:00
- name: Terraform Init
id: init
2025-02-10 16:31:15 +00:00
run: terraform init -input=false
2020-05-21 10:36:02 -04:00
- name: Terraform Validate
id: validate
run: terraform validate -no-color
- name: Terraform Plan
id: plan
2025-02-10 16:31:15 +00:00
run: terraform plan -no-color -input=false
2020-05-21 10:36:02 -04:00
continue-on-error: true
2020-04-23 12:57:44 -04:00
2024-04-30 09:30:45 -04:00
- uses: actions/github-script@v7
2020-04-23 12:57:44 -04:00
if: github.event_name == 'pull_request'
env:
2020-05-21 10:36:02 -04:00
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
2020-04-23 12:57:44 -04:00
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
2020-05-21 10:36:02 -04:00
const output = `#### Terraform Format and Style 🖌\` ${{ steps.fmt.outcome }}\`
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
2022-03-07 11:04:19 -05:00
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
< details > < summary > Validation Output< / summary >
\`\`\`\n
${{ steps.validate.outputs.stdout }}
\`\`\`
< / details >
2020-05-21 10:36:02 -04:00
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
2022-09-17 02:01:55 +10:00
2020-05-21 10:36:02 -04:00
< details > < summary > Show Plan< / summary >
2022-09-17 02:01:55 +10:00
2021-05-13 18:20:26 -04:00
\`\`\`\n
${process.env.PLAN}
\`\`\`
2022-09-17 02:01:55 +10:00
2020-05-21 10:36:02 -04:00
< / details >
2022-09-17 02:01:55 +10:00
2020-05-21 10:36:02 -04:00
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.tf_actions_working_dir }}\`, Workflow: \`${{ github.workflow }}\`* `;
2022-09-17 02:01:55 +10:00
2022-03-04 09:36:33 +09:00
github.rest.issues.createComment({
2020-04-23 12:57:44 -04:00
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
2020-05-15 13:27:42 -04:00
body: output
2020-04-23 12:57:44 -04:00
})
```
2022-04-08 20:24:17 +08:00
Instead of creating a new comment each time, you can also update an existing one:
```yaml
defaults:
run:
working-directory: ${{ env.tf_actions_working_dir }}
2022-10-12 14:49:47 +03:00
permissions:
pull-requests: write
2022-04-08 20:24:17 +08:00
steps:
2024-01-29 16:35:07 +09:00
- uses: actions/checkout@v4
2023-11-27 08:07:44 +01:00
- uses: hashicorp/setup-terraform@v3
2022-04-08 20:24:17 +08:00
- name: Terraform fmt
id: fmt
run: terraform fmt -check
continue-on-error: true
- name: Terraform Init
id: init
2025-02-10 16:31:15 +00:00
run: terraform init -input=false
2022-04-08 20:24:17 +08:00
- name: Terraform Validate
id: validate
run: terraform validate -no-color
- name: Terraform Plan
id: plan
2025-02-10 16:31:15 +00:00
run: terraform plan -no-color -input=false
2022-04-08 20:24:17 +08:00
continue-on-error: true
2024-04-30 09:30:45 -04:00
- uses: actions/github-script@v7
2022-04-08 20:24:17 +08:00
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 }}\`
2022-09-17 02:01:55 +10:00
2022-04-08 20:24:17 +08:00
< details > < summary > Show Plan< / summary >
2022-09-17 02:01:55 +10:00
2022-04-08 20:24:17 +08:00
\`\`\`\n
${process.env.PLAN}
\`\`\`
2022-09-17 02:01:55 +10:00
2022-04-08 20:24:17 +08:00
< / details >
2022-09-17 02:01:55 +10:00
2022-04-08 20:24:17 +08:00
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.tf_actions_working_dir }}\`, Workflow: \`${{ github.workflow }}\`* `;
2022-09-17 02:01:55 +10:00
2022-04-08 20:24:17 +08:00
// 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
})
}
```
2020-04-23 12:57:44 -04:00
## Inputs
2020-09-08 16:18:09 +02:00
The action supports the following inputs:
2020-04-23 12:57:44 -04:00
2024-05-08 15:05:15 +02:00
- `cli_config_credentials_hostname` - (optional) The hostname of a HCP Terraform/Terraform Enterprise instance to
2020-09-08 16:18:09 +02:00
place within the credentials block of the Terraform CLI configuration file. Defaults to `app.terraform.io` .
2024-05-08 15:05:15 +02:00
- `cli_config_credentials_token` - (optional) The API token for a HCP Terraform/Terraform Enterprise instance to
2020-09-08 16:18:09 +02:00
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 ](https://www.npmjs.com/package/semver#ranges )
2023-10-17 14:20:19 +03:00
for available range specifications). Examples are: `"<1.2.0"` , `"~1.1.0"` , `"1.1.7"` (all three installing
2022-04-08 14:25:40 +02:00
the latest available `1.1` version). Prerelease versions can be specified and a range will stay within the
2020-12-08 11:26:01 -08:00
given tag such as `beta` or `rc` . If no version is given, it will default to `latest` .
2022-09-17 02:01:55 +10:00
- `terraform_wrapper` - (optional) Whether to install a wrapper to wrap subsequent calls of
2020-09-08 16:18:09 +02:00
the `terraform` binary and expose its STDOUT, STDERR, and exit code as outputs
named `stdout` , `stderr` , and `exitcode` respectively. Defaults to `true` .
2020-04-23 12:57:44 -04:00
## Outputs
2020-09-08 16:18:09 +02:00
This action does not configure any outputs directly. However, when you set the `terraform_wrapper` input
2022-09-17 02:01:55 +10:00
to `true` , the following outputs are available for subsequent steps that call the `terraform` binary:
2020-04-23 12:57:44 -04:00
2020-05-15 13:27:42 -04:00
- `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.
2020-04-23 12:57:44 -04:00
## License
2022-10-12 23:20:02 +11:00
[Mozilla Public License v2.0 ](LICENSE )
2020-04-23 12:57:44 -04:00
## Code of Conduct
2022-10-12 23:20:02 +11:00
[Code of Conduct ](CODE_OF_CONDUCT.md )
2020-10-27 15:33:24 -04:00
## 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.
2023-06-05 11:20:41 -04:00
## Contributing
### License Headers
All source code files (excluding autogenerated files like `package.json` , prose, and files excluded in [.copywrite.hcl ](.copywrite.hcl )) must have a license header at the top.
This can be autogenerated by installing the HashiCorp [`copywrite` ](https://github.com/hashicorp/copywrite#getting-started ) tool and running `copywrite headers` in the root of the repository.