From e0891f6c0914580f44e1af373de1df7b8eb42bc4 Mon Sep 17 00:00:00 2001 From: jon brookes Date: Fri, 3 Oct 2025 15:46:30 +0100 Subject: [PATCH] fix: Add create_tfvars script and update pipeline configuration --- .../tf/scripts/build-gcloud-k3s-pipeline.json | 9 ++++++ gcloud/tf/scripts/create_tfvars.sh | 32 +++++++++++++++++++ gcloud/tf/terraform.tfvars.template | 13 ++++++++ 3 files changed, 54 insertions(+) create mode 100755 gcloud/tf/scripts/create_tfvars.sh create mode 100644 gcloud/tf/terraform.tfvars.template diff --git a/gcloud/tf/scripts/build-gcloud-k3s-pipeline.json b/gcloud/tf/scripts/build-gcloud-k3s-pipeline.json index 9bed001..179282f 100644 --- a/gcloud/tf/scripts/build-gcloud-k3s-pipeline.json +++ b/gcloud/tf/scripts/build-gcloud-k3s-pipeline.json @@ -17,6 +17,15 @@ "retryCount": 0, "shouldAbort": true }, + { + "name": "create tfvars", + "function": "RunCommand", + "params": [ + "./gcloud/tf/scripts/create_tfvars.sh" + ], + "retryCount": 0, + "shouldAbort": true + }, { "name": "run tofu", "function": "RunCommand", diff --git a/gcloud/tf/scripts/create_tfvars.sh b/gcloud/tf/scripts/create_tfvars.sh new file mode 100755 index 0000000..26c5cda --- /dev/null +++ b/gcloud/tf/scripts/create_tfvars.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +set -a + +# read environment variables from .env file +# for value $PROJECT_NAME +. .env + + +# Check if PROJECT_NAME environment variable is set +if [ -z "$PROJECT_NAME" ]; then + echo "Error: PROJECT_NAME environment variable is not set." + echo "Please set the PROJECT_NAME variable and try again." + exit 1 +fi + +# Get the directory where the script is located +SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" +cd "$SCRIPT_DIR" || { echo "Failed to change directory to $SCRIPT_DIR"; exit 1; } +# Define the template file path and output file path +TEMPLATE_FILE="../terraform.tfvars.template" +OUTPUT_FILE="../terraform.tfvars" + + +# Use envsubst to substitute the PROJECT_NAME variable into the template +envsubst < "$TEMPLATE_FILE" > "$OUTPUT_FILE" +if [ $? -ne 0 ]; then + echo "Error: Failed to substitute variables in the template." + exit 1 +fi + +echo "tfvars has been created at $OUTPUT_FILE" \ No newline at end of file diff --git a/gcloud/tf/terraform.tfvars.template b/gcloud/tf/terraform.tfvars.template new file mode 100644 index 0000000..819c570 --- /dev/null +++ b/gcloud/tf/terraform.tfvars.template @@ -0,0 +1,13 @@ +// Your GCP project name +// it will be refererred as the project id +// in Google Cloud +// ---------------------------------- +project_name = "${PROJECT_NAME}" + +// where to deploy to +// region +region = "us-central1" +zone = "us-central1-a" + +// application name +app_name = "${PROJECT_NAME}-k3s-cluster"