{"id":13483470,"url":"https://github.com/hashicorp/setup-terraform","last_synced_at":"2025-05-13T20:22:12.412Z","repository":{"id":37382623,"uuid":"258271226","full_name":"hashicorp/setup-terraform","owner":"hashicorp","description":"Sets up Terraform CLI in your GitHub Actions workflow.","archived":false,"fork":false,"pushed_at":"2025-05-12T22:18:01.000Z","size":5445,"stargazers_count":1468,"open_issues_count":61,"forks_count":257,"subscribers_count":20,"default_branch":"main","last_synced_at":"2025-05-12T23:26:13.841Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://developer.hashicorp.com/terraform/tutorials/automation/github-actions","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hashicorp.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-04-23T16:53:07.000Z","updated_at":"2025-05-12T12:29:41.000Z","dependencies_parsed_at":"2024-03-05T07:39:27.326Z","dependency_job_id":"878e7b44-d94e-4ee1-b30d-7bee6f74b421","html_url":"https://github.com/hashicorp/setup-terraform","commit_stats":{"total_commits":232,"total_committers":43,"mean_commits":5.395348837209302,"dds":0.603448275862069,"last_synced_commit":"4fab0119030f0b41039f372591efdbf7dbcd8fd8"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fsetup-terraform","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fsetup-terraform/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fsetup-terraform/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fsetup-terraform/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hashicorp","download_url":"https://codeload.github.com/hashicorp/setup-terraform/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253932835,"owners_count":21986461,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T17:01:11.577Z","updated_at":"2025-05-13T20:22:12.387Z","avatar_url":"https://github.com/hashicorp.png","language":"JavaScript","readme":"# setup-terraform\n\n[![Continuous Integration](https://github.com/hashicorp/setup-terraform/actions/workflows/continuous-integration.yml/badge.svg)](https://github.com/hashicorp/setup-terraform/actions/workflows/continuous-integration.yml)\n[![Setup Terraform](https://github.com/hashicorp/setup-terraform/actions/workflows/setup-terraform.yml/badge.svg)](https://github.com/hashicorp/setup-terraform/actions/workflows/setup-terraform.yml)\n\nThe `hashicorp/setup-terraform` action is a JavaScript action that sets up Terraform CLI in your GitHub Actions workflow by:\n\n- Downloading a specific version of Terraform CLI and adding it to the `PATH`.\n- 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.\n- 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.)\n\nAfter 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.\n\n## Usage\n\nThis 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).\n\nThe default configuration installs the latest version of Terraform CLI and installs the wrapper script to wrap subsequent calls to the `terraform` binary:\n\n```yaml\nsteps:\n- uses: hashicorp/setup-terraform@v3\n```\n\nA specific version of Terraform CLI can be installed:\n\n```yaml\nsteps:\n- uses: hashicorp/setup-terraform@v3\n  with:\n    terraform_version: \"1.1.7\"\n```\n\nCredentials for HCP Terraform ([app.terraform.io](https://app.terraform.io/)) can be configured:\n\n```yaml\nsteps:\n- uses: hashicorp/setup-terraform@v3\n  with:\n    cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}\n```\n\nCredentials for Terraform Enterprise (TFE) can be configured:\n\n```yaml\nsteps:\n- uses: hashicorp/setup-terraform@v3\n  with:\n    cli_config_credentials_hostname: 'terraform.example.com'\n    cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}\n```\n\nThe wrapper script installation can be skipped by setting the `terraform_wrapper` variable to `false`:\n\n```yaml\nsteps:\n- uses: hashicorp/setup-terraform@v3\n  with:\n    terraform_wrapper: false\n```\n\nSubsequent steps can access outputs when the wrapper script is installed:\n\n```yaml\nsteps:\n- uses: hashicorp/setup-terraform@v3\n\n- run: terraform init\n\n- id: plan\n  run: terraform plan -no-color\n\n- run: echo ${{ steps.plan.outputs.stdout }}\n- run: echo ${{ steps.plan.outputs.stderr }}\n- run: echo ${{ steps.plan.outputs.exitcode }}\n```\n\nOutputs can be used in subsequent steps to comment on the pull request:\n\n\u003e **Notice:** There's a limit to the number of characters inside a GitHub comment (65535).\n\u003e\n\u003e Due to that limitation, you might end up with a failed workflow run even if the plan succeeded.\n\u003e\n\u003e Another approach is to append your plan into the $GITHUB_STEP_SUMMARY environment variable which supports markdown.\n\n```yaml\ndefaults:\n  run:\n    working-directory: ${{ env.tf_actions_working_dir }}\npermissions:\n  pull-requests: write\nsteps:\n- uses: actions/checkout@v4\n- uses: hashicorp/setup-terraform@v3\n\n- name: Terraform fmt\n  id: fmt\n  run: terraform fmt -check\n  continue-on-error: true\n\n- name: Terraform Init\n  id: init\n  run: terraform init -input=false\n\n- name: Terraform Validate\n  id: validate\n  run: terraform validate -no-color\n\n- name: Terraform Plan\n  id: plan\n  run: terraform plan -no-color -input=false\n  continue-on-error: true\n\n- uses: actions/github-script@v7\n  if: github.event_name == 'pull_request'\n  env:\n    PLAN: \"terraform\\n${{ steps.plan.outputs.stdout }}\"\n  with:\n    github-token: ${{ secrets.GITHUB_TOKEN }}\n    script: |\n      const output = `#### Terraform Format and Style 🖌\\`${{ steps.fmt.outcome }}\\`\n      #### Terraform Initialization ⚙️\\`${{ steps.init.outcome }}\\`\n      #### Terraform Validation 🤖\\`${{ steps.validate.outcome }}\\`\n      \u003cdetails\u003e\u003csummary\u003eValidation Output\u003c/summary\u003e\n\n      \\`\\`\\`\\n\n      ${{ steps.validate.outputs.stdout }}\n      \\`\\`\\`\n\n      \u003c/details\u003e\n\n      #### Terraform Plan 📖\\`${{ steps.plan.outcome }}\\`\n\n      \u003cdetails\u003e\u003csummary\u003eShow Plan\u003c/summary\u003e\n\n      \\`\\`\\`\\n\n      ${process.env.PLAN}\n      \\`\\`\\`\n\n      \u003c/details\u003e\n\n      *Pusher: @${{ github.actor }}, Action: \\`${{ github.event_name }}\\`, Working Directory: \\`${{ env.tf_actions_working_dir }}\\`, Workflow: \\`${{ github.workflow }}\\`*`;\n\n      github.rest.issues.createComment({\n        issue_number: context.issue.number,\n        owner: context.repo.owner,\n        repo: context.repo.repo,\n        body: output\n      })\n```\n\nInstead of creating a new comment each time, you can also update an existing one:\n\n```yaml\ndefaults:\n  run:\n    working-directory: ${{ env.tf_actions_working_dir }}\npermissions:\n  pull-requests: write\nsteps:\n- uses: actions/checkout@v4\n- uses: hashicorp/setup-terraform@v3\n\n- name: Terraform fmt\n  id: fmt\n  run: terraform fmt -check\n  continue-on-error: true\n\n- name: Terraform Init\n  id: init\n  run: terraform init -input=false\n\n- name: Terraform Validate\n  id: validate\n  run: terraform validate -no-color\n\n- name: Terraform Plan\n  id: plan\n  run: terraform plan -no-color -input=false\n  continue-on-error: true\n\n- uses: actions/github-script@v7\n  if: github.event_name == 'pull_request'\n  env:\n    PLAN: \"terraform\\n${{ steps.plan.outputs.stdout }}\"\n  with:\n    github-token: ${{ secrets.GITHUB_TOKEN }}\n    script: |\n      // 1. Retrieve existing bot comments for the PR\n      const { data: comments } = await github.rest.issues.listComments({\n        owner: context.repo.owner,\n        repo: context.repo.repo,\n        issue_number: context.issue.number,\n      })\n      const botComment = comments.find(comment =\u003e {\n        return comment.user.type === 'Bot' \u0026\u0026 comment.body.includes('Terraform Format and Style')\n      })\n\n      // 2. Prepare format of the comment\n      const output = `#### Terraform Format and Style 🖌\\`${{ steps.fmt.outcome }}\\`\n      #### Terraform Initialization ⚙️\\`${{ steps.init.outcome }}\\`\n      #### Terraform Validation 🤖\\`${{ steps.validate.outcome }}\\`\n      \u003cdetails\u003e\u003csummary\u003eValidation Output\u003c/summary\u003e\n\n      \\`\\`\\`\\n\n      ${{ steps.validate.outputs.stdout }}\n      \\`\\`\\`\n\n      \u003c/details\u003e\n\n      #### Terraform Plan 📖\\`${{ steps.plan.outcome }}\\`\n\n      \u003cdetails\u003e\u003csummary\u003eShow Plan\u003c/summary\u003e\n\n      \\`\\`\\`\\n\n      ${process.env.PLAN}\n      \\`\\`\\`\n\n      \u003c/details\u003e\n\n      *Pusher: @${{ github.actor }}, Action: \\`${{ github.event_name }}\\`, Working Directory: \\`${{ env.tf_actions_working_dir }}\\`, Workflow: \\`${{ github.workflow }}\\`*`;\n\n      // 3. If we have a comment, update it, otherwise create a new one\n      if (botComment) {\n        github.rest.issues.updateComment({\n          owner: context.repo.owner,\n          repo: context.repo.repo,\n          comment_id: botComment.id,\n          body: output\n        })\n      } else {\n        github.rest.issues.createComment({\n          issue_number: context.issue.number,\n          owner: context.repo.owner,\n          repo: context.repo.repo,\n          body: output\n        })\n      }\n```\n\n## Inputs\n\nThe action supports the following inputs:\n\n- `cli_config_credentials_hostname` - (optional) The hostname of a HCP Terraform/Terraform Enterprise instance to\n   place within the credentials block of the Terraform CLI configuration file. Defaults to `app.terraform.io`.\n- `cli_config_credentials_token` - (optional) The API token for a HCP Terraform/Terraform Enterprise instance to\n   place within the credentials block of the Terraform CLI configuration file.\n- `terraform_version` - (optional) The version of Terraform CLI to install. Instead of a full version string,\n   you can also specify a constraint string (see [Semver Ranges](https://www.npmjs.com/package/semver#ranges)\n   for available range specifications). Examples are: `\"\u003c1.2.0\"`, `\"~1.1.0\"`, `\"1.1.7\"` (all three installing\n   the latest available `1.1` version). Prerelease versions can be specified and a range will stay within the\n   given tag such as `beta` or `rc`. If no version is given, it will default to `latest`.\n- `terraform_wrapper` - (optional) Whether to install a wrapper to wrap subsequent calls of\n   the `terraform` binary and expose its STDOUT, STDERR, and exit code as outputs\n   named `stdout`, `stderr`, and `exitcode` respectively. Defaults to `true`.\n\n## Outputs\n\nThis action does not configure any outputs directly. However, when you set the `terraform_wrapper` input\nto `true`, the following outputs are available for subsequent steps that call the `terraform` binary:\n\n- `stdout` - The STDOUT stream of the call to the `terraform` binary.\n- `stderr` - The STDERR stream of the call to the `terraform` binary.\n- `exitcode` - The exit code of the call to the `terraform` binary.\n\n## License\n\n[Mozilla Public License v2.0](LICENSE)\n\n## Code of Conduct\n\n[Code of Conduct](CODE_OF_CONDUCT.md)\n\n## Experimental Status\n\nBy 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.\n\n## Contributing\n\n### License Headers\n\nAll 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.\n\nThis 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.\n","funding_links":[],"categories":["JavaScript","Community Resources","Tools","CI/CD Integration","others","六、编程语言与工具专属Action"],"sub_categories":["Collection of Actions","CI","8. 环境配置"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhashicorp%2Fsetup-terraform","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhashicorp%2Fsetup-terraform","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhashicorp%2Fsetup-terraform/lists"}