Skip to content

Commit 4dff81d

Browse files
OJFordaustinvalle
andauthored
Fix output malformed when wrapper enabled (#367)
* Fix output malformed when wrapper enabled Presently using a command such as `terraform output -json | jq` does not work with the wrapper enabled, as it is by default. In order to consume terraform's output having set it up with this Action, it is necessary either to disable the wrapper (`with: terraform_wrapper: false`) or run it in its own Actions step with an explicit `id` (e.g. `id: foo`) so that it can be referred to and consumed (`${{steps.foo.outputs.stdout}}` et al.) in later steps. This seems to be the result of much confusion (issues passim) and is not at all easy (#338) to debug/diagnose and come to the realisation that it's due to the wrapper, or even that such a thing exists. @austinvalle identified the issue as being due to the `@actions/exec` package writing the spawned command to stdout (along with then its actual stdout). This has previously been reported upstream in actions/toolkit#649; I've proposed actions/toolkit#1573 to fix it. This commit aims to address the issue for `setup-terraform` in the meantime by silencing `@actions/exec` and then writing out to stdout & stderr from the listener buffers, which it writes to without this additional logging. Closes #20, #80, #85, #149, #338, and probably more. * add test for stdout with jq * update test name * remove debug lines and add changelog * add additional note about the bug fix to wrapper --------- Co-authored-by: Austin Valle <[email protected]>
1 parent 4c41f96 commit 4dff81d

File tree

6 files changed

+99
-15
lines changed

6 files changed

+99
-15
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: BUG FIXES
2+
body: Fixed malformed stdout when wrapper is enabled
3+
time: 2023-10-27T09:26:14.675402-04:00
4+
custom:
5+
Issue: "367"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
kind: NOTES
2+
body: The wrapper around the installed Terraform binary has been fixed to return the
3+
exact STDOUT and STDERR from Terraform when executing commands. Previous versions
4+
of setup-terraform may have required workarounds to process the STDOUT in bash,
5+
such as filtering out the first line or selectively parsing STDOUT with jq. These
6+
workarounds may need to be adjusted with `v3.0.0`, which will now return just the
7+
STDOUT/STDERR from Terraform with no errant characters/statements.
8+
time: 2023-10-27T13:43:31.430759-04:00
9+
custom:
10+
Issue: "367"

.github/workflows/data/local/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
resource "null_resource" "null" {
2-
triggers = {
3-
value = timestamp()
4-
}
1+
resource "random_pet" "pet" {}
2+
3+
output "pet" {
4+
value = random_pet.pet.id
55
}

.github/workflows/setup-terraform.yml

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: 'Setup Terraform'
1+
name: 'setup-terraform tests'
22

33
on:
44
push:
@@ -303,3 +303,70 @@ jobs:
303303
- name: Terraform Plan
304304
id: plan
305305
run: terraform plan
306+
307+
308+
terraform-stdout-wrapper:
309+
name: 'Terraform STDOUT'
310+
runs-on: ${{ matrix.os }}
311+
strategy:
312+
matrix:
313+
os: [ubuntu-latest, windows-latest, macos-latest]
314+
defaults:
315+
run:
316+
shell: bash
317+
working-directory: ./.github/workflows/data/local
318+
steps:
319+
- name: Checkout
320+
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
321+
322+
- name: Setup Terraform
323+
uses: ./
324+
with:
325+
terraform_wrapper: true
326+
327+
- name: Terraform Init
328+
run: terraform init
329+
330+
- name: Terraform Format
331+
run: terraform fmt -check
332+
333+
- name: Terraform Apply
334+
id: apply
335+
run: terraform apply -auto-approve
336+
337+
- name: Terraform Output to JQ
338+
id: output
339+
run: terraform output -json | jq '.pet.value'
340+
341+
terraform-stdout-no-wrapper:
342+
name: 'Terraform STDOUT No Wrapper'
343+
runs-on: ${{ matrix.os }}
344+
strategy:
345+
matrix:
346+
os: [ubuntu-latest, windows-latest, macos-latest]
347+
defaults:
348+
run:
349+
shell: bash
350+
working-directory: ./.github/workflows/data/local
351+
steps:
352+
- name: Checkout
353+
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
354+
355+
- name: Setup Terraform
356+
uses: ./
357+
with:
358+
terraform_wrapper: false
359+
360+
- name: Terraform Init
361+
run: terraform init
362+
363+
- name: Terraform Format
364+
run: terraform fmt -check
365+
366+
- name: Terraform Apply
367+
id: apply
368+
run: terraform apply -auto-approve
369+
370+
- name: Terraform Output to JQ
371+
id: output
372+
run: terraform output -json | jq '.pet.value'

dist/index1.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27221,13 +27221,14 @@ async function checkTerraform () {
2722127221
const args = process.argv.slice(2);
2722227222
const options = {
2722327223
listeners,
27224-
ignoreReturnCode: true
27224+
ignoreReturnCode: true,
27225+
silent: true, // avoid printing command in stdout: https://github.com/actions/toolkit/issues/649
2722527226
};
2722627227
const exitCode = await exec(pathToCLI, args, options);
27227-
core.debug(`Terraform exited with code ${exitCode}.`);
27228-
core.debug(`stdout: ${stdout.contents}`);
27229-
core.debug(`stderr: ${stderr.contents}`);
27230-
core.debug(`exitcode: ${exitCode}`);
27228+
27229+
// Pass-through stdout/err as `exec` won't due to `silent: true` option
27230+
process.stdout.write(stdout.contents);
27231+
process.stderr.write(stderr.contents);
2723127232

2723227233
// Set outputs, result, exitcode, and stderr
2723327234
core.setOutput('stdout', stdout.contents);

wrapper/terraform.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ async function checkTerraform () {
3333
const args = process.argv.slice(2);
3434
const options = {
3535
listeners,
36-
ignoreReturnCode: true
36+
ignoreReturnCode: true,
37+
silent: true, // avoid printing command in stdout: https://github.com/actions/toolkit/issues/649
3738
};
3839
const exitCode = await exec(pathToCLI, args, options);
39-
core.debug(`Terraform exited with code ${exitCode}.`);
40-
core.debug(`stdout: ${stdout.contents}`);
41-
core.debug(`stderr: ${stderr.contents}`);
42-
core.debug(`exitcode: ${exitCode}`);
40+
41+
// Pass-through stdout/err as `exec` won't due to `silent: true` option
42+
process.stdout.write(stdout.contents);
43+
process.stderr.write(stderr.contents);
4344

4445
// Set outputs, result, exitcode, and stderr
4546
core.setOutput('stdout', stdout.contents);

0 commit comments

Comments
 (0)