|
| 1 | +name: OpenTofu |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + pull_request: |
| 7 | + |
| 8 | +defaults: |
| 9 | + run: |
| 10 | + shell: bash |
| 11 | + |
| 12 | +permissions: |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | +concurrency: |
| 16 | + group: ci-${{ github.ref }} |
| 17 | + cancel-in-progress: true |
| 18 | + |
| 19 | +env: |
| 20 | + TF_VAR_fastly_api_key: ${{ secrets.fastly_api_key }} |
| 21 | + TF_VAR_porkbun_api_key: ${{ secrets.porkbun_api_key }} |
| 22 | + TF_VAR_porkbun_secret_key: ${{ secrets.porkbun_secret_key }} |
| 23 | + AWS_ACCESS_KEY_ID: ${{ secrets.aws_access_key_id }} |
| 24 | + AWS_SECRET_ACCESS_KEY: ${{ secrets.aws_secret_access_key }} |
| 25 | + |
| 26 | +jobs: |
| 27 | + validate: |
| 28 | + name: "Validate" |
| 29 | + runs-on: ubuntu-latest |
| 30 | + steps: |
| 31 | + - uses: actions/checkout@v5 |
| 32 | + - uses: opentofu/setup-opentofu@v1 |
| 33 | + with: |
| 34 | + tofu_version_file: .opentofu-version |
| 35 | + - name: Tofu Validate |
| 36 | + run: tofu validate -no-color |
| 37 | + - name: Tofu Format Check |
| 38 | + run: tofu fmt -check |
| 39 | + |
| 40 | + plan: |
| 41 | + name: "Plan" |
| 42 | + runs-on: ubuntu-latest |
| 43 | + steps: |
| 44 | + - uses: actions/checkout@v5 |
| 45 | + - uses: opentofu/setup-opentofu@v1 |
| 46 | + with: |
| 47 | + tofu_version_file: .opentofu-version |
| 48 | + - name: Tofu Init (Staging) |
| 49 | + run: tofu init -var-file="env-staging.tfvars" -input=false |
| 50 | + - name: Tofu Plan (Staging) |
| 51 | + run: | |
| 52 | + { PLAN=$(tofu plan -var-file="env-staging.tfvars" -no-color | tee /dev/fd/5 || true); } 5>&1 |
| 53 | + echo "<details><summary>OpenTofu Plan (Staging)</summary><code>$PLAN</code></details>" >> $GITHUB_STEP_SUMMARY |
| 54 | + continue-on-error: true |
| 55 | + - name: Tofu Init (Prod) |
| 56 | + run: tofu init -var-file="env-prod.tfvars" -input=false |
| 57 | + - name: Tofu Plan (Prod) |
| 58 | + run: | |
| 59 | + { PLAN=$(tofu plan -var-file="env-prod.tfvars" -no-color | tee /dev/fd/5 || true); } 5>&1 |
| 60 | + echo "<details><summary>OpenTofu Plan (Prod)</summary><code>$PLAN</code></details>" >> $GITHUB_STEP_SUMMARY |
| 61 | +
|
| 62 | + deploy-staging: |
| 63 | + name: "Deploy (Staging)" |
| 64 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 65 | + environment: staging |
| 66 | + needs: [validate, plan] |
| 67 | + runs-on: ubuntu-latest |
| 68 | + steps: |
| 69 | + - uses: actions/checkout@v5 |
| 70 | + - uses: opentofu/setup-opentofu@v1 |
| 71 | + with: |
| 72 | + tofu_version_file: .opentofu-version |
| 73 | + - name: Tofu Init |
| 74 | + run: tofu init -var-file="env-staging.tfvars" |
| 75 | + - name: Tofu Apply |
| 76 | + run: tofu apply -var-file="env-staging.tfvars" -auto-approve |
| 77 | + |
| 78 | + deploy-prod: |
| 79 | + name: "Deploy (Prod)" |
| 80 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 81 | + environment: prod |
| 82 | + needs: [deploy-staging] |
| 83 | + runs-on: ubuntu-latest |
| 84 | + steps: |
| 85 | + - uses: actions/checkout@v5 |
| 86 | + - uses: opentofu/setup-opentofu@v1 |
| 87 | + with: |
| 88 | + tofu_version_file: .opentofu-version |
| 89 | + - name: Tofu Init |
| 90 | + run: tofu init -var-file="env-prod.tfvars" |
| 91 | + - name: Tofu Apply |
| 92 | + run: tofu apply -var-file="env-prod.tfvars" -auto-approve |
0 commit comments