Skip to content

Commit 62ade84

Browse files
authored
Merge pull request #182 from dflook/remote-workspace-validate
Use correct value for terraform.workspace when validating with remote backend
2 parents 80692d2 + 5a5a2ac commit 62ade84

File tree

5 files changed

+55
-17
lines changed

5 files changed

+55
-17
lines changed

.github/workflows/test-http.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ jobs:
8787
8888
git_no_credentials:
8989
runs-on: ubuntu-latest
90-
name: git_http no creds
90+
name: git+http no creds
9191
steps:
9292
- name: Checkout
9393
uses: actions/checkout@v2

.github/workflows/test-validate.yaml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
5353
validate_workspace:
5454
runs-on: ubuntu-latest
55-
name: Use workspace name during validationg
55+
name: Use workspace name during validation
5656
steps:
5757
- name: Checkout
5858
uses: actions/checkout@v2
@@ -87,3 +87,16 @@ jobs:
8787
echo "::error:: failure-reason not set correctly"
8888
exit 1
8989
fi
90+
91+
validate_remote_workspace:
92+
runs-on: ubuntu-latest
93+
name: Use workspace name during validation
94+
steps:
95+
- name: Checkout
96+
uses: actions/checkout@v2
97+
98+
- name: validate prod
99+
uses: ./terraform-validate
100+
with:
101+
path: tests/workflows/test-validate/workspace_eval_remote
102+
workspace: prod

image/entrypoints/validate.sh

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ setup
1111
# How do you get a full validation report? You can't.
1212

1313
# 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.
14+
# Pass through the workspace input, except for remote backend where it should be 'default'
15+
16+
if [[ "$TERRAFORM_BACKEND_TYPE" == "remote" ]]; then
17+
TF_WORKSPACE="default"
18+
else
19+
TF_WORKSPACE="$INPUT_WORKSPACE"
20+
fi
1521

1622
init || true
1723

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)
24+
if ! (cd "$INPUT_PATH" && TF_WORKSPACE="$TF_WORKSPACE" terraform validate -json | convert_validate_report "$INPUT_PATH"); then
25+
(cd "$INPUT_PATH" && TF_WORKSPACE="$TF_WORKSPACE" terraform validate)
2026
else
2127
echo -e "\033[1;32mSuccess!\033[0m The configuration is valid"
2228
fi

tests/workflows/test-validate/workspace_eval/main.tf

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

0 commit comments

Comments
 (0)