|
| 1 | +# action.yml |
| 2 | +name: 'Build Terraform Plan' |
| 3 | +description: 'Builds a terraform plan' |
| 4 | +author: 'Timothy Farrell' |
| 5 | + |
| 6 | +inputs: |
| 7 | + terraform_version: |
| 8 | + description: 'Terraform version' |
| 9 | + required: true |
| 10 | + root: |
| 11 | + description: 'Run terraform in this subdirectory. Passed to -chdir' |
| 12 | + required: true |
| 13 | + validate_format: |
| 14 | + description: 'Validate the tf file format' |
| 15 | + required: false |
| 16 | + default: 'true' |
| 17 | + cache_providers: |
| 18 | + description: 'Set to "" to not try to cache providers' |
| 19 | + required: false |
| 20 | + default: 'true' |
| 21 | + add_plan_to_pr: |
| 22 | + description: 'For Pull Request events, add a plan comment to the current PR' |
| 23 | + required: false |
| 24 | + default: 'true' |
| 25 | + text_artifact_name: |
| 26 | + description: 'Save the plan text as an artifact with the provided name' |
| 27 | + required: false |
| 28 | + default: '' |
| 29 | + plan_artifact_name: |
| 30 | + description: 'Save the plan as an artifact with the provided name' |
| 31 | + required: false |
| 32 | + default: '' |
| 33 | +outputs: |
| 34 | + text: |
| 35 | + description: 'Plan text' |
| 36 | + value: ${{ steps.plan.outputs.stdout }} |
| 37 | + |
| 38 | +runs: |
| 39 | + using: "composite" |
| 40 | + steps: |
| 41 | + - name: Setup Terraform |
| 42 | + uses: hashicorp/setup-terraform@v1 |
| 43 | + with: |
| 44 | + terraform_version: ${{ inputs.terraform_version }} |
| 45 | + |
| 46 | + - name: Hash Lockfile |
| 47 | + id: lockfile_path |
| 48 | + if: inputs.cache_providers |
| 49 | + shell: bash |
| 50 | + run: echo "${{ inputs.root }}/.terraform.lock.hcl" |
| 51 | + |
| 52 | + - name: Load cached Terraform Providers |
| 53 | + uses: actions/cache@v3 |
| 54 | + if: inputs.cache_providers && hashFiles(steps.lockfile_path.output.stdout) != '' |
| 55 | + with: |
| 56 | + path: ${{ inputs.root }}/.terraform |
| 57 | + key: tf-providers-${{ hashFiles(steps.lockfile_path.output.stdout) }} |
| 58 | + |
| 59 | + - name: Initialize Terraform |
| 60 | + shell: bash |
| 61 | + run: terraform -chdir=${{ inputs.root }} init |
| 62 | + |
| 63 | + - name: Validate Terraform |
| 64 | + if: inputs.validate_format != 'true' |
| 65 | + shell: bash |
| 66 | + run: | |
| 67 | + terraform fmt -check -recursive -diff infrastructure |
| 68 | + terraform -chdir=${{ inputs.root }} validate |
| 69 | +
|
| 70 | + - name: Terraform Plan |
| 71 | + id: plan |
| 72 | + shell: bash |
| 73 | + run: terraform -chdir=${{ inputs.root }} plan -out "tf_plan_${{ github.event.after }}" -no-color -input=false |
| 74 | + |
| 75 | + - name: Save Terraform Plan |
| 76 | + if: inputs.plan_artifact_name != '' && steps.plan.outcome == 'success' |
| 77 | + uses: actions/upload-artifact@v3 |
| 78 | + with: |
| 79 | + name: ${{ inputs.plan_artifact_name }} |
| 80 | + path: ${{ inputs.root }}/tf_plan_${{ github.event.after }} |
| 81 | + if-no-files-found: error |
| 82 | + retention-days: 7 |
| 83 | + |
| 84 | + - name: Dump Plan Text to File |
| 85 | + if: inputs.text_artifact_name != '' && steps.plan.outcome == 'success' |
| 86 | + shell: bash |
| 87 | + env: |
| 88 | + PLAN: "${{ steps.plan.outputs.stdout }}" |
| 89 | + run: echo "${PLAN}" >> tf-plan-${{ github.event.after }}.txt |
| 90 | + |
| 91 | + - name: Save Terraform Plan Text |
| 92 | + if: inputs.text_artifact_name != '' && steps.plan.outcome == 'success' |
| 93 | + uses: actions/upload-artifact@v3 |
| 94 | + with: |
| 95 | + name: ${{ inputs.text_artifact_name }} |
| 96 | + path: ${{ inputs.text_artifact_name }} |
| 97 | + if-no-files-found: error |
| 98 | + retention-days: 7 |
| 99 | + |
| 100 | + - uses: actions/github-script@0.9.0 |
| 101 | + if: steps.plan.outcome == 'success' && inputs.add_plan_to_pr && github.event_name == 'pull_request' |
| 102 | + env: |
| 103 | + PLAN: "${{ steps.plan.outputs.stdout }}" |
| 104 | + with: |
| 105 | + github-token: ${{ github.token }} |
| 106 | + script: | |
| 107 | + const output = `#### Terraform Plan 📖 \`${{ steps.plan.outcome }}\` |
| 108 | + <details><summary>Show Plan</summary> |
| 109 | +
|
| 110 | + \`\`\`terraform\n |
| 111 | + ${process.env.PLAN} |
| 112 | + \`\`\` |
| 113 | +
|
| 114 | + </details> |
| 115 | +
|
| 116 | + *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`* |
| 117 | + `; |
| 118 | + github.issues.createComment({ |
| 119 | + issue_number: context.issue.number, |
| 120 | + owner: context.repo.owner, |
| 121 | + repo: context.repo.repo, |
| 122 | + body: output |
| 123 | + }) |
| 124 | +
|
| 125 | + - name: Terraform Plan Status |
| 126 | + if: steps.plan.outcome == 'failure' |
| 127 | + shell: bash |
| 128 | + run: exit 1 |
0 commit comments