Skip to content

Commit 7c27822

Browse files
authored
Merge pull request #140 from dflook/workspace-eval
Add workspace validation test
2 parents 2305d4f + f54173c commit 7c27822

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

.github/workflows/test-validate.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,41 @@ jobs:
4848
echo "::error:: failure-reason not set correctly"
4949
exit 1
5050
fi
51+
52+
validate_workspace:
53+
runs-on: ubuntu-latest
54+
name: Invalid terraform configuration
55+
steps:
56+
- name: Checkout
57+
uses: actions/checkout@v2
58+
59+
- name: validate prod
60+
uses: ./terraform-validate
61+
with:
62+
path: tests/validate/workspace_eval
63+
workspace: prod
64+
65+
- name: validate dev
66+
uses: ./terraform-validate
67+
with:
68+
path: tests/validate/workspace_eval
69+
workspace: dev
70+
71+
- name: validate nonexistant workspace
72+
uses: ./terraform-validate
73+
id: validate
74+
continue-on-error: true
75+
with:
76+
path: tests/validate/workspace_eval
77+
78+
- name: Check invalid
79+
run: |
80+
if [[ "${{ steps.validate.outcome }}" != "failure" ]]; then
81+
echo "Validate did not fail correctly"
82+
exit 1
83+
fi
84+
85+
if [[ "${{ steps.validate.outputs.failure-reason }}" != "validate-failed" ]]; then
86+
echo "::error:: failure-reason not set correctly"
87+
exit 1
88+
fi

image/entrypoints/validate.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ setup
1010
# You can't initialize without having valid terraform.
1111
# How do you get a full validation report? You can't.
1212

13+
# terraform.workspace will be evaluated during a validate, but it is not initialized properly.
14+
# Pass through the workspace input, even if it doesn't make sense for some backends.
15+
1316
init || true
1417

15-
if ! (cd "$INPUT_PATH" && terraform validate -json | convert_validate_report "$INPUT_PATH"); then
16-
(cd "$INPUT_PATH" && terraform validate)
18+
if ! (cd "$INPUT_PATH" && TF_WORKSPACE="$INPUT_WORKSPACE" terraform validate -json | convert_validate_report "$INPUT_PATH"); then
19+
(cd "$INPUT_PATH" && TF_WORKSPACE="$INPUT_WORKSPACE" terraform validate)
1720
else
1821
echo -e "\033[1;32mSuccess!\033[0m The configuration is valid"
1922
fi

terraform-validate/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ If the terraform configuration is not valid, the build is failed.
2323
- Optional
2424
- Default: The action workspace
2525

26+
* `workspace`
27+
28+
Terraform workspace to use for the `terraform.workspace` value while validating.
29+
30+
- Type: string
31+
- Optional
32+
- Default: `default`
33+
2634
## Outputs
2735

2836
* `failure-reason`

terraform-validate/action.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ inputs:
77
description: Path to the terraform configuration
88
required: false
99
default: .
10+
workspace:
11+
description: Name of the workspace to use for the `terraform.workspace` value while validating.
12+
required: false
13+
default: default
1014

1115
runs:
1216
using: docker

tests/validate/workspace_eval/main.tf

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
locals {
2+
aws_provider_config = {
3+
prod = {
4+
region = "..."
5+
account_id = "..."
6+
profile = "..."
7+
}
8+
dev = {
9+
region = "..."
10+
account_id = "..."
11+
profile = "..."
12+
}
13+
}
14+
}
15+
16+
provider "aws" {
17+
region = local.aws_provider_config[terraform.workspace].region
18+
profile = local.aws_provider_config[terraform.workspace].profile
19+
allowed_account_ids = [local.aws_provider_config[terraform.workspace].account_id]
20+
}
21+
22+
resource "aws_s3_bucket" "bucket" {
23+
bucket = "hello"
24+
}
25+
26+
terraform {
27+
backend "remote" {
28+
hostname = "app.terraform.io"
29+
organization = "flooktech"
30+
31+
workspaces {
32+
name = "banana"
33+
}
34+
}
35+
}
36+

0 commit comments

Comments
 (0)