6666 username : ${{ secrets.username }}
6767 password : ${{ secrets.password }}
6868
69- - name : Run Hadolint Dockerfile linter
70- if : ${{ inputs.hadolint }}
71- 72- with :
73- dockerfile : ${{ inputs.dockerfile }}
74- output-file : hadolint.txt
75- no-fail : true
76-
7769 - name : Build Docker Image
7870 if : ${{ inputs.push }}
7971 uses : docker/build-push-action@v6
9183 docker save -o vuln-image.tar ${{ inputs.image-name }}:${{ inputs.image-tag }}
9284
9385 - name : Run Trivy vulnerability scanner
86+ id : trivy
9487 if : ${{ inputs.security-scan }}
9588 uses :
aquasecurity/[email protected] 9689 with :
@@ -102,60 +95,87 @@ jobs:
10295 hide-progress : true
10396 output : trivy.txt
10497
105- - name : Update Pull Request with Security Scan Results
106- uses : actions/github-script@v7
98+ - name : Read Trivy report file
99+ id : read_trivy
107100 if : github.event_name == 'pull_request' && inputs.security-scan && inputs.security-report == 'comment'
101+ run : |
102+ echo "report<<EOF" >> "$GITHUB_OUTPUT"
103+ cat trivy.txt >> "$GITHUB_OUTPUT"
104+ echo "EOF" >> "$GITHUB_OUTPUT"
105+
106+ - name : Find existing Trivy comment
107+ if : github.event_name == 'pull_request' && inputs.security-scan && inputs.security-report == 'comment'
108+ id : find_trivy
109+ uses : peter-evans/find-comment@v3
110+ with :
111+ issue-number : ${{ github.event.pull_request.number }}
112+ comment-author : ' github-actions[bot]'
113+ body-includes : ' Trivy Security Scan Results'
114+
115+ - name : Create or update Trivy comment
116+ if : github.event_name == 'pull_request' && inputs.security-scan && inputs.security-report == 'comment'
117+ uses : peter-evans/create-or-update-comment@v4
108118 with :
109- script : |
110- const fs = require('fs');
111- const trivyResults = fs.readFileSync('trivy.txt', 'utf8');
112-
113- const output = `
119+ token : ${{ secrets.GITHUB_TOKEN }}
120+ issue-number : ${{ github.event.pull_request.number }}
121+ comment-id : ${{ steps.find_trivy.outputs.comment-id }}
122+ edit-mode : replace
123+ body : |
124+ <!-- trivy-scan -->
114125 ### 🔒 Trivy Security Scan Results
115126 <details><summary>Click to expand detailed results</summary>
116-
117- \`\`\`
118- ${trivyResults }
119- \`\`\ `
127+
128+ ```bash
129+ ${{ steps.read_trivy.outputs.report } }
130+ `` `
120131 </details>
121- ` ;
122-
123- await github.rest.issues.createComment({
124- issue_number: context.issue.number,
125- owner: context.repo.owner,
126- repo: context.repo.repo,
127- body: output
128- });
129132
130133 - name : Upload Trivy scan results to GitHub Security tab
131134 if : github.event_name == 'pull_request' && inputs.security-scan && inputs.security-report == 'sarif'
132135 uses : github/codeql-action/upload-sarif@v3
133136 with :
134137 sarif_file : ' trivy-results.sarif'
135138
136- - name: Update Pull Request with Hadolint Results
137- uses: actions/github-script@v7
138- if: github.event_name == 'pull_request' && inputs.hadolint
139+ - name : Run Hadolint Dockerfile linter
140+ id : hadolint
141+ if : ${{ inputs.hadolint }}
142+ 143+ with :
144+ dockerfile : ${{ inputs.dockerfile }}
145+ output-file : hadolint.txt
146+ no-fail : true
147+
148+ - name : Read Hadolint report file
149+ id : read_hadolint
150+ if : ${{ inputs.hadolint }}
151+ run : |
152+ echo "report<<EOF" >> "$GITHUB_OUTPUT"
153+ cat hadolint.txt >> "$GITHUB_OUTPUT"
154+ echo "EOF" >> "$GITHUB_OUTPUT"
155+
156+ - name : Find existing Hadolint comment
157+ id : find_hadolint
158+ if : ${{ inputs.hadolint }}
159+ uses : peter-evans/find-comment@v3
160+ with :
161+ issue-number : ${{ github.event.pull_request.number }}
162+ comment-author : ' github-actions[bot]'
163+ body-includes : ' Hadolint Dockerfile Lint Results'
164+
165+ - name : Create or update Hadolint comment
166+ if : ${{ inputs.hadolint && steps.read_hadolint.outputs.report != '' }}
167+ uses : peter-evans/create-or-update-comment@v4
139168 with :
140- script: |
141- const fs = require('fs');
142- const hadolintResults = fs.readFileSync('hadolint.txt', 'utf8').trim();
143-
144- if (hadolintResults.length > 0) {
145- const output = `
146- # ## 🐳 Hadolint Dockerfile Lint Results
147- <details><summary>Click to expand</summary>
148-
149- \`\`\`
150- ${hadolintResults}
151- \`\`\`
152- </details>
153- ` ;
154-
155- await github.rest.issues.createComment({
156- issue_number: context.issue.number,
157- owner: context.repo.owner,
158- repo: context.repo.repo,
159- body: output
160- });
161- }
169+ token : ${{ secrets.GITHUB_TOKEN }}
170+ issue-number : ${{ github.event.pull_request.number }}
171+ comment-id : ${{ steps.find_hadolint.outputs.comment-id }}
172+ edit-mode : replace
173+ body : |
174+ <!-- hadolint-scan -->
175+ ### 🐳 Hadolint Dockerfile Lint Results
176+ <details><summary>Click to expand detailed results</summary>
177+
178+ ```bash
179+ ${{ steps.read_hadolint.outputs.report }}
180+ ```
181+ </details>
0 commit comments