|
| 1 | +name: "Terraform Plan" |
| 2 | +description: "Run a Terraform plan" |
| 3 | + |
| 4 | +inputs: |
| 5 | + environment: |
| 6 | + description: "The environment to deploy to" |
| 7 | + required: true |
| 8 | + terraform-path: |
| 9 | + description: "The path passed to Terraform e.g. -chdir=<terraform-path>" |
| 10 | + required: false |
| 11 | + default: 'terraform' |
| 12 | + github-token: |
| 13 | + description: "The GitHub token, needed to update PRs" |
| 14 | + required: false |
| 15 | + default: '' |
| 16 | + upload-plan-file: |
| 17 | + description: "Upload the plan file to the GitHub artifacts" |
| 18 | + required: false |
| 19 | + default: 'true' |
| 20 | + upload-output-file: |
| 21 | + description: "Upload the plan output to the GitHub artifacts" |
| 22 | + required: false |
| 23 | + default: 'true' |
| 24 | + add-output-to-pr: |
| 25 | + description: "Add the plan output to the current PR if there is one" |
| 26 | + required: false |
| 27 | + default: 'true' |
| 28 | + |
| 29 | +outputs: |
| 30 | + plan-file: |
| 31 | + description: "Plan File" |
| 32 | + value: ${{ steps.plan.outputs.plan-file }} |
| 33 | + output-file: |
| 34 | + description: "Human Readable plan of action" |
| 35 | + value: ${{ steps.plan.outputs.output-file }} |
| 36 | + plan: |
| 37 | + description: "Human Readable plan of action" |
| 38 | + value: ${{ steps.plan.outputs.stdout }} |
| 39 | + |
| 40 | +runs: |
| 41 | + using: 'composite' |
| 42 | + steps: |
| 43 | + - name: Terraform Plan |
| 44 | + id: plan |
| 45 | + shell: bash |
| 46 | + env: |
| 47 | + TF_WORKSPACE: ${{ inputs.environment }} |
| 48 | + run: | |
| 49 | + terraform -chdir=${{ inputs.terraform-path }} plan -var-file="vars/${{ inputs.environment }}.tfvars" -no-color -out=/tmp/plan.tfplan |
| 50 | + echo "plan-file=/tmp/plan.tfplan" >> $GITHUB_OUTPUT |
| 51 | + terraform -chdir=${{ inputs.terraform-path }} show -no-color /tmp/plan.tfplan > /tmp/plan.txt |
| 52 | + echo "output-file=/tmp/plan.txt" >> $GITHUB_OUTPUT |
| 53 | +
|
| 54 | + - uses: actions/upload-artifact@v3 |
| 55 | + if: ${{ inputs.upload-plan-file == 'true' }} |
| 56 | + with: |
| 57 | + name: plan.tfplan |
| 58 | + path: ${{ steps.plan.outputs.plan-file }} |
| 59 | + |
| 60 | + - uses: actions/upload-artifact@v3 |
| 61 | + if: ${{ inputs.upload-output-file == 'true' }} |
| 62 | + with: |
| 63 | + name: plan.txt |
| 64 | + path: ${{ steps.plan.outputs.output-file }} |
| 65 | + |
| 66 | + - name: Add Plan to PR |
| 67 | + if: ${{ github.event_name == 'pull_request' || inputs.add-output-to-pr == 'true' }} |
| 68 | + uses: actions/github-script@v6 |
| 69 | + env: |
| 70 | + PLAN_FILE: ${{ steps.plan.outputs.output-file }} |
| 71 | + with: |
| 72 | + github-token: ${{ inputs.github-token }} |
| 73 | + script: | |
| 74 | + const { promises: fs } = require('fs'); |
| 75 | +
|
| 76 | + const MAX_LENGTH = 65535; |
| 77 | + const ellipsis = `\n...\n`; |
| 78 | +
|
| 79 | + const prefix = `<details><summary>Show Plan</summary> |
| 80 | +
|
| 81 | + \`\`\`\n |
| 82 | + `; |
| 83 | + const postfix = ` |
| 84 | + \`\`\` |
| 85 | +
|
| 86 | + </details> |
| 87 | +
|
| 88 | + *Action: \`${{ github.event_name }}\`*`; |
| 89 | +
|
| 90 | + let content = await fs.readFile(process.env.PLAN_FILE, 'utf8') |
| 91 | + let output = prefix + content + postfix; |
| 92 | +
|
| 93 | + if (output.length > MAX_LENGTH) { |
| 94 | + let l = MAX_LENGTH - prefix.length - postfix.length - ellipsis.length; |
| 95 | + content = content.slice(0, l); |
| 96 | + output = prefix + content + ellipsis + postfix; |
| 97 | + } |
| 98 | +
|
| 99 | + github.rest.issues.createComment({ |
| 100 | + issue_number: context.issue.number, |
| 101 | + owner: context.repo.owner, |
| 102 | + repo: context.repo.repo, |
| 103 | + body: output |
| 104 | + }); |
0 commit comments