@@ -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 }}
0 commit comments