hashicorp / hashicorp/setup-terraform
Migrating from terraform-github-actions with PR comment support is hard
- Dominant language
- JavaScript
- Stars
- 1.6k
- Forks
- 288
- Avg merge
- 23h 21m
- Merged PRs (30d)
- 3
Description
I am trying to migrate from `terraform-github-actions` to take advantage of the speed increase of running Javascript-based actions and native commands over Docker-based actions. I am running into some trouble to replicate the functionality of automatic PR comments.
The Readme provides this as an equivalent example:
```yaml
steps:
- uses: hashicorp/setup-terraform@v1
- run: terraform init
- id: plan
run: terraform plan -no-color
- uses: actions/github-script@0.9.0
if: github.event_name == 'pull_request'
env:
STDOUT: "```${{ steps.plan.outputs.stdout }}```"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: process.env.STDOUT
})
```
However this is not correct, as far as I can tell, due to a number of issues
1. To be able to detect that a plan results in a change, you need to invoke `run: terraform plan -no-color -detailed-exitcode`.
2. Then to ensure that a PR comment is emitted only if there's a change, you need to modify the condition to: `if: github.event_name == 'pull_request' && steps.plan.outputs.exitcode == 2`
3. Of course, to be able to even run the PR comment step you need to make sure that the plan step doesn't return an error: `run: terraform plan -no-color -detailed-exitcode || echo 0`
4. But you still want the job to fail if `terraform plan` returns code `1` in case there's a problem with the terraform files, maybe by adding `if (${{ steps.plan.outputs.exitcode }} == 1) { core.setFailed("There's a problem with the plan"); }` after the PR comment is created.
I can try to whip up a PR to try to at least improve the Readme, but I wanted to ask for some thoughts first on what the intended usage is.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.