diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 159e8c97a..1d7b46e60 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -38,5 +38,7 @@ jobs: bun-version: latest - name: Install dependencies run: bun install - - name: Run tests + - name: Run TypeScript tests run: bun test + - name: Run Terraform Validate + run: bun terraform-validate diff --git a/package.json b/package.json index aa3c7e220..081315498 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,9 @@ "name": "modules", "scripts": { "test": "bun test", + "terraform-validate": "./scripts/terraform_validate.sh", "fmt": "bun x prettier -w **/*.sh .sample/run.sh new.sh **/*.ts **/*.md *.md && terraform fmt **/*.tf .sample/main.tf", - "fmt:ci": "bun x prettier --check **/*.sh .sample/run.sh new.sh **/*.ts **/*.md *.md && terraform fmt -check **/*.tf .sample/main.tf", - "update-version": "./update-version.sh" + "fmt:ci": "bun x prettier --check **/*.sh .sample/run.sh new.sh **/*.ts **/*.md *.md && terraform fmt -check **/*.tf .sample/main.tf" }, "devDependencies": { "@types/bun": "^1.2.9", diff --git a/scripts/terraform_validate.sh b/scripts/terraform_validate.sh new file mode 100755 index 000000000..2c28a0da2 --- /dev/null +++ b/scripts/terraform_validate.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +set -euo pipefail + +validate_terraform_directory() { + local dir="$1" + echo "Running \`terraform validate\` in $dir" + pushd "$dir" + terraform init -upgrade + terraform validate + popd +} + +main() { + # Get the directory of the script + local script_dir=$(dirname "$(readlink -f "$0")") + + # Code assumes that registry directory will always be in same position + # relative to the main script directory + local registry_dir="$script_dir/../registry" + + # Get all subdirectories in the registry directory. Code assumes that + # Terraform directories won't begin to appear until three levels deep into + # the registry (e.g., registry/coder/modules/coder-login, which will then + # have a main.tf file inside it) + local subdirs=$(find "$registry_dir" -mindepth 3 -type d | sort) + + for dir in $subdirs; do + # Skip over any directories that obviously don't have the necessary + # files + if test -f "$dir/main.tf"; then + validate_terraform_directory "$dir" + fi + done +} + +main