|
| 1 | +# Terraform API-driven speculative plan run using Terraform Cloud |
| 2 | +name: Terraform Speculative Plan |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_call: |
| 6 | + # See https://developer.hashicorp.com/terraform/cli/cloud/settings#environment-variables |
| 7 | + inputs: |
| 8 | + cloud_organization: |
| 9 | + type: string |
| 10 | + default: "soat-tech-challenge" |
| 11 | + cloud_workspace: |
| 12 | + type: string |
| 13 | + config_directory: |
| 14 | + type: string |
| 15 | + default: "./" |
| 16 | + secrets: |
| 17 | + cloud_token: |
| 18 | + required: true |
| 19 | + |
| 20 | +jobs: |
| 21 | + terraform-cloud-speculative-plan: |
| 22 | + name: Terraform Cloud Speculative Plan |
| 23 | + runs-on: ubuntu-latest |
| 24 | + |
| 25 | + outputs: |
| 26 | + status: ${{ steps.apply-run.outputs.status }} |
| 27 | + payload: ${{ steps.apply-run.outputs.payload }} |
| 28 | + run_id: ${{ steps.apply-run.outputs.id }} |
| 29 | + run_link: ${{ steps.apply-run.outputs.run_link }} |
| 30 | + plan_id: ${{ steps.apply-run.outputs.plan_id }} |
| 31 | + |
| 32 | + env: |
| 33 | + TF_CLOUD_ORGANIZATION: ${{ inputs.cloud_organization }} |
| 34 | + TF_API_TOKEN: ${{ secrets.cloud_token }} |
| 35 | + TF_WORKSPACE: ${{ inputs.cloud_workspace }} |
| 36 | + CONFIG_DIRECTORY: ${{ inputs.config_directory }} |
| 37 | + |
| 38 | + steps: |
| 39 | + - name: Checkout |
| 40 | + uses: actions/checkout@v3 |
| 41 | + |
| 42 | + - name: Upload Configuration |
| 43 | + uses: hashicorp/tfc-workflows-github/actions/[email protected] |
| 44 | + id: plan-upload |
| 45 | + with: |
| 46 | + workspace: ${{ env.TF_WORKSPACE }} |
| 47 | + directory: ${{ env.CONFIG_DIRECTORY }} |
| 48 | + speculative: true |
| 49 | + |
| 50 | + - name: Create Plan Run |
| 51 | + uses: hashicorp/tfc-workflows-github/actions/[email protected] |
| 52 | + id: plan-run |
| 53 | + ## run may fail, if so continue to output PR comment |
| 54 | + ## step.terraform-cloud-check-run-status will fail job after pr comment is created/updated. |
| 55 | + continue-on-error: true |
| 56 | + with: |
| 57 | + workspace: ${{ env.TF_WORKSPACE }} |
| 58 | + configuration_version: ${{ steps.plan-upload.outputs.configuration_version_id }} |
| 59 | + plan_only: true |
| 60 | + |
| 61 | + - name: Get Plan Output |
| 62 | + uses: hashicorp/tfc-workflows-github/actions/[email protected] |
| 63 | + id: plan-output |
| 64 | + with: |
| 65 | + plan: ${{ steps.plan-run.outputs.plan_id }} |
| 66 | + |
| 67 | + - name: Update PR with Plan comment |
| 68 | + uses: actions/github-script@v6 |
| 69 | + if: github.event_name == 'pull_request' |
| 70 | + with: |
| 71 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 72 | + script: | |
| 73 | + // 1. Retrieve existing bot comments for the PR |
| 74 | + const { data: comments } = await github.rest.issues.listComments({ |
| 75 | + owner: context.repo.owner, |
| 76 | + repo: context.repo.repo, |
| 77 | + issue_number: context.issue.number, |
| 78 | + }) |
| 79 | + const botComment = comments.find(comment => { |
| 80 | + return comment.user.type === 'Bot' && comment.body.includes('Terraform Cloud Plan Output') |
| 81 | + }) |
| 82 | + const output = `#### Terraform Cloud Plan Output |
| 83 | + \`\`\` |
| 84 | + Plan: ${{ steps.plan-output.outputs.add }} to add, ${{ steps.plan-output.outputs.change }} to change, ${{ steps.plan-output.outputs.destroy }} to destroy. |
| 85 | + \`\`\` |
| 86 | + [Terraform Cloud Plan](${{ steps.plan-run.outputs.run_link }}) |
| 87 | + ` |
| 88 | + // 3. If we have a comment, update it, otherwise create a new one |
| 89 | + if (botComment) { |
| 90 | + github.rest.issues.updateComment({ |
| 91 | + owner: context.repo.owner, |
| 92 | + repo: context.repo.repo, |
| 93 | + comment_id: botComment.id, |
| 94 | + body: output |
| 95 | + }) |
| 96 | + } else { |
| 97 | + github.rest.issues.createComment({ |
| 98 | + issue_number: context.issue.number, |
| 99 | + owner: context.repo.owner, |
| 100 | + repo: context.repo.repo, |
| 101 | + body: output |
| 102 | + }) |
| 103 | + } |
| 104 | +
|
| 105 | + ## Check Run Status, if not planned_and_finished fail the job |
| 106 | + - id: terraform-cloud-check-run-status |
| 107 | + if: ${{ steps.plan-run.outputs.run_status != 'planned_and_finished'}} |
| 108 | + run: | |
| 109 | + echo "Terraform Cloud Run Failed or Requires Further Attention" |
| 110 | + echo "Run Status: '${{ steps.plan-run.outputs.run_status }}'" |
| 111 | + echo "${{ steps.plan-run.outputs.run_link }}" |
| 112 | + exit 1 |
0 commit comments