hashicorp / hashicorp/setup-terraform

Reactions to ease of use with new actions workflow

Open
#57 1 comment 0 reactions 0 assignees View on GitHub
documentation
Dominant language
JavaScript
Stars
1.6k
Forks
288
Avg merge
23h 21m
Merged PRs (30d)
3

Description

From #7;
> Migrating from terraform-github-actions with PR comment support is hard

yeah, this is hard alright.

My immediate reactions when working with this in order to get a good workflow:

- I'm not sure it makes sense to split `stdout` and `stderr` into two different outputs. Errors reported from `terraform fmt -check` are printed to stdout, while `terraform validate` ends up in stderr. I would rather prefer everything ends up in a single output.

- When we have to use `continue-on-error` in order to get the run results as a comment on the PR, an additional check is required to actually fail the actions run, which feels clunky

```
- name: 'End results'
shell: bash
run: |
echo fmt
test ${{ steps.fmt.outputs.exitcode }} -eq 0
echo init
test ${{ steps.init.outputs.exitcode }} -eq 0
echo validate
test ${{ steps.validate.outputs.exitcode }} -eq 0
```

- In our workflow, we want `fmt`, `init` and `validate`. For reference, this is what we end up with.

```
name: 'Terraform'

on:
push:
branches:
- main
- master
pull_request:
branches:
- main
- master

jobs:
terraform:
name: 'Terraform'
runs-on: ubuntu-latest
steps:
- name: 'Checkout'
uses: actions/checkout@v2

- name: 'Setup'
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 0.13.4

- name: 'Check formatting'
id: fmt
run: terraform fmt -check -recursive -list=true
continue-on-error: true

- name: 'Initialise'
id: init
run: terraform init -no-color
continue-on-error: true

- name: 'Validate'
id: validate
run: terraform validate -no-color
continue-on-error: true

- name: 'Post results as comment'
uses: actions/github-script@v3
if: github.event_name == 'pull_request'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const CODE = '```';
const output = `
### Terraform
Format: **${{ steps.fmt.outcome }}**

Output

${CODE}
# stdout
${{ steps.fmt.outputs.stdout }}
${CODE}

Init: **${{ steps.init.outcome }}**

Output

${CODE}
# stderr
${{ steps.init.outputs.stderr }}
${CODE}

Validate: **${{ steps.validate.outcome }}**

Output

${CODE}
# stderr
${{ steps.validate.outputs.stderr }}
${CODE}

`;

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

- name: 'End results'
shell: bash
run: |
echo fmt
test ${{ steps.fmt.outputs.exitcode }} -eq 0
echo init
test ${{ steps.init.outputs.exitcode }} -eq 0
echo validate
test ${{ steps.validate.outputs.exitcode }} -eq 0
```

Which in turn posts a result like the below (when there are format and validation errors). As you can see, I have to add some placeholder content inside the code tags (`#stdout`) to prevent bad formatting in the rendered GitHub comment if there's no output at all.

---
### Terraform
Format: **failure**

Output

```
# stdout
test.tf

```

Init: **success**

Output

```
# stderr

```

Validate: **failure**

Output

```
# stderr

Error: Unsupported attribute

on siteshield/outputs.tf line 3, in output "cidr_list":
3: value = data.external.cidr_list.resul

This object has no argument, nested block, or exported attribute named
"resul". Did you mean "result"?

```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.