|
| 1 | +name: OpenTofu Tests, Plan & Apply |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - main |
| 10 | + |
| 11 | +env: |
| 12 | + SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }} |
| 13 | + |
| 14 | +jobs: |
| 15 | + test: |
| 16 | + name: Pre-commit Tests |
| 17 | + runs-on: ubuntu-latest |
| 18 | + container: |
| 19 | + image: ghcr.io/makeitworkcloud/runner:latest |
| 20 | + steps: |
| 21 | + - name: Checkout |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + fetch-depth: 0 |
| 25 | + |
| 26 | + - name: Initialize OpenTofu |
| 27 | + run: tofu init -backend=false |
| 28 | + |
| 29 | + - name: Run tests |
| 30 | + run: make test |
| 31 | + |
| 32 | + - name: Show README.md changes after pre-commit |
| 33 | + run: | |
| 34 | + echo "=== Git status after pre-commit ===" |
| 35 | + git status --porcelain |
| 36 | + echo "=== Git diff after pre-commit ===" |
| 37 | + git diff HEAD |
| 38 | + echo "=== README.md content after pre-commit ===" |
| 39 | + cat README.md | head -50 |
| 40 | +
|
| 41 | + plan: |
| 42 | + name: OpenTofu Plan |
| 43 | + runs-on: ubuntu-latest |
| 44 | + container: |
| 45 | + image: ghcr.io/makeitworkcloud/runner:latest |
| 46 | + if: github.event_name == 'pull_request' |
| 47 | + needs: [test] |
| 48 | + steps: |
| 49 | + - name: Checkout |
| 50 | + uses: actions/checkout@v4 |
| 51 | + |
| 52 | + - name: OpenTofu Plan |
| 53 | + id: plan |
| 54 | + run: | |
| 55 | + # Run make plan - Makefile will handle writing plan to file |
| 56 | + make plan || true |
| 57 | +
|
| 58 | + # Extract only the plan summary - what will actually change |
| 59 | + # Start from "OpenTofu will perform" and take everything after |
| 60 | + sed -n '/OpenTofu will perform the following actions:/,$p' plan-output.txt > plan-filtered.txt |
| 61 | +
|
| 62 | + # If no changes, look for "No changes" message |
| 63 | + if [ ! -s plan-filtered.txt ]; then |
| 64 | + grep -A 2 "No changes" plan-output.txt > plan-filtered.txt || echo "No plan output found" > plan-filtered.txt |
| 65 | + fi |
| 66 | +
|
| 67 | + # Limit output to last 1000 lines to prevent "Argument list too long" error |
| 68 | + # The plan summary with actual changes is at the end, that's what matters |
| 69 | + tail -n 1000 plan-filtered.txt > plan-filtered-truncated.txt |
| 70 | + mv plan-filtered-truncated.txt plan-filtered.txt |
| 71 | +
|
| 72 | + - name: Comment PR with Plan |
| 73 | + uses: actions/github-script@v7 |
| 74 | + if: github.event_name == 'pull_request' |
| 75 | + with: |
| 76 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 77 | + script: | |
| 78 | + const fs = require('fs'); |
| 79 | + const planOutput = fs.readFileSync('plan-filtered.txt', 'utf8'); |
| 80 | +
|
| 81 | + const output = `#### OpenTofu Plan 📋 |
| 82 | + \`\`\` |
| 83 | + ${planOutput} |
| 84 | + \`\`\` |
| 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 | + apply: |
| 94 | + name: OpenTofu Apply |
| 95 | + runs-on: ubuntu-latest |
| 96 | + container: |
| 97 | + image: ghcr.io/makeitworkcloud/runner:latest |
| 98 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 99 | + needs: [test] |
| 100 | + environment: production |
| 101 | + steps: |
| 102 | + - name: Checkout |
| 103 | + uses: actions/checkout@v4 |
| 104 | + |
| 105 | + - name: OpenTofu Apply |
| 106 | + run: make apply |
0 commit comments