Skip to content

Commit

Permalink
Exit early if exitCode is 0 or 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jpogran committed Apr 27, 2022
1 parent 5cd44b3 commit 46ea660
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
26 changes: 16 additions & 10 deletions dist/index1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3040,7 +3040,7 @@ async function checkTerraform () {
return io.which(pathToCLI, check);
}

(async () => {
async () => {
// This will fail if Terraform isn't found, which is what we want
await checkTerraform();

Expand All @@ -3049,14 +3049,14 @@ async function checkTerraform () {
const stderr = new OutputListener();
const listeners = {
stdout: stdout.listener,
stderr: stderr.listener
stderr: stderr.listener,
};

// Execute terraform and capture output
const args = process.argv.slice(2);
const options = {
listeners,
ignoreReturnCode: true
ignoreReturnCode: true,
};
const exitCode = await exec(pathToCLI, args, options);
core.debug(`Terraform exited with code ${exitCode}.`);
Expand All @@ -3065,15 +3065,21 @@ async function checkTerraform () {
core.debug(`exitcode: ${exitCode}`);

// Set outputs, result, exitcode, and stderr
core.setOutput('stdout', stdout.contents);
core.setOutput('stderr', stderr.contents);
core.setOutput('exitcode', exitCode.toString(10));
core.setOutput("stdout", stdout.contents);
core.setOutput("stderr", stderr.contents);
core.setOutput("exitcode", exitCode.toString(10));

if (exitCode === 0 || exitCode === 2) {
// A exitCode of 0 is considered a success
// An exitCode of 2 may be returned when the '-detailed-exitcode' option
// is passed to plan. This denotes Success with non-empty
// diff (changes present).
return;
}

// A non-zero exitCode is considered an error
if (exitCode !== 0) {
core.setFailed(`Terraform exited with code ${exitCode}.`);
}
})();
core.setFailed(`Terraform exited with code ${exitCode}.`);
};

})();

Expand Down
27 changes: 16 additions & 11 deletions wrapper/terraform.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function checkTerraform () {
return io.which(pathToCLI, check);
}

(async () => {
async () => {
// This will fail if Terraform isn't found, which is what we want
await checkTerraform();

Expand All @@ -21,14 +21,14 @@ async function checkTerraform () {
const stderr = new OutputListener();
const listeners = {
stdout: stdout.listener,
stderr: stderr.listener
stderr: stderr.listener,
};

// Execute terraform and capture output
const args = process.argv.slice(2);
const options = {
listeners,
ignoreReturnCode: true
ignoreReturnCode: true,
};
const exitCode = await exec(pathToCLI, args, options);
core.debug(`Terraform exited with code ${exitCode}.`);
Expand All @@ -37,13 +37,18 @@ async function checkTerraform () {
core.debug(`exitcode: ${exitCode}`);

// Set outputs, result, exitcode, and stderr
core.setOutput('stdout', stdout.contents);
core.setOutput('stderr', stderr.contents);
core.setOutput('exitcode', exitCode.toString(10));
core.setOutput("stdout", stdout.contents);
core.setOutput("stderr", stderr.contents);
core.setOutput("exitcode", exitCode.toString(10));

// A non-zero exitCode is considered an error
// An exit-code 2 may be returned when the '-detailed-exitcode' option is passed to plan. This denotes Success with non-empty diff (changes present).
if (exitCode !== 0 || exitCode !== 2 ) {
core.setFailed(`Terraform exited with code ${exitCode}.`);
if (exitCode === 0 || exitCode === 2) {
// A exitCode of 0 is considered a success
// An exitCode of 2 may be returned when the '-detailed-exitcode' option
// is passed to plan. This denotes Success with non-empty
// diff (changes present).
return;
}
})();

// A non-zero exitCode is considered an error
core.setFailed(`Terraform exited with code ${exitCode}.`);
};

0 comments on commit 46ea660

Please sign in to comment.