Skip to content

Commit aefcd20

Browse files
committed
destroy terraform resources after each test at start
1 parent ccf9e39 commit aefcd20

5 files changed

Lines changed: 55 additions & 25 deletions

File tree

  • samples
    • function-app-managed-identity/python/terraform
    • function-app-storage-http/dotnet/terraform
    • web-app-cosmosdb-mongodb-api/python/terraform
    • web-app-managed-identity/python/terraform
    • web-app-sql-database/python/terraform

samples/function-app-managed-identity/python/terraform/deploy.sh

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ export TF_LOG_PATH="$CURRENT_DIR/terraform-debug.log"
5757
echo "[DEBUG] Checking what tflocal does..."echo "[DEBUG] tflocal version: $($TERRAFORM version 2>&1 | head -1)"echo "[DEBUG] Contents of current directory before init:"ls -la . 2>&1 | head -20
5858
echo "[DEBUG] Terraform debug logging enabled: TF_LOG=DEBUG, TF_LOG_PATH=$TF_LOG_PATH"
5959

60+
# Clean up any existing Terraform state from previous runs
61+
if [[ $ENVIRONMENT == "LocalStack" ]]; then
62+
echo "Cleaning up any existing resources from previous runs..."
63+
if [ -f ".terraform/terraform.tfstate" ] || [ -f "terraform.tfstate" ]; then
64+
$TERRAFORM init -upgrade 2>/dev/null || true
65+
$TERRAFORM destroy -auto-approve 2>/dev/null || true
66+
rm -f terraform.tfstate* tfplan .terraform.lock.hcl 2>/dev/null || true
67+
rm -rf .terraform 2>/dev/null || true
68+
fi
69+
fi
70+
6071
echo "Initializing Terraform..."
6172
$TERRAFORM init -upgrade
6273

@@ -132,8 +143,3 @@ fi
132143
if [ -f "$ZIPFILE" ]; then
133144
rm "$ZIPFILE"
134145
fi
135-
136-
# Clean up Terraform resources
137-
echo "Cleaning up Terraform resources..."
138-
cd "$CURRENT_DIR" || exit
139-
$TERRAFORM destroy -auto-approve

samples/function-app-storage-http/dotnet/terraform/deploy.sh

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ export TF_LOG_PATH="$CURRENT_DIR/terraform-debug.log"
5656
echo "[DEBUG] Checking what tflocal does..."echo "[DEBUG] tflocal version: $($TERRAFORM version 2>&1 | head -1)"echo "[DEBUG] Contents of current directory before init:"ls -la . 2>&1 | head -20
5757
echo "[DEBUG] Terraform debug logging enabled: TF_LOG=DEBUG, TF_LOG_PATH=$TF_LOG_PATH"
5858

59+
# Clean up any existing Terraform state from previous runs
60+
if [[ $ENVIRONMENT == "LocalStack" ]]; then
61+
echo "Cleaning up any existing resources from previous runs..."
62+
if [ -f ".terraform/terraform.tfstate" ] || [ -f "terraform.tfstate" ]; then
63+
$TERRAFORM init -upgrade 2>/dev/null || true
64+
$TERRAFORM destroy -auto-approve 2>/dev/null || true
65+
rm -f terraform.tfstate* tfplan .terraform.lock.hcl 2>/dev/null || true
66+
rm -rf .terraform 2>/dev/null || true
67+
fi
68+
fi
69+
5970
echo "Initializing Terraform..."
6071
$TERRAFORM init -upgrade
6172

@@ -115,8 +126,3 @@ $AZ functionapp deploy \
115126
--name "$FUNCTION_APP_NAME" \
116127
--src-path $ZIPFILE \
117128
--type zip 1> /dev/null
118-
119-
# Clean up Terraform resources
120-
echo "Cleaning up Terraform resources..."
121-
cd "$CURRENT_DIR" || exit
122-
$TERRAFORM destroy -auto-approve

samples/web-app-cosmosdb-mongodb-api/python/terraform/deploy.sh

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ export TF_LOG_PATH="$CURRENT_DIR/terraform-debug.log"
5656
echo "[DEBUG] Checking what tflocal does..."echo "[DEBUG] tflocal version: $($TERRAFORM version 2>&1 | head -1)"echo "[DEBUG] Contents of current directory before init:"ls -la . 2>&1 | head -20
5757
echo "[DEBUG] Terraform debug logging enabled: TF_LOG=DEBUG, TF_LOG_PATH=$TF_LOG_PATH"
5858

