-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test
- Loading branch information
Showing
1 changed file
with
42 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,64 +16,70 @@ jobs: | |
name: 'Terraform' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Upload Configuration | ||
uses: hashicorp/tfc-workflows-github/actions/[email protected] | ||
id: plan-upload | ||
with: | ||
workspace: ${{ env.TF_WORKSPACE }} | ||
directory: ${{ env.CONFIG_DIRECTORY }} | ||
speculative: true | ||
|
||
- name: Create Plan Run | ||
uses: hashicorp/tfc-workflows-github/actions/[email protected] | ||
id: plan-run | ||
- uses: hashicorp/tfc-workflows-github/actions/[email protected] | ||
id: run | ||
## run may fail, if so continue to output PR comment | ||
## step.terraform-cloud-check-run-status will fail job after pr comment is created/updated. | ||
continue-on-error: true | ||
with: | ||
workspace: ${{ env.TF_WORKSPACE }} | ||
configuration_version: ${{ steps.plan-upload.outputs.configuration_version_id }} | ||
configuration_version: ${{ steps.upload.outputs.configuration_version_id }} | ||
plan_only: true | ||
## OPTIONAL: set your own message for run. A default message will be defined for you. | ||
## Example: | ||
# message: "Triggered From GitHub Actions CI ${{ github.sha }}" | ||
|
||
- name: Get Plan Output | ||
uses: hashicorp/tfc-workflows-github/actions/[email protected] | ||
- uses: hashicorp/tfc-workflows-github/actions/[email protected] | ||
id: plan-output | ||
with: | ||
plan: ${{ fromJSON(steps.plan-run.outputs.payload).data.relationships.plan.data.id }} | ||
plan: ${{ steps.run.outputs.plan_id }} | ||
|
||
- name: Update PR | ||
uses: actions/github-script@v6 | ||
id: plan-comment | ||
## REQUIRED: Workflow permissions: `Read and write permissions` | ||
- uses: actions/github-script@v6 | ||
if: github.event_name == 'pull_request' | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
// 1. Retrieve existing bot comments for the PR | ||
const { data: comments } = await github.rest.issues.listComments({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: context.issue.number, | ||
}); | ||
}) | ||
const botComment = comments.find(comment => { | ||
return comment.user.type === 'Bot' && comment.body.includes('Terraform Cloud Plan Output') | ||
}); | ||
}) | ||
const output = `#### Terraform Cloud Plan Output | ||
\`\`\` | ||
Plan: ${{ steps.plan-output.outputs.add }} to add, ${{ steps.plan-output.outputs.change }} to change, ${{ steps.plan-output.outputs.destroy }} to destroy. | ||
\`\`\` | ||
[Terraform Cloud Plan](${{ steps.plan-run.outputs.run_link }}) | ||
`; | ||
\`\`\`\n | ||
Plan: ${{ steps.plan-output.outputs.add }} to add, ${{ steps.plan-output.outputs.change }} to change, ${{ steps.plan-output.outputs.destroy }} to destroy. | ||
\`\`\` | ||
[Terraform Cloud Plan](${{ steps.run.outputs.run_link }}) | ||
` | ||
// 3. If we have a comment, update it, otherwise create a new one | ||
if (botComment) { | ||
github.rest.issues.deleteComment({ | ||
github.rest.issues.updateComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
comment_id: botComment.id, | ||
}); | ||
body: output | ||
}) | ||
} else { | ||
github.rest.issues.createComment({ | ||
issue_number: context.issue.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: output | ||
}) | ||
} | ||
github.rest.issues.createComment({ | ||
issue_number: context.issue.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: output | ||
}) | ||
## Check Run Status, if not planned_and_finished fail the job | ||
- id: terraform-cloud-check-run-status | ||
if: ${{ steps.run.outputs.run_status != 'planned_and_finished'}} | ||
run: | | ||
echo "Terraform Cloud Run Failed or Requires Further Attention" | ||
echo "Run Status: '${{ steps.run.outputs.run_status }}'" | ||
echo "${{ steps.run.outputs.run_link }}" | ||
exit 1 | ||
- name: Terraform Plan Status | ||
if: steps.plan.outcome == 'failure' | ||
|