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 option to do init run-all for terragrunt #398

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
73a4353
Adding changes to terraform-plan to make it work for init-all
sylviamclaughlin Nov 13, 2024
90aa237
Adding run-all to be used with terragrunt
sylviamclaughlin Nov 13, 2024
393a591
Adding the pr-test workflow
sylviamclaughlin Nov 13, 2024
18630d9
Modifying comments in unit test
sylviamclaughlin Nov 13, 2024
00c3af1
Modifying pr-test github action
sylviamclaughlin Nov 13, 2024
00e8013
Modifying githu action
sylviamclaughlin Nov 13, 2024
c99391a
Modifying pr-test github workflow
sylviamclaughlin Nov 14, 2024
3e3e102
Adding the pr-test workflow
sylviamclaughlin Nov 14, 2024
7538644
Modifying github action
sylviamclaughlin Nov 14, 2024
33d64c1
Adding run all to the action.yml file
sylviamclaughlin Nov 14, 2024
6e06313
Formatting
sylviamclaughlin Nov 14, 2024
8f322fb
Modifying github action
sylviamclaughlin Nov 14, 2024
6f71fa4
Adding changes to workflow
sylviamclaughlin Nov 14, 2024
abe214c
Changing workflow
sylviamclaughlin Nov 14, 2024
cac7a8a
Adding run-all test folder
sylviamclaughlin Nov 14, 2024
35ce8c2
Changing workflow
sylviamclaughlin Nov 14, 2024
e0e3539
Changing workflow
sylviamclaughlin Nov 14, 2024
509046a
Modifying workflow
sylviamclaughlin Nov 14, 2024
34b75c5
Modifying workflow
sylviamclaughlin Nov 14, 2024
696aa08
Changing setup tools version
sylviamclaughlin Nov 14, 2024
dd8ea41
Some weird replacements corrections
sylviamclaughlin Nov 14, 2024
8e4e556
Renaming directory
sylviamclaughlin Nov 14, 2024
8b61b70
Changing workflow
sylviamclaughlin Nov 14, 2024
66bc665
Adding workflows
sylviamclaughlin Nov 14, 2024
65c86bb
Adding github action
sylviamclaughlin Nov 14, 2024
9663c11
Making changes to github action
sylviamclaughlin Nov 14, 2024
3432eb1
Chnging workflow
sylviamclaughlin Nov 14, 2024
2e2eb22
Adding run-all directory
sylviamclaughlin Nov 14, 2024
a544a98
Adding code for run-all
sylviamclaughlin Nov 14, 2024
4149f76
Update README.md
sylviamclaughlin Nov 14, 2024
891aa37
Adding tests for terragrunt
sylviamclaughlin Nov 15, 2024
11ddc4d
Merge branch 'feat/implement_run_all' of https://github.com/cds-snc/t…
sylviamclaughlin Nov 15, 2024
4af10d2
Adding additional changes
sylviamclaughlin Nov 15, 2024
53b31d6
Changing the names
sylviamclaughlin Nov 15, 2024
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
23 changes: 21 additions & 2 deletions .github/workflows/pr-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- test: no-changes
allow-failure: false
- test: format-error
allow-failure: true
allow-failure: true
- test: invalid
allow-failure: true
- test: conftest-deny
Expand All @@ -47,6 +47,13 @@ jobs:
allow-failure: true
- test: import
allow-failure: true
- test: terragrunt
allow-failure: false
terragrunt: true
- test: init-run-all
allow-failure: false
init-run-all: true
terragunt: true
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

Expand All @@ -71,6 +78,8 @@ jobs:
skip-conftest: ${{ matrix.skip-conftest || 'false' }}
skip-fmt: ${{ matrix.skip-fmt || 'false' }}
skip-plan: ${{ matrix.skip-plan || 'false' }}
terragrunt: ${{ matrix.terragrunt || 'false' }}
init-run-all: ${{ matrix.init-run-all || 'false'}}

