Skip to content

Commit 85e373b

Browse files
authored
[NDR-307] Build your sandbox from a single make command (#488)
1 parent 07623cd commit 85e373b

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

makefile

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
default: help
22

3+
help: ## This help message
4+
@grep -E --no-filename '^[a-zA-Z-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-42s\033[0m %s\n", $$1, $$2}'
5+
36
.PHONY: Install
4-
install:
7+
install: ## Run NPM install
58
cd ./infrastructure && npm install
69

710
# Formatting
811
.PHONY:format-all
9-
format-all:
12+
format-all: ## Format all terraform
1013
terraform fmt -recursive .
1114

1215
# Documentation
1316
.PHONY:generate-terraform-docs
14-
generate-terraform-docs:
17+
generate-terraform-docs: ## Generate terraform documentation
1518
./scripts/run_terraform_docs.py
1619

1720
# Installing
18-
21+
.PHONY:build-sandbox
22+
build-sandbox: ## Build a sandbox using either the branch as the workspace name, sanitised, or pass in a name for the workspace e.g. make build-sandbox WORKSPACE=my-workspace . By default only a plan will run unless APPLY=true is used.
23+
WORKSPACE=$(WORKSPACE) APPLY=$(APPLY) ./scripts/build_sandbox.sh
1924
# Linting
2025

2126
# Testing
2227

2328
# Bootstrap
2429
.PHONY: init-bootstrap
25-
init-bootstrap:
30+
init-bootstrap: ## Run Bootstrap terraform
2631
cd ./bootstrap && terraform init
2732

2833
.PHONY: apply-bootstrap
29-
apply-bootstrap:
34+
apply-bootstrap: ## Apply Bootstrap terraform
3035
cd ./bootstrap && terraform apply

scripts/build_sandbox.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
required_profile="NDR-Dev-RW"
5+
6+
if [[ "${AWS_PROFILE:-}" != "$required_profile" ]]; then
7+
echo "❌ Error: AWS_PROFILE must be set to \"$required_profile\""
8+
echo "👉 Example: export AWS_PROFILE=$required_profile"
9+
exit 1
10+
fi
11+
12+
branch="${WORKSPACE:-$(git rev-parse --abbrev-ref HEAD)}"
13+
branch=$(echo "$branch" | sed 's/[^a-zA-Z0-9]//g')
14+
branch="${branch,,}"
15+
apply="${APPLY:-false}"
16+
17+
if [ ${#branch} -gt 7 ]; then
18+
echo "Error: your workspace name '$branch' is longer than 7 characters, please start again and choose a workspace less than 8 characters."
19+
exit 1
20+
fi
21+
22+
# Forbidden branches
23+
forbidden_workspaces=("main" "prod" "pre-prod" "ndr-test" "ndr-dev" "preprod" "ndrtest" "ndrdev", "dev", "test")
24+
25+
for fb in "${forbidden_workspaces[@]}"; do
26+
if [[ "$branch" == "$fb" ]]; then
27+
echo "❌ Error: Deployment of workspace '$fb' is not allowed. If you are trying to deploy main to your sandbox then provide a workspace name."
28+
exit 1
29+
fi
30+
done
31+
32+
cd infrastructure/
33+
terraform init -backend-config=backend.conf
34+
terraform workspace select -or-create "$branch"
35+
terraform plan -input=false -var-file=dev.tfvars -out tf.plan
36+
37+
if [[ "$apply" == "true" ]]; then
38+
terraform apply -auto-approve -input=false tf.plan
39+
fi

0 commit comments

Comments
 (0)