-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci(#8): Switch to Terraform PR Commenter
- Loading branch information
1 parent
8a911f1
commit 3ae202f
Showing
2 changed files
with
55 additions
and
187 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,94 +1,83 @@ | ||
name: What's the Plan | ||
name: "Terraform" | ||
|
||
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- main | ||
- master | ||
|
||
env: | ||
TF_IN_AUTOMATION: true | ||
TF_VAR_gh_token: ${{ secrets.GITHUB_TOKEN }} | ||
TF_VAR_billing_email: ${{ secrets.TF_VAR_billing_email }} | ||
|
||
jobs: | ||
defaults: | ||
terraform: | ||
name: "Terraform" | ||
runs-on: ubuntu-latest | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
TF_IN_AUTOMATION: true | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: hashicorp/setup-terraform@v2 | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
||
- name: Terraform fmt | ||
- name: Setup Terraform | ||
uses: hashicorp/setup-terraform@v1 | ||
with: | ||
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }} | ||
terraform_version: 0.15.0 | ||
|
||
- name: Terraform Format | ||
id: fmt | ||
run: terraform fmt -check | ||
run: terraform fmt -check -recursive | ||
continue-on-error: true | ||
|
||
- name: Post Format | ||
if: always() && github.ref != 'refs/heads/master' && (steps.fmt.outcome == 'success' || steps.fmt.outcome == 'failure') | ||
uses: robburger/terraform-pr-commenter@v1 | ||
with: | ||
commenter_type: fmt | ||
commenter_input: ${{ format('{0}{1}', steps.fmt.outputs.stdout, steps.fmt.outputs.stderr) }} | ||
commenter_exitcode: ${{ steps.fmt.outputs.exitcode }} | ||
|
||
- name: Terraform Init | ||
id: init | ||
run: terraform init | ||
|
||
- name: Post Init | ||
if: always() && github.ref != 'refs/heads/master' && (steps.init.outcome == 'success' || steps.init.outcome == 'failure') | ||
uses: robburger/terraform-pr-commenter@v1 | ||
with: | ||
commenter_type: init | ||
commenter_input: ${{ format('{0}{1}', steps.init.outputs.stdout, steps.init.outputs.stderr) }} | ||
commenter_exitcode: ${{ steps.init.outputs.exitcode }} | ||
|
||
- name: Terraform Validate | ||
id: validate | ||
run: terraform validate -no-color | ||
run: terraform validate | ||
|
||
- name: Post Validate | ||
if: always() && github.ref != 'refs/heads/master' && (steps.validate.outcome == 'success' || steps.validate.outcome == 'failure') | ||
uses: robburger/terraform-pr-commenter@v1 | ||
with: | ||
commenter_type: validate | ||
commenter_input: ${{ format('{0}{1}', steps.validate.outputs.stdout, steps.validate.outputs.stderr) }} | ||
commenter_exitcode: ${{ steps.validate.outputs.exitcode }} | ||
|
||
- name: Terraform Plan | ||
id: plan | ||
run: terraform plan -no-color -input=false | ||
continue-on-error: true | ||
run: terraform plan -out workspace.plan | ||
|
||
- uses: actions/github-script@v6 | ||
if: github.event_name == 'pull_request' | ||
env: | ||
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}" | ||
- name: Post Plan | ||
if: always() && github.ref != 'refs/heads/master' && (steps.plan.outcome == 'success' || steps.plan.outcome == 'failure') | ||
uses: robburger/terraform-pr-commenter@v1 | ||
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 Format and Style') | ||
}) | ||
// 2. Prepare format of the comment | ||
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\` | ||
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\` | ||
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\` | ||
<details><summary>Validation Output</summary> | ||
\`\`\`\n | ||
${{ steps.validate.outputs.stdout }} | ||
\`\`\` | ||
</details> | ||
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\` | ||
<details><summary>Show Plan</summary> | ||
\`\`\`\n | ||
${process.env.PLAN} | ||
\`\`\` | ||
</details> | ||
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.tf_actions_working_dir }}\`, Workflow: \`${{ github.workflow }}\`*`; | ||
// 3. If we have a comment, update it, otherwise create a new one | ||
if (botComment) { | ||
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 | ||
}) | ||
} | ||
commenter_type: plan | ||
commenter_input: ${{ format('{0}{1}', steps.plan.outputs.stdout, steps.plan.outputs.stderr) }} | ||
commenter_exitcode: ${{ steps.plan.outputs.exitcode }} | ||
|
||
- name: Terraform Apply | ||
id: apply | ||
if: github.ref == 'refs/heads/master' && github.event_name == 'push' | ||
run: terraform apply workspace.plan |