59+
# Clean up any existing Terraform state from previous runs
60+
if [[ $ENVIRONMENT == "LocalStack" ]]; then
61+
echo "Cleaning up any existing resources from previous runs..."
62+
if [ -f ".terraform/terraform.tfstate" ] || [ -f "terraform.tfstate" ]; then
63+
$TERRAFORM init -upgrade 2>/dev/null || true
64+
$TERRAFORM destroy -auto-approve 2>/dev/null || true
65+
rm -f terraform.tfstate* tfplan .terraform.lock.hcl 2>/dev/null || true
66+
rm -rf .terraform 2>/dev/null || true
67+
fi
68+
fi
69+
5970
echo "Initializing Terraform..."
6071
$TERRAFORM init -upgrade
6172

@@ -118,8 +129,3 @@ $AZ webapp deploy \
118129
if [ -f "$ZIPFILE" ]; then
119130
rm "$ZIPFILE"
120131
fi
121-
122-
# Clean up Terraform resources
123-
echo "Cleaning up Terraform resources..."
124-
cd "$CURRENT_DIR" || exit
125-
$TERRAFORM destroy -auto-approve

samples/web-app-managed-identity/python/terraform/deploy.sh

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ export TF_LOG_PATH="$CURRENT_DIR/terraform-debug.log"
5757
echo "[DEBUG] Checking what tflocal does..."echo "[DEBUG] tflocal version: $($TERRAFORM version 2>&1 | head -1)"echo "[DEBUG] Contents of current directory before init:"ls -la . 2>&1 | head -20
5858
echo "[DEBUG] Terraform debug logging enabled: TF_LOG=DEBUG, TF_LOG_PATH=$TF_LOG_PATH"
5959

60+
# Clean up any existing Terraform state from previous runs
61+
if [[ $ENVIRONMENT == "LocalStack" ]]; then
62+
echo "Cleaning up any existing resources from previous runs..."
63+
if [ -f ".terraform/terraform.tfstate" ] || [ -f "terraform.tfstate" ]; then
64+
$TERRAFORM init -upgrade 2>/dev/null || true
65+
$TERRAFORM destroy -auto-approve 2>/dev/null || true
66+
rm -f terraform.tfstate* tfplan .terraform.lock.hcl 2>/dev/null || true
67+
rm -rf .terraform 2>/dev/null || true
68+
fi
69+
fi
70+
6071
echo "Initializing Terraform..."
6172
$TERRAFORM init -upgrade
6273

@@ -130,8 +141,3 @@ fi
130141
if [ -f "$ZIPFILE" ]; then
131142
rm "$ZIPFILE"
132143
fi
133-
134-
# Clean up Terraform resources
135-
echo "Cleaning up Terraform resources..."
136-
cd "$CURRENT_DIR" || exit
137-
$TERRAFORM destroy -auto-approve

samples/web-app-sql-database/python/terraform/deploy.sh

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ export TF_LOG_PATH="$CURRENT_DIR/terraform-debug.log"
6161
echo "[DEBUG] Checking what tflocal does..."echo "[DEBUG] tflocal version: $($TERRAFORM version 2>&1 | head -1)"echo "[DEBUG] Contents of current directory before init:"ls -la . 2>&1 | head -20
6262
echo "[DEBUG] Terraform debug logging enabled: TF_LOG=DEBUG, TF_LOG_PATH=$TF_LOG_PATH"
6363

64+
# Clean up any existing Terraform state from previous runs
65+
if [[ $ENVIRONMENT == "LocalStack" ]]; then
66+
echo "Cleaning up any existing resources from previous runs..."
67+
if [ -f ".terraform/terraform.tfstate" ] || [ -f "terraform.tfstate" ]; then
68+
$TERRAFORM init -upgrade 2>/dev/null || true
69+
$TERRAFORM destroy -auto-approve 2>/dev/null || true
70+
rm -f terraform.tfstate* tfplan .terraform.lock.hcl 2>/dev/null || true
71+
rm -rf .terraform 2>/dev/null || true
72+
fi
73+
fi
74+
6475
echo "Initializing Terraform..."
6576
$TERRAFORM init -upgrade
6677

@@ -288,8 +299,3 @@ fi
288299
if [ -f "$ZIPFILE" ]; then
289300
rm "$ZIPFILE"
290301
fi
291-
292-
# Clean up Terraform resources
293-
echo "Cleaning up Terraform resources..."
294-
cd "$CURRENT_DIR" || exit
295-
$TERRAFORM destroy -auto-approve

0 commit comments

Comments
 (0)