Skip to content

Commit 4a2d9d9

Browse files
committed
Improved terraform state handling and preview envs
1 parent 06f8899 commit 4a2d9d9

3 files changed

Lines changed: 62 additions & 67 deletions

File tree

.github/workflows/ci.yml

Lines changed: 24 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -184,93 +184,53 @@ jobs:
184184
with:
185185
submodules: recursive
186186

187-
- name: Cache Terraform state
188-
uses: actions/cache@v4
189-
with:
190-
path: ./terraform/terraform.tfstate
191-
key: terraform-state-${{ github.ref }}
192-
restore-keys: |
193-
terraform-state-
194-
195187
- name: Setup Terraform
196188
uses: hashicorp/setup-terraform@v3
197189
with:
198190
terraform_version: latest
199191

192+
- name: Extract branch name
193+
id: extract_branch
194+
run: |
195+
# Extract branch name from ref (remove refs/heads/ prefix)
196+
BRANCH_NAME="${GITHUB_REF#refs/heads/}"
197+
# For PRs, use the head branch
198+
if [ "${{ github.event_name }}" = "pull_request" ]; then
199+
BRANCH_NAME="${{ github.head_ref }}"
200+
fi
201+
# Sanitize branch name (replace / with -)
202+
BRANCH_NAME=$(echo "$BRANCH_NAME" | sed 's/\//-/g')
203+
echo "branch=${BRANCH_NAME}" >> $GITHUB_OUTPUT
204+
echo "Deploying environment: ${BRANCH_NAME}"
205+
200206
- name: Terraform Init
201207
working-directory: ./terraform
202-
run: terraform init
208+
run: |
209+
BRANCH_NAME="${{ steps.extract_branch.outputs.branch }}"
210+
echo "Using state file: simple-ci-${BRANCH_NAME}/terraform.tfstate"
211+
terraform init \
212+
-backend-config="access_key=${{ secrets.S3_ACCESS_KEY }}" \
213+
-backend-config="secret_key=${{ secrets.S3_SECRET_KEY }}" \
214+
-backend-config="key=simple-ci-${BRANCH_NAME}/terraform.tfstate"
203215
204216
- name: Terraform Validate
205217
working-directory: ./terraform
206218
run: terraform validate
207219

208-
- name: Remove container from terraform.tfstate
209-
working-directory: ./terraform
210-
run: |
211-
# Check if state file exists and if resources exist in state
212-
if [ -f "terraform.tfstate" ]; then
213-
echo "State file found, checking for existing resources..."
214-
215-
# Remove namespace if it exists in state
216-
if terraform state list | grep -q "nexaa_namespace.simple-ci"; then
217-
echo "Namespace resource found in state, removing..."
218-
terraform state rm nexaa_namespace.simple-ci
219-
else
220-
echo "Namespace resource not found in state, skipping removal"
221-
fi
222-
223-
# Remove container if it exists in state
224-
if terraform state list | grep -q "nexaa_container.simple-ci"; then
225-
echo "Container resource found in state, removing..."
226-
terraform state rm nexaa_container.simple-ci
227-
else
228-
echo "Container resource not found in state, skipping removal"
229-
fi
230-
else
231-
echo "No state file found, skipping removal"
232-
fi
233-
env:
234-
TF_VAR_nexaa_username: ${{ secrets.NEXAA_USERNAME }}
235-
TF_VAR_nexaa_password: ${{ secrets.NEXAA_PASSWORD }}
236-
237-
- name: Terraform Import
238-
working-directory: ./terraform
239-
run: |
240-
# Try to import the namespace resource first
241-
echo "Attempting to import namespace resource..."
242-
if terraform import nexaa_namespace.simple-ci simple-ci; then
243-
echo "✅ Namespace resource imported successfully"
244-
else
245-
echo "⚠️ Namespace resource import failed (resource may not exist remotely)"
246-
echo "This is normal for first deployments or if the namespace was deleted"
247-
echo "Terraform will create the resource during apply"
248-
fi
249-
250-
# Try to import the container resource
251-
echo "Attempting to import container resource..."
252-
if terraform import nexaa_container.simple-ci simple-ci/simple-ci; then
253-
echo "✅ Container resource imported successfully"
254-
else
255-
echo "⚠️ Container resource import failed (resource may not exist remotely)"
256-
echo "This is normal for first deployments or if the container was deleted"
257-
echo "Terraform will create the resource during apply"
258-
fi
259-
env:
260-
TF_VAR_nexaa_username: ${{ secrets.NEXAA_USERNAME }}
261-
TF_VAR_nexaa_password: ${{ secrets.NEXAA_PASSWORD }}
262-
263220
- name: Terraform Plan
264221
working-directory: ./terraform
265222
run: terraform plan
266223
env:
267224
TF_VAR_nexaa_username: ${{ secrets.NEXAA_USERNAME }}
268225
TF_VAR_nexaa_password: ${{ secrets.NEXAA_PASSWORD }}
226+
TF_VAR_environment: ${{ steps.extract_branch.outputs.branch }}
227+
TF_VAR_container_image: ${{ needs.build.outputs.image }}
269228

