Skip to content

Commit

Permalink
Add support for specifying terraform targets (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evy Bongers authored Dec 2, 2021
1 parent 9d14556 commit c4ffaa4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
3 changes: 3 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ inputs:
required: false
description: Limit the number of concurrent operations during plan/apply
default: '10'
terraform_targets:
required: false
description: A multiline string containing targets that should be passed to terraform (one per line)
terraform_variables:
required: false
description: 'A JSON string containing variables that should be passed to terraform. For example: {"my_var": "my_value"}'
Expand Down
19 changes: 8 additions & 11 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5240,10 +5240,11 @@ async function terraform(args) {

(async () => {
const terraformDirectory = core.getInput('terraform_directory');
let terraformDoApply = core.getInput('terraform_do_apply') === 'true';
let terraformDoDestroy = core.getInput('terraform_do_destroy') === 'true';
const terraformLock = core.getInput('terraform_lock');
let terraformDoApply = core.getBooleanInput('terraform_do_apply');
let terraformDoDestroy = core.getBooleanInput('terraform_do_destroy');
const terraformLock = core.getBooleanInput('terraform_lock');
const terraformParallelism = core.getInput('terraform_parallelism');
const terraformTargets = core.getMultilineInput('terraform_targets');
const terraformVariables = core.getInput('terraform_variables');
const terraformWorkspace = core.getInput('terraform_workspace');

Expand All @@ -5258,10 +5259,6 @@ async function terraform(args) {
let tf_workspace_deletion = status_skipped;

core.startGroup('Sanity checking inputs');
if (terraformLock !== 'true' && terraformLock !== 'false') {
core.setFailed(`Sanity checks failed. Unknown value for 'terraform_lock': ${terraformLock}`);
process.exit(1);
}
if (/^\d+$/.test(terraformParallelism) === false) {
core.setFailed(`Sanity checks failed. Non-integer value for 'terraform_parallelism': ${terraformParallelism}`);
process.exit(1);
Expand Down Expand Up @@ -5379,7 +5376,7 @@ async function terraform(args) {
}

core.startGroup('Run terraform plan');
const tfp = await terraform(['plan', `-lock=${terraformLock}`, `-parallelism=${terraformParallelism}`, '-out=terraform.plan']);
const tfp = await terraform(['plan', `-lock=${terraformLock}`, `-parallelism=${terraformParallelism}`, '-out=terraform.plan'].concat(terraformTargets));
core.endGroup();
if (tfp.status > 0) {
tf_plan = status_failed;
Expand All @@ -5391,7 +5388,7 @@ async function terraform(args) {

core.startGroup('Run terraform apply');
if (terraformDoApply === true) {
const tfa = await terraform(['apply', `-lock=${terraformLock}`, `-parallelism=${terraformParallelism}`, '-auto-approve', 'terraform.plan']);
const tfa = await terraform(['apply', `-lock=${terraformLock}`, `-parallelism=${terraformParallelism}`, '-auto-approve', 'terraform.plan'].concat(terraformTargets));
core.endGroup();
if (tfa.status > 0) {
tf_apply = status_failed;
Expand All @@ -5407,7 +5404,7 @@ async function terraform(args) {
/* DESTROY START */
core.startGroup('Run terraform destroy');
if (terraformDoDestroy === true) {
const tfd = await terraform(['destroy', `-lock=${terraformLock}`, `-parallelism=${terraformParallelism}`, '-auto-approve']);
const tfd = await terraform(['destroy', `-lock=${terraformLock}`, `-parallelism=${terraformParallelism}`, '-auto-approve'].concat(terraformTargets));
core.info(tfd.stdout);
core.endGroup();
if (tfd.status > 0) {
Expand All @@ -5416,7 +5413,7 @@ async function terraform(args) {
} else {
tf_destroy = status_success;

if (terraformWorkspace && terraformWorkspace !== 'default') {
if (terraformWorkspace && terraformWorkspace !== 'default' && !terraformTargets) {
core.startGroup('Run terraform workspace deletion');
await terraform(['workspace', 'select', 'default']); // have to switch to different workspace before deleting workspace defined in `terraformWorkspace`
const tfwd = await terraform(['workspace', 'delete', terraformWorkspace]); // have to switch to different workspace before deleting workspace defined in `terraformWorkspace`
Expand Down
19 changes: 8 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ async function terraform(args) {

(async () => {
const terraformDirectory = core.getInput('terraform_directory');
let terraformDoApply = core.getInput('terraform_do_apply') === 'true';
let terraformDoDestroy = core.getInput('terraform_do_destroy') === 'true';
const terraformLock = core.getInput('terraform_lock');
let terraformDoApply = core.getBooleanInput('terraform_do_apply');
let terraformDoDestroy = core.getBooleanInput('terraform_do_destroy');
const terraformLock = core.getBooleanInput('terraform_lock');
const terraformParallelism = core.getInput('terraform_parallelism');
const terraformTargets = core.getMultilineInput('terraform_targets');
const terraformVariables = core.getInput('terraform_variables');
const terraformWorkspace = core.getInput('terraform_workspace');

Expand All @@ -57,10 +58,6 @@ async function terraform(args) {
let tf_workspace_deletion = status_skipped;

core.startGroup('Sanity checking inputs');
if (terraformLock !== 'true' && terraformLock !== 'false') {
core.setFailed(`Sanity checks failed. Unknown value for 'terraform_lock': ${terraformLock}`);
process.exit(1);
}
if (/^\d+$/.test(terraformParallelism) === false) {
core.setFailed(`Sanity checks failed. Non-integer value for 'terraform_parallelism': ${terraformParallelism}`);
process.exit(1);
Expand Down Expand Up @@ -178,7 +175,7 @@ async function terraform(args) {
}

core.startGroup('Run terraform plan');
const tfp = await terraform(['plan', `-lock=${terraformLock}`, `-parallelism=${terraformParallelism}`, '-out=terraform.plan']);
const tfp = await terraform(['plan', `-lock=${terraformLock}`, `-parallelism=${terraformParallelism}`, '-out=terraform.plan'].concat(terraformTargets));
core.endGroup();
if (tfp.status > 0) {
tf_plan = status_failed;
Expand All @@ -190,7 +187,7 @@ async function terraform(args) {

core.startGroup('Run terraform apply');
if (terraformDoApply === true) {
const tfa = await terraform(['apply', `-lock=${terraformLock}`, `-parallelism=${terraformParallelism}`, '-auto-approve', 'terraform.plan']);
const tfa = await terraform(['apply', `-lock=${terraformLock}`, `-parallelism=${terraformParallelism}`, '-auto-approve', 'terraform.plan'].concat(terraformTargets));
core.endGroup();
if (tfa.status > 0) {
tf_apply = status_failed;
Expand All @@ -206,7 +203,7 @@ async function terraform(args) {
/* DESTROY START */
core.startGroup('Run terraform destroy');
if (terraformDoDestroy === true) {
const tfd = await terraform(['destroy', `-lock=${terraformLock}`, `-parallelism=${terraformParallelism}`, '-auto-approve']);
const tfd = await terraform(['destroy', `-lock=${terraformLock}`, `-parallelism=${terraformParallelism}`, '-auto-approve'].concat(terraformTargets));
core.info(tfd.stdout);
core.endGroup();
if (tfd.status > 0) {
Expand All @@ -215,7 +212,7 @@ async function terraform(args) {
} else {
tf_destroy = status_success;

if (terraformWorkspace && terraformWorkspace !== 'default') {
if (terraformWorkspace && terraformWorkspace !== 'default' && !terraformTargets) {
core.startGroup('Run terraform workspace deletion');
await terraform(['workspace', 'select', 'default']); // have to switch to different workspace before deleting workspace defined in `terraformWorkspace`
const tfwd = await terraform(['workspace', 'delete', terraformWorkspace]); // have to switch to different workspace before deleting workspace defined in `terraformWorkspace`
Expand Down

0 comments on commit c4ffaa4

Please sign in to comment.