|
| 1 | +name: VPC Demo - Terraform Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - 'vpc-demo/**' |
| 9 | + pull_request: |
| 10 | + branches: |
| 11 | + - main |
| 12 | + paths: |
| 13 | + - 'vpc-demo/**' |
| 14 | + workflow_dispatch: |
| 15 | + |
| 16 | +permissions: |
| 17 | + id-token: write |
| 18 | + contents: read |
| 19 | + pull-requests: write |
| 20 | + |
| 21 | +jobs: |
| 22 | + terraform: |
| 23 | + name: Terraform |
| 24 | + runs-on: ubuntu-latest |
| 25 | + defaults: |
| 26 | + run: |
| 27 | + working-directory: vpc-demo |
| 28 | + |
| 29 | + steps: |
| 30 | + - name: Checkout |
| 31 | + uses: actions/checkout@v4 |
| 32 | + |
| 33 | + - name: Configure AWS Credentials |
| 34 | + uses: aws-actions/configure-aws-credentials@v4 |
| 35 | + with: |
| 36 | + role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/github-actions-role |
| 37 | + aws-region: eu-west-3 |
| 38 | + |
| 39 | + - name: Setup Terraform |
| 40 | + uses: hashicorp/setup-terraform@v3 |
| 41 | + with: |
| 42 | + terraform_version: 1.13.5 |
| 43 | + |
| 44 | + - name: Terraform Init |
| 45 | + run: terraform init |
| 46 | + |
| 47 | + - name: Terraform Format |
| 48 | + run: terraform fmt -check |
| 49 | + continue-on-error: true |
| 50 | + |
| 51 | + - name: Terraform Validate |
| 52 | + run: terraform validate |
| 53 | + |
| 54 | + - name: Terraform Plan |
| 55 | + id: plan |
| 56 | + run: terraform plan -no-color -var="aws_profile=" -out=tfplan |
| 57 | + continue-on-error: true |
| 58 | + |
| 59 | + - name: Comment PR |
| 60 | + if: github.event_name == 'pull_request' |
| 61 | + uses: actions/github-script@v7 |
| 62 | + with: |
| 63 | + script: | |
| 64 | + const output = `#### Terraform Plan 📖\`${{ steps.plan.outcome }}\` |
| 65 | +
|
| 66 | + <details><summary>Show Plan</summary> |
| 67 | +
|
| 68 | + \`\`\`terraform |
| 69 | + ${{ steps.plan.outputs.stdout }} |
| 70 | + \`\`\` |
| 71 | +
|
| 72 | + </details>`; |
| 73 | +
|
| 74 | + github.rest.issues.createComment({ |
| 75 | + issue_number: context.issue.number, |
| 76 | + owner: context.repo.owner, |
| 77 | + repo: context.repo.repo, |
| 78 | + body: output |
| 79 | + }) |
| 80 | +
|
| 81 | + - name: Terraform Apply |
| 82 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 83 | + run: terraform apply -auto-approve -var="aws_profile=" tfplan |
0 commit comments