270229
- name: Terraform Apply
271230
working-directory: ./terraform
272231
run: terraform apply -auto-approve
273232
env:
274233
TF_VAR_nexaa_username: ${{ secrets.NEXAA_USERNAME }}
275234
TF_VAR_nexaa_password: ${{ secrets.NEXAA_PASSWORD }}
235+
TF_VAR_environment: ${{ steps.extract_branch.outputs.branch }}
276236
TF_VAR_container_image: ${{ needs.build.outputs.image }}

terraform/main.tf

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@ terraform {
66
version = "0.1.27"
77
}
88
}
9+
10+
backend "s3" {
11+
bucket = "terraform-bucket"
12+
key = "simple-ci/terraform.tfstate"
13+
region = "eu-west-1"
14+
15+
endpoints = {
16+
s3 = "https://101010-rockstar-demo-minio.container.tilaa.cloud"
17+
}
18+
19+
skip_credentials_validation = true
20+
skip_metadata_api_check = true
21+
skip_region_validation = true
22+
skip_requesting_account_id = true
23+
use_path_style = true
24+
}
25+
926
}
1027

1128
provider "nexaa" {
@@ -19,11 +36,11 @@ data "nexaa_container_resources" "container_resource" {
1936
}
2037

2138
resource "nexaa_namespace" "simple-ci" {
22-
name = "simple-ci"
39+
name = "simple-ci-${var.environment}"
2340
}
2441

2542
resource "nexaa_container" "simple-ci" {
26-
name = "simple-ci"
43+
name = "simple-ci-${var.environment}"
2744
namespace = nexaa_namespace.simple-ci.name
2845
image = var.container_image
2946

@@ -33,7 +50,7 @@ resource "nexaa_container" "simple-ci" {
3350

3451
ingresses = [
3552
{
36-
domain_name = "simple-ci.nexaa.io"
53+
domain_name = "simple-ci-${var.environment}.nexaa.io"
3754
port = 3000
3855
tls = true
3956
}

terraform/variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,22 @@ variable "container_image" {
1414
description = "Docker image to deploy"
1515
type = string
1616
default = "ghcr.io/nexaa-cloud/simple-ci:latest"
17+
}
18+
19+
variable "environment" {
20+
description = "Environment name (e.g., branch name for preview environments)"
21+
type = string
22+
default = "main"
23+
}
24+
25+
variable "s3_access_key" {
26+
description = "S3 backend access key"
27+
type = string
28+
sensitive = true
29+
}
30+
31+
variable "s3_secret_key" {
32+
description = "S3 backend secret key"
33+
type = string
34+
sensitive = true
1735
}

0 commit comments

Comments
 (0)