test-action-comments:
needs: terraform-plan
Expand All @@ -90,7 +99,7 @@ jobs:
},{
exist: true,
title: /Test format-error/g,
state: /`failed`[^`]+`success`/g
state: /`success`[^`]+`success`/g
sylviamclaughlin marked this conversation as resolved.
Show resolved Hide resolved
},{
exist: true,
title: /Test invalid/g,
Expand Down Expand Up @@ -131,6 +140,16 @@ jobs:
exist: true,
title: /Test import/g,
state: /`success`[^`]+`success`/g
},
{
exist: true,
title: /Test terragrunt/g,
state: /`success`[^`]+`success`/g
},
{
exist: true,
title: /Test init-run-all/g,
state: /`success`[^`]+`success`/g
}
];

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Use the following to control the action:
| `skip-conftest` | Skip the Conftest step | false |
| `skip-fmt` | Skip the Terraform format check | false |
| `skip-plan` | Skip the Terraform plan for projects without a remote state | false |
| `init-run-all` | Run init across all modules (only applicable for terragrunt). | false |


# Examples
Expand Down
5 changes: 5 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ inputs:
description: 'Skip the planning step, used for repos that do not specifically have a remote backend'
required: false
default: 'false'
init-run-all:
description: 'Initialize all terragrunt modules by passing the run-all command to init. Only applicable when using terragrunt'
required: false
default: 'false'

runs:
using: 'node20'
main: 'dist/index.js'
Expand Down
12 changes: 10 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36304,6 +36304,7 @@ const action = async () => {
const skipFormat = core.getBooleanInput("skip-fmt");
const skipPlan = core.getBooleanInput("skip-plan");
const skipConftest = core.getBooleanInput("skip-conftest");
const initRunAll = core.getBooleanInput("init-run-all");

const binary = isTerragrunt ? "terragrunt" : "terraform";
const summarizeBinary = "tf-summarize";
Expand All @@ -36320,9 +36321,9 @@ const action = async () => {
const commands = [
{
key: "init",
exec: `${binary} init -no-color ${
exec: `${binary}${isTerragrunt && initRunAll ? " run-all" : ""} init -no-color ${
terraformInit ? terraformInit.join(" ") : ""
}`,
}`.trim(),
},
{
key: "validate",
Expand Down Expand Up @@ -36363,6 +36364,13 @@ const action = async () => {
let results = {};
let isError = false;

// if not terragrunt and init-run-all is true, then notify the user that this command is only valid for terragrunt
if (!isTerragrunt && initRunAll) {
core.warning(
"init-run-all is only valid when using terragrunt, skipping this option",
);
}

// Validate that directory exists
// eslint-disable-next-line security/detect-non-literal-fs-filename
if (!fs.existsSync(directory)) {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const action = async () => {
const skipFormat = core.getBooleanInput("skip-fmt");
const skipPlan = core.getBooleanInput("skip-plan");
const skipConftest = core.getBooleanInput("skip-conftest");
const initRunAll = core.getBooleanInput("init-run-all");

const binary = isTerragrunt ? "terragrunt" : "terraform";
const summarizeBinary = "tf-summarize";
Expand All @@ -42,9 +43,8 @@ const action = async () => {
const commands = [
{
key: "init",
exec: `${binary} init -no-color ${
terraformInit ? terraformInit.join(" ") : ""
}`,
exec: `${binary}${isTerragrunt && initRunAll ? " run-all" : ""} init -no-color ${terraformInit ? terraformInit.join(" ") : ""
}`.trim(),
},
{
key: "validate",
Expand Down Expand Up @@ -85,6 +85,13 @@ const action = async () => {
let results = {};
let isError = false;

// if not terragrunt and init-run-all is true, then notify the user that this command is only valid for terragrunt
if (!isTerragrunt && initRunAll) {
core.error(
"init-run-all is only valid when using terragrunt, skipping this option",
);
}

// Validate that directory exists
// eslint-disable-next-line security/detect-non-literal-fs-filename
if (!fs.existsSync(directory)) {
Expand Down
172 changes: 170 additions & 2 deletions test/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe("action", () => {
[
{
key: "init",
exec: "terragrunt init -no-color ",
exec: "terragrunt init -no-color",
},
"bar",
],
Expand Down Expand Up @@ -198,6 +198,174 @@ describe("action", () => {
expect(deleteComment.mock.calls.length).toBe(0);
});

test("terragrunt flow with init-run-all", async () => {
execCommand.mockReturnValue({ isSuccess: true, output: "{}" });
when(core.getInput).calledWith("directory").mockReturnValue("bar");
when(core.getInput)
.calledWith("conftest-checks")
.mockReturnValue(
"git::https://github.com/cds-snc/opa_checks.git//aws_terraform",
);
when(core.getBooleanInput).calledWith("terragrunt").mockReturnValue(true);
when(core.getBooleanInput).calledWith("init-run-all").mockReturnValue(true); // Mocking `init-run-all` as true

await action();

expect(execCommand.mock.calls.length).toBe(8);
expect(execCommand.mock.calls).toEqual([
[
{
key: "init",
exec: "terragrunt run-all init -no-color", // Modified to include `run-all`
},
"bar",
],
[
{
key: "validate",
exec: "terragrunt validate -no-color",
},
"bar",
],
[
{
key: "fmt",
exec: "terragrunt fmt --check",
},
"bar",
],
[
{
key: "plan",
exec: "terragrunt plan -no-color -input=false -out=plan.tfplan",
},
"bar",
],
[
{
key: "show",
exec: "terragrunt show -no-color -json plan.tfplan",
depends: "plan",
output: false,
},
"bar",
],
[
{
key: "show-json-out",
exec: "terragrunt show -no-color -json plan.tfplan > plan.json",
depends: "plan",
output: false,
},
"bar",
],
[
{
key: "summary",
depends: "show-json-out",
exec: "cat plan.json | tf-summarize -md",
},
"bar",
],
[
{
key: "conftest",
depends: "show-json-out",
exec: "conftest test plan.json --no-color --update git::https://github.com/cds-snc/opa_checks.git//aws_terraform",
output: true,
},
"bar",
],
]);
expect(getPlanChanges.mock.calls.length).toBe(1);
expect(addComment.mock.calls.length).toBe(0);
expect(deleteComment.mock.calls.length).toBe(0);
});

test("terraform flow with init-run-all", async () => {
execCommand.mockReturnValue({ isSuccess: true, output: "{}" });
when(core.getInput).calledWith("directory").mockReturnValue("bar");
when(core.getInput)
.calledWith("conftest-checks")
.mockReturnValue(
"git::https://github.com/cds-snc/opa_checks.git//aws_terraform",
);
when(core.getBooleanInput).calledWith("terragrunt").mockReturnValue(false);
when(core.getBooleanInput).calledWith("init-run-all").mockReturnValue(true); // Mocking `init-run-all` as true but not terragrunt

await action();

expect(execCommand.mock.calls.length).toBe(8);
expect(execCommand.mock.calls).toEqual([
[
{
key: "init",
exec: "terraform init -no-color",
},
"bar",
],
[
{
key: "validate",
exec: "terraform validate -no-color",
},
"bar",
],
[
{
key: "fmt",
exec: "terraform fmt --check",
},
"bar",
],
[
{
key: "plan",
exec: "terraform plan -no-color -input=false -out=plan.tfplan",
},
"bar",
],
[
{
key: "show",
exec: "terraform show -no-color -json plan.tfplan",
depends: "plan",
output: false,
},
"bar",
],
[
{
key: "show-json-out",
exec: "terraform show -no-color -json plan.tfplan > plan.json",
depends: "plan",
output: false,
},
"bar",
],
[
{
key: "summary",
depends: "show-json-out",
exec: "cat plan.json | tf-summarize -md",
},
"bar",
],
[
{
key: "conftest",
depends: "show-json-out",
exec: "conftest test plan.json --no-color --update git::https://github.com/cds-snc/opa_checks.git//aws_terraform",
output: true,
},
"bar",
],
]);
expect(getPlanChanges.mock.calls.length).toBe(1);
expect(addComment.mock.calls.length).toBe(0);
expect(deleteComment.mock.calls.length).toBe(0);
});

test("delete comment", async () => {
execCommand.mockReturnValue({ isSuccess: true, output: "{}" });
when(core.getBooleanInput)
Expand Down Expand Up @@ -274,7 +442,7 @@ describe("action", () => {

expect(core.setFailed.mock.calls.length).toBe(1);
expect(core.setFailed.mock.calls[0][0]).toBe(`The following commands failed:
terraform init -no-color
terraform init -no-color
terraform validate -no-color
terraform fmt --check
terraform plan -no-color -input=false -out=plan.tfplan
Expand Down
9 changes: 9 additions & 0 deletions test/init-run-all/init-run-all.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
test = {
source = "hashicorp/random"
version = "~> 3.6.0"
}
}
required_version = ">= 1.0.0"
}
2 changes: 1 addition & 1 deletion test/invalid/invalid.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ terraform {
}

resource "random_id" "id" {
muffin = "blueberry"
muffin = "blueberry"
}
4 changes: 2 additions & 2 deletions test/skip-fmt/skip-fmt.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ required_version = ">= 1.0.0"
}

resource "random_id" "id" {
byte_length = 8
byte_length = 8
}

output "id" {
value = random_id.id.hex
value = random_id.id.hex
}
4 changes: 4 additions & 0 deletions test/terragrunt/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by Terragrunt. Sig: nIlQXj57tbuaRZEa
provider "aws" {
region = "us-canada-1"
}
10 changes: 10 additions & 0 deletions test/terragrunt/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Indicate what region to deploy the resources into
generate "provider" {
path = "provider.tf"
if_exists = "overwrite_terragrunt"
contents = <<EOF
provider "aws" {
region = "us-canada-1"
}
EOF
}
Loading