Skip to content

Commit b1f3b16

Browse files
committed
Just link to Trivy results in PR comment
1 parent 71bc7c4 commit b1f3b16

2 files changed

Lines changed: 60 additions & 42 deletions

File tree

.github/workflows/docker_security.yml

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ jobs:
5959

6060
- name: Scan image with Trivy (JUnit)
6161
id: trivy-junit
62-
if: github.event_name == 'pull_request'
6362
uses: aquasecurity/trivy-action@0.34.2
6463
with:
6564
image-ref: vecoli:latest
@@ -69,9 +68,26 @@ jobs:
6968
vuln-type: os,library
7069
ignore-unfixed: true
7170

72-
- name: Upload Trivy JUnit result
73-
if: github.event_name == 'pull_request'
74-
uses: actions/upload-artifact@v4
71+
- name: Publish Trivy test report (JUnit)
72+
uses: ctrf-io/github-test-reporter@v1
7573
with:
76-
name: trivy-pr-report
77-
path: trivy-junit.xml
74+
report-path: trivy-junit.xml
75+
summary-report: false
76+
failed-report: true
77+
pull-request: false
78+
github-report: false
79+
integrations-config: |
80+
{
81+
"junit-to-ctrf": {
82+
"enabled": true,
83+
"action": "convert",
84+
"options": {
85+
"output": "./ctrf-reports/ctrf-report.json",
86+
"toolname": "junit-to-ctrf",
87+
"useSuiteName": false,
88+
"env": {
89+
"appName": "vEcoli"
90+
}
91+
}
92+
}
93+
}

.github/workflows/trivy_pr_comment.yml

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,9 @@ on:
1212

1313
jobs:
1414
comment:
15-
if: github.event.workflow_run.event == 'pull_request'
15+
if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request' }}
1616
runs-on: ubuntu-latest
1717
steps:
18-
- name: Check out PR code
19-
uses: actions/checkout@v6
20-
21-
- name: Download Trivy PR artifacts
22-
uses: actions/download-artifact@v4
23-
with:
24-
run-id: ${{ github.event.workflow_run.id }}
25-
name: trivy-pr-report
26-
path: trivy-artifacts
27-
repository: ${{ github.repository }}
28-
github-token: ${{ secrets.GITHUB_TOKEN }}
29-
3018
- name: Determine PR number securely
3119
id: get_pr
3220
env:
@@ -46,29 +34,43 @@ jobs:
4634
4735
echo "PR_NUMBER=$PR_NUM" >> $GITHUB_ENV
4836
49-
- name: Publish Trivy test report
50-
uses: ctrf-io/github-test-reporter@v1
37+
- name: Comment PR with Trivy summary link
38+
uses: actions/github-script@v8
5139
with:
52-
report-path: trivy-artifacts/trivy-junit.xml
53-
summary-report: false
54-
failed-report: true
55-
pull-request: ${{ env.PR_NUMBER }}
56-
update-comment: true
57-
overwrite-comment: true
58-
integrations-config: |
59-
{
60-
"junit-to-ctrf": {
61-
"enabled": true,
62-
"action": "convert",
63-
"options": {
64-
"output": "./ctrf-reports/ctrf-report.json",
65-
"toolname": "junit-to-ctrf",
66-
"useSuiteName": false,
67-
"env": {
68-
"appName": "vEcoli"
69-
}
70-
}
71-
}
40+
script: |
41+
const issue_number = Number(process.env.PR_NUMBER);
42+
if (!issue_number) {
43+
core.info('No PR number available.');
44+
return;
45+
}
46+
const header = '## Trivy scan results';
47+
const runUrl = context.payload.workflow_run.html_url;
48+
const comment = `${header}\n\nThe Trivy vulnerability report is available in the workflow summary:\n${runUrl}`;
49+
const { data: comments } = await github.rest.issues.listComments({
50+
owner: context.repo.owner,
51+
repo: context.repo.repo,
52+
issue_number,
53+
per_page: 100,
54+
});
55+
const existing = comments.find((c) => {
56+
if (!c.body) return false;
57+
const isBot = c.user && c.user.type === 'Bot';
58+
return isBot && c.body.startsWith(header);
59+
});
60+
if (existing) {
61+
await github.rest.issues.updateComment({
62+
owner: context.repo.owner,
63+
repo: context.repo.repo,
64+
comment_id: existing.id,
65+
body: comment,
66+
});
67+
} else {
68+
await github.rest.issues.createComment({
69+
owner: context.repo.owner,
70+
repo: context.repo.repo,
71+
issue_number,
72+
body: comment,
73+
});
7274
}
7375
env:
74-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
76+
PR_NUMBER: ${{ env.PR_NUMBER }}

0 commit comments

Comments
 (0)