@@ -10,45 +10,91 @@ jobs:
1010 name : Terraform
1111 runs-on : ubuntu-latest
1212
13- steps :
14- - name : Checkout code
15- uses : actions/checkout@v3
16-
17- - name : Set up Terraform
18- uses : hashicorp/setup-terraform@v3
19- with :
20- terraform_version : 1.7.3
21-
22- - name : Configure AWS Credentials
23- uses : aws-actions/configure-aws-credentials@v4
24- with :
25- aws-access-key-id : ${{ secrets.AWS_ACCESS_KEY_ID }}
26- aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
27- aws-region : us-west-2
13+ defaults :
14+ run :
15+ working-directory : terraform
16+ permissions :
17+ pull-requests : write
2818
19+ steps :
20+ - uses : actions/checkout@v4
21+ - uses : hashicorp/setup-terraform@v3
22+
23+ - name : Terraform fmt
24+ id : fmt
25+ run : terraform fmt -check
26+ continue-on-error : true
27+
2928 - name : Terraform Init
29+ id : init
3030 run : terraform init
31- working-directory : ./terraform
32-
31+ env :
32+ AWS_ACCESS_KEY_ID : ${{ secrets.AWS_ACCESS_KEY_ID }}
33+ AWS_SECRET_ACCESS_KEY : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
34+
35+ - name : Terraform Validate
36+ id : validate
37+ run : terraform validate -no-color
38+
3339 - name : Terraform Plan
3440 id : plan
35- run : |
36- terraform plan -no-color > plan-output.txt
37- cat plan-output.txt
38- working-directory : ./terraform
41+ run : terraform plan -no-color
3942 continue-on-error : true
40-
41- - name : Comment Plan Output on PR
42- uses : actions/github-script@v7
43+
44+ - uses : actions/github-script@v6
4345 if : github.event_name == 'pull_request'
46+ env :
47+ PLAN : " terraform\n ${{ steps.plan.outputs.stdout }}"
4448 with :
4549 github-token : ${{ secrets.GITHUB_TOKEN }}
4650 script : |
47- const fs = require('fs');
48- const planOutput = fs.readFileSync('${{ github.workspace }}/terraform/plan-output.txt', 'utf8');
49- github.rest.issues.createComment({
50- issue_number: context.issue.number,
51+ // 1. Retrieve existing bot comments for the PR
52+ const { data: comments } = await github.rest.issues.listComments({
5153 owner: context.repo.owner,
52- repo: context.repo.name,
53- body: '### Terraform Plan Output\n' + '```\n' + planOutput + '\n```',
54- });
54+ repo: context.repo.repo,
55+ issue_number: context.issue.number,
56+ })
57+ const botComment = comments.find(comment => {
58+ return comment.user.type === 'Bot' && comment.body.includes('Terraform Format and Style')
59+ })
60+
61+ // 2. Prepare format of the comment
62+ const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
63+ # ### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
64+ # ### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
65+ <details><summary>Validation Output</summary>
66+
67+ \`\`\`\n
68+ ${{ steps.validate.outputs.stdout }}
69+ \`\`\`
70+
71+ </details>
72+
73+ # ### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
74+
75+ <details><summary>Show Plan</summary>
76+
77+ \`\`\`\n
78+ ${process.env.PLAN}
79+ \`\`\`
80+
81+ </details>
82+
83+ *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.tf_actions_working_dir }}\`, Workflow: \`${{ github.workflow }}\`*`;
84+
85+ // 3. If we have a comment, update it, otherwise create a new one
86+ if (botComment) {
87+ github.rest.issues.updateComment({
88+ owner : context.repo.owner,
89+ repo : context.repo.repo,
90+ comment_id : botComment.id,
91+ body : output
92+ })
93+ } else {
94+ github.rest.issues.createComment({
95+ issue_number : context.issue.number,
96+ owner : context.repo.owner,
97+ repo : context.repo.repo,
98+ body : output
99+ })
100+ }
0 commit comments