|
| 1 | +name: Deploy Staging |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [main] |
| 6 | + paths: |
| 7 | + - 'infra/**' |
| 8 | + - '.github/workflows/deploy-staging.yml' |
| 9 | + |
| 10 | +env: |
| 11 | + TF_VERSION: "1.8.5" |
| 12 | + AWS_REGION: "us-east-1" |
| 13 | + TF_WORKING_DIR: infra/envs/staging |
| 14 | + |
| 15 | +jobs: |
| 16 | + plan: |
| 17 | + name: Terraform Plan (staging) |
| 18 | + runs-on: ubuntu-latest |
| 19 | + environment: staging |
| 20 | + permissions: |
| 21 | + contents: read |
| 22 | + pull-requests: write |
| 23 | + |
| 24 | + steps: |
| 25 | + - uses: actions/checkout@v4 |
| 26 | + |
| 27 | + - uses: hashicorp/setup-terraform@v3 |
| 28 | + with: |
| 29 | + terraform_version: ${{ env.TF_VERSION }} |
| 30 | + |
| 31 | + - name: Configure AWS credentials |
| 32 | + uses: aws-actions/configure-aws-credentials@v4 |
| 33 | + with: |
| 34 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 35 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 36 | + aws-region: ${{ env.AWS_REGION }} |
| 37 | + |
| 38 | + - name: Terraform init |
| 39 | + run: terraform init -input=false -backend-config=../../environments/staging.backend.hcl |
| 40 | + working-directory: ${{ env.TF_WORKING_DIR }} |
| 41 | + |
| 42 | + - name: Terraform plan |
| 43 | + id: plan |
| 44 | + run: | |
| 45 | + terraform plan -no-color -input=false \ |
| 46 | + -var-file=../../environments/staging.tfvars \ |
| 47 | + -out=tfplan 2>&1 | tee plan.txt |
| 48 | + working-directory: ${{ env.TF_WORKING_DIR }} |
| 49 | + env: |
| 50 | + TF_VAR_acm_certificate_arn: ${{ vars.STAGING_ACM_CERT_ARN }} |
| 51 | + TF_VAR_vpc_id: ${{ secrets.STAGING_VPC_ID }} |
| 52 | + TF_VAR_private_subnet_ids: ${{ secrets.STAGING_PRIVATE_SUBNET_IDS }} |
| 53 | + TF_VAR_db_username: ${{ secrets.STAGING_DB_USERNAME }} |
| 54 | + TF_VAR_db_password: ${{ secrets.STAGING_DB_PASSWORD }} |
| 55 | + |
| 56 | + - name: Comment plan on PR |
| 57 | + uses: actions/github-script@v7 |
| 58 | + if: always() |
| 59 | + with: |
| 60 | + script: | |
| 61 | + const fs = require('fs'); |
| 62 | + const plan = fs.readFileSync('${{ env.TF_WORKING_DIR }}/plan.txt', 'utf8'); |
| 63 | + const truncated = plan.length > 60000 ? plan.slice(0, 60000) + '\n...(truncated)' : plan; |
| 64 | + github.rest.issues.createComment({ |
| 65 | + issue_number: context.issue.number, |
| 66 | + owner: context.repo.owner, |
| 67 | + repo: context.repo.repo, |
| 68 | + body: `## Terraform Plan — staging\n\`\`\`hcl\n${truncated}\n\`\`\`` |
| 69 | + }); |
0 commit comments