Skip to content

Commit

Permalink
Print scan output to PR
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangpengcheng committed May 14, 2024
1 parent e500941 commit 29d7220
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions .github/workflows/trivy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,44 +66,115 @@ jobs:
PULSAR_IMAGE_TAG=3.2.2.5 PULSAR_IMAGE=streamnative/sn-platform KIND_PUSH=false images/build.sh
- name: Run Trivy vulnerability scanner for java
id: scan-java-runner
uses: aquasecurity/trivy-action@master
with:
image-ref: 'pulsar-functions-java-runner:latest'
format: 'table'
exit-code: '0'
output: '${{ github.workspace }}/scan-java-runner_output.txt'

- name: Run Trivy vulnerability scanner for python
id: scan-python-runner
uses: aquasecurity/trivy-action@master
with:
image-ref: 'pulsar-functions-python-runner:latest'
format: 'table'
exit-code: '0'
output: '${{ github.workspace }}/scan-python-runner_output.txt'

- name: Run Trivy vulnerability scanner for go
id: scan-go-runner
uses: aquasecurity/trivy-action@master
with:
image-ref: 'pulsar-functions-go-runner:latest'
format: 'table'
exit-code: '0'
output: '${{ github.workspace }}/scan-go-runner_output.txt'

- name: Run Trivy vulnerability scanner for java with pulsarctl
id: scan-java-pulsarctl-runner
uses: aquasecurity/trivy-action@master
with:
image-ref: 'pulsar-functions-pulsarctl-java-runner:latest'
format: 'table'
exit-code: '0'
output: '${{ github.workspace }}/scan-java-pulsarctl-runner_output.txt'

- name: Run Trivy vulnerability scanner for python with pulsarctl
id: scan-python-pulsarctl-runner
uses: aquasecurity/trivy-action@master
with:
image-ref: 'pulsar-functions-pulsarctl-python-runner:latest'
format: 'table'
exit-code: '0'
output: '${{ github.workspace }}/scan-python-pulsarctl-runner_output.txt'

- name: Run Trivy vulnerability scanner for go with pulsarctl
id: scan-go-pulsarctl-runner
uses: aquasecurity/trivy-action@master
with:
image-ref: 'pulsar-functions-pulsarctl-go-runner:latest'
format: 'table'
exit-code: '0'
output: '${{ github.workspace }}/scan-go-pulsarctl-runner_output.txt'

# Comment on PR with the scan output since the action won't fail with CVEs
- name: Comment on PR
uses: actions/github-script@v5
with:
script: |
const fs = require('fs');
const path = require('path');
const uniqueIdentifier = '#Runner Images Scan Result:';
// Function to get output from a file
function getOutput(stepId) {
const outputFile = path.join(process.env.GITHUB_WORKSPACE, `${stepId}_output.txt`);
if (fs.existsSync(outputFile)) {
return fs.readFileSync(outputFile, 'utf8');
}
return ''; // Return empty string if file does not exist
}
// Combine outputs from different steps
const outputs = [
{ label: 'Java Runner', output: getOutput('scan-java-runner') },
{ label: 'Python Runner', output: getOutput('scan-python-runner') },
{ label: 'Go Runner', output: getOutput('scan-go-runner') },
{ label: 'Java Runner with Pulsarctl', output: getOutput('scan-java-pulsarctl-runner') },
{ label: 'Python Runner with Pulsarctl', output: getOutput('scan-python-pulsarctl-runner') },
{ label: 'Go Runner with Pulsarctl', output: getOutput('scan-go-pulsarctl-runner') },
].filter(item => item.output !== '');
// Format the combined message
let combinedMessage = outputs.map(item => `**${item.label} Vulnerabilities:**\n\`\`\`\n${item.output}\n\`\`\``).join('\n\n');
combinedMessage = `${uniqueIdentifier}\n\n` + combinedMessage; // Add unique identifier to the message
const issue_number = context.issue.number;
const octokit = github.getOctokit(process.env.GITHUB_TOKEN);
const { data: comments } = await octokit.rest.issues.listComments({
...context.repo,
issue_number: issue_number,
});
// Find existing comment
const existingComment = comments.find(comment => comment.body.includes(uniqueIdentifier));
// Update or create comment
if (existingComment) {
await octokit.rest.issues.updateComment({
...context.repo,
comment_id: existingComment.id,
body: combinedMessage
});
} else {
await octokit.rest.issues.createComment({
...context.repo,
issue_number: issue_number,
body: combinedMessage
});
}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 comments on commit 29d7220

Please sign in to comment.