Tested and fixed tfstate store #7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Terraform Deploy | |
| on: | |
| push: | |
| branches: | |
| - staging | |
| - main | |
| workflow_dispatch: | |
| env: | |
| TF_WORKING_DIR: terraform/ | |
| jobs: | |
| terraform: | |
| name: Terraform (plan & apply) | |
| runs-on: ubuntu-latest | |
| environment: ${{ github.ref == 'refs/heads/main' && 'prod' || 'staging' }} | |
| permissions: | |
| contents: read | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Terraform | |
| uses: hashicorp/setup-terraform@v2 | |
| with: | |
| terraform_version: 1.5.6 | |
| - name: Configure AWS Credentials | |
| uses: aws-actions/configure-aws-credentials@v2 | |
| with: | |
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| aws-region: ${{ secrets.AWS_REGION }} | |
| - name: Terraform Init | |
| working-directory: ${{ env.TF_WORKING_DIR }} | |
| run: terraform init -input=false | |
| - name: Terraform Validate & Format | |
| working-directory: ${{ env.TF_WORKING_DIR }} | |
| run: | | |
| terraform fmt -check | |
| terraform validate | |
| - name: Terraform Plan | |
| id: plan | |
| working-directory: ${{ env.TF_WORKING_DIR }} | |
| run: | | |
| if [ "${{ github.ref }}" = "refs/heads/staging" ]; then | |
| terraform plan -var-file="staging.tfvars" -out=tfplan | |
| else | |
| terraform plan -var-file="prod.tfvars" -out=tfplan | |
| fi | |
| - name: Terraform Apply | |
| if: github.ref == 'refs/heads/staging' | |
| working-directory: ${{ env.TF_WORKING_DIR }} | |
| run: terraform apply -input=false -auto-approve tfplan | |
| - name: Terraform Apply (prod) - requires env approval | |
| if: github.ref == 'refs/heads/main' | |
| working-directory: ${{ env.TF_WORKING_DIR }} | |
| run: terraform apply -input=false -auto-approve tfplan | |
| - name: Show outputs | |
| if: success() | |
| working-directory: ${{ env.TF_WORKING_DIR }} | |
| run: terraform output -json |