|
| 1 | +name: OpenTofu |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + environment: |
| 7 | + description: Environment for apply job |
| 8 | + type: string |
| 9 | + default: production |
| 10 | + secrets: |
| 11 | + SOPS_AGE_KEY: |
| 12 | + required: true |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: read |
| 16 | + pull-requests: write |
| 17 | + |
| 18 | +jobs: |
| 19 | + test: |
| 20 | + name: Pre-commit Tests |
| 21 | + runs-on: ubuntu-latest |
| 22 | + container: |
| 23 | + image: ghcr.io/makeitworkcloud/runner:latest |
| 24 | + steps: |
| 25 | + - name: Checkout |
| 26 | + uses: actions/checkout@v4 |
| 27 | + with: |
| 28 | + fetch-depth: 0 |
| 29 | + |
| 30 | + - name: Initialize OpenTofu |
| 31 | + run: tofu init -backend=false |
| 32 | + |
| 33 | + - name: Run tests |
| 34 | + run: make test |
| 35 | + |
| 36 | + plan: |
| 37 | + name: OpenTofu Plan |
| 38 | + runs-on: ubuntu-latest |
| 39 | + container: |
| 40 | + image: ghcr.io/makeitworkcloud/runner:latest |
| 41 | + if: github.event_name == 'pull_request' |
| 42 | + needs: [test] |
| 43 | + env: |
| 44 | + SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }} |
| 45 | + steps: |
| 46 | + - name: Checkout |
| 47 | + uses: actions/checkout@v4 |
| 48 | + |
| 49 | + - name: OpenTofu Plan |
| 50 | + id: plan |
| 51 | + run: | |
| 52 | + make plan || true |
| 53 | +
|
| 54 | + sed -n '/OpenTofu will perform the following actions:/,$p' plan-output.txt > plan-filtered.txt |
| 55 | +
|
| 56 | + if [ ! -s plan-filtered.txt ]; then |
| 57 | + grep -A 2 "No changes" plan-output.txt > plan-filtered.txt || echo "No plan output found" > plan-filtered.txt |
| 58 | + fi |
| 59 | +
|
| 60 | + tail -n 1000 plan-filtered.txt > plan-filtered-truncated.txt |
| 61 | + mv plan-filtered-truncated.txt plan-filtered.txt |
| 62 | +
|
| 63 | + - name: Comment PR with Plan |
| 64 | + uses: actions/github-script@v7 |
| 65 | + with: |
| 66 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 67 | + script: | |
| 68 | + const fs = require('fs'); |
| 69 | + const planOutput = fs.readFileSync('plan-filtered.txt', 'utf8'); |
| 70 | +
|
| 71 | + const output = `#### OpenTofu Plan |
| 72 | + \`\`\` |
| 73 | + ${planOutput} |
| 74 | + \`\`\` |
| 75 | + `; |
| 76 | + github.rest.issues.createComment({ |
| 77 | + issue_number: context.issue.number, |
| 78 | + owner: context.repo.owner, |
| 79 | + repo: context.repo.repo, |
| 80 | + body: output |
| 81 | + }); |
| 82 | +
|
| 83 | + apply: |
| 84 | + name: OpenTofu Apply |
| 85 | + runs-on: ubuntu-latest |
| 86 | + container: |
| 87 | + image: ghcr.io/makeitworkcloud/runner:latest |
| 88 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 89 | + needs: [test] |
| 90 | + environment: ${{ inputs.environment }} |
| 91 | + env: |
| 92 | + SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }} |
| 93 | + steps: |
| 94 | + - name: Checkout |
| 95 | + uses: actions/checkout@v4 |
| 96 | + |
| 97 | + - name: OpenTofu Apply |
| 98 | + run: make apply |
0 commit comments