Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for specifying terraform targets #14

Merged
merged 4 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Comment on lines +42 to +44
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found this method by chance. It validates values against the YAML 1.2 "Core Schema" specification.
https://github.com/actions/toolkit/blob/main/packages/core/src/core.ts#L161-L181

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);
}
Comment on lines -60 to -63
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsoleted by using core.getBooleanInput.

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));
EvyBongers marked this conversation as resolved.
Show resolved Hide resolved
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