|
| 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: Setup Python |
| 34 | + uses: actions/setup-python@v5 |
| 35 | + with: |
| 36 | + python-version: '3.12' |
| 37 | + |
| 38 | + - name: Setup Terraform |
| 39 | + uses: hashicorp/setup-terraform@v3 |
| 40 | + with: |
| 41 | + terraform_version: 1.13.5 |
| 42 | + terraform_wrapper: false |
| 43 | + |
| 44 | + - name: Install terraform-docs |
| 45 | + run: | |
| 46 | + curl -sSLo /tmp/terraform-docs.tar.gz https://github.com/terraform-docs/terraform-docs/releases/download/v0.19.0/terraform-docs-v0.19.0-linux-amd64.tar.gz |
| 47 | + tar -xzf /tmp/terraform-docs.tar.gz -C /tmp |
| 48 | + sudo mv /tmp/terraform-docs /usr/local/bin/ |
| 49 | +
|
| 50 | + - name: Install trivy |
| 51 | + run: | |
| 52 | + curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin |
| 53 | +
|
| 54 | + - name: Run pre-commit |
| 55 | + uses: pre-commit/[email protected] |
| 56 | + |
| 57 | + - name: Configure AWS Credentials |
| 58 | + uses: aws-actions/configure-aws-credentials@v4 |
| 59 | + with: |
| 60 | + role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/github-actions-role |
| 61 | + aws-region: eu-west-3 |
| 62 | + |
| 63 | + - name: Terraform Init |
| 64 | + run: terraform init |
| 65 | + |
| 66 | + - name: Terraform Plan |
| 67 | + id: plan |
| 68 | + run: terraform plan -no-color -var="aws_profile=" -out=tfplan |
| 69 | + continue-on-error: true |
| 70 | + |
| 71 | + - name: Comment PR |
| 72 | + if: github.event_name == 'pull_request' |
| 73 | + uses: actions/github-script@v7 |
| 74 | + with: |
| 75 | + script: | |
| 76 | + const output = `#### Terraform Plan 📖\`${{ steps.plan.outcome }}\` |
| 77 | +
|
| 78 | + <details><summary>Show Plan</summary> |
| 79 | +
|
| 80 | + \`\`\`terraform |
| 81 | + ${{ steps.plan.outputs.stdout }} |
| 82 | + \`\`\` |
| 83 | +
|
| 84 | + </details>`; |
| 85 | +
|
| 86 | + github.rest.issues.createComment({ |
| 87 | + issue_number: context.issue.number, |
| 88 | + owner: context.repo.owner, |
| 89 | + repo: context.repo.repo, |
| 90 | + body: output |
| 91 | + }) |
| 92 | +
|
| 93 | + - name: Terraform Apply |
| 94 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 95 | + run: terraform apply -auto-approve -var="aws_profile=" tfplan |
0 commit comments