|
| 1 | +name: Terraform |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + paths: [tf/**] |
| 7 | + pull_request: |
| 8 | + branches: [main] |
| 9 | + paths: [tf/**] |
| 10 | + |
| 11 | +concurrency: |
| 12 | + group: terraform |
| 13 | + cancel-in-progress: false |
| 14 | + |
| 15 | +env: |
| 16 | + AWS_ACCESS_KEY_ID: ${{ secrets.WASABI_ACCESS_KEY }} |
| 17 | + AWS_SECRET_ACCESS_KEY: ${{ secrets.WASABI_SECRET_KEY }} |
| 18 | + |
| 19 | +jobs: |
| 20 | + terraform-plan: |
| 21 | + name: Terraform Plan |
| 22 | + runs-on: ubuntu-latest |
| 23 | + outputs: |
| 24 | + tfplanExitCode: ${{ steps.tf-plan.outputs.exitcode }} |
| 25 | + |
| 26 | + steps: |
| 27 | + - uses: actions/checkout@v4 |
| 28 | + |
| 29 | + - uses: hashicorp/setup-terraform@v3 |
| 30 | + with: |
| 31 | + terraform_wrapper: false |
| 32 | + |
| 33 | + - name: Terraform Init |
| 34 | + working-directory: ./tf |
| 35 | + run: terraform init |
| 36 | + |
| 37 | + - name: Terraform Format |
| 38 | + working-directory: ./tf |
| 39 | + run: terraform fmt -check |
| 40 | + |
| 41 | + - name: Terraform Plan |
| 42 | + id: tf-plan |
| 43 | + working-directory: ./tf |
| 44 | + env: |
| 45 | + TF_VAR_hcloud_token: ${{ secrets.HCLOUD_TOKEN }} |
| 46 | + TF_VAR_cloudflare_api_token: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 47 | + TF_VAR_cloudflare_zone_id: ${{ secrets.CLOUDFLARE_ZONE_ID }} |
| 48 | + TF_VAR_ssh_public_key: ${{ secrets.SSH_PUBLIC_KEY }} |
| 49 | + run: | |
| 50 | + export exitcode=0 |
| 51 | + terraform plan \ |
| 52 | + -lock-timeout=5m \ |
| 53 | + -detailed-exitcode \ |
| 54 | + -no-color \ |
| 55 | + -out tfplan \ |
| 56 | + || export exitcode=$? |
| 57 | +
|
| 58 | + echo "exitcode=$exitcode" >> "$GITHUB_OUTPUT" |
| 59 | +
|
| 60 | + if [ "$exitcode" -eq 1 ]; then |
| 61 | + echo "Terraform Plan Failed!" |
| 62 | + exit 1 |
| 63 | + else |
| 64 | + exit 0 |
| 65 | + fi |
| 66 | +
|
| 67 | + - uses: actions/upload-artifact@v4 |
| 68 | + with: |
| 69 | + name: tfplan |
| 70 | + path: tf/tfplan |
| 71 | + |
| 72 | + - name: Create Plan Summary |
| 73 | + id: tf-plan-string |
| 74 | + working-directory: ./tf |
| 75 | + run: | |
| 76 | + TERRAFORM_PLAN=$(terraform show -no-color tfplan) |
| 77 | +
|
| 78 | + delimiter="$(openssl rand -hex 8)" |
| 79 | + { |
| 80 | + echo "summary<<${delimiter}" |
| 81 | + echo "## Terraform Plan Output" |
| 82 | + echo "<details><summary>Click to expand</summary>" |
| 83 | + echo "" |
| 84 | + echo '```terraform' |
| 85 | + echo "$TERRAFORM_PLAN" |
| 86 | + echo '```' |
| 87 | + echo "</details>" |
| 88 | + echo "${delimiter}" |
| 89 | + } >> "$GITHUB_OUTPUT" |
| 90 | +
|
| 91 | + - name: Publish Plan to Summary |
| 92 | + env: |
| 93 | + SUMMARY: ${{ steps.tf-plan-string.outputs.summary }} |
| 94 | + run: echo "$SUMMARY" >> "$GITHUB_STEP_SUMMARY" |
| 95 | + |
| 96 | + - name: Comment Plan on PR |
| 97 | + if: github.event_name == 'pull_request' |
| 98 | + uses: actions/github-script@v7 |
| 99 | + env: |
| 100 | + SUMMARY: "${{ steps.tf-plan-string.outputs.summary }}" |
| 101 | + with: |
| 102 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 103 | + script: | |
| 104 | + const body = process.env.SUMMARY; |
| 105 | + github.rest.issues.createComment({ |
| 106 | + issue_number: context.issue.number, |
| 107 | + owner: context.repo.owner, |
| 108 | + repo: context.repo.repo, |
| 109 | + body: body |
| 110 | + }) |
| 111 | +
|
| 112 | + terraform-apply: |
| 113 | + name: Terraform Apply |
| 114 | + if: github.ref == 'refs/heads/main' && needs.terraform-plan.outputs.tfplanExitCode == 2 |
| 115 | + runs-on: ubuntu-latest |
| 116 | + needs: [terraform-plan] |
| 117 | + |
| 118 | + steps: |
| 119 | + - uses: actions/checkout@v4 |
| 120 | + |
| 121 | + - uses: hashicorp/setup-terraform@v3 |
| 122 | + |
| 123 | + - name: Terraform Init |
| 124 | + working-directory: ./tf |
| 125 | + run: terraform init |
| 126 | + |
| 127 | + - uses: actions/download-artifact@v4 |
| 128 | + with: |
| 129 | + name: tfplan |
| 130 | + path: tf |
| 131 | + |
| 132 | + - name: Terraform Apply |
| 133 | + working-directory: ./tf |
| 134 | + run: terraform apply -auto-approve -lock-timeout=5m tfplan |
0 commit comments