Skip to content

Commit cf86907

Browse files
authored
Merge pull request #6065 from GSA/snyk-scan-template
add reusable Snyk scanning workflow
2 parents d28305e + 37282ca commit cf86907

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
name: Snyk Scan Template
3+
4+
on: # yamllint disable-line rule:truthy
5+
workflow_call:
6+
inputs:
7+
all_projects:
8+
description: Scan all supported dependency projects in the caller repository
9+
required: false
10+
type: boolean
11+
default: true
12+
node_version:
13+
description: Node.js version used to install the Snyk CLI
14+
required: false
15+
type: string
16+
default: "20"
17+
severity_threshold:
18+
description: Minimum vulnerability severity that fails the scan
19+
required: false
20+
type: string
21+
default: medium
22+
issue_labels:
23+
description: Comma-separated labels for scan failure issues
24+
required: false
25+
type: string
26+
default: "bug,o&m,compliance"
27+
secrets:
28+
SNYK_TOKEN:
29+
required: true
30+
31+
permissions:
32+
contents: read
33+
issues: write
34+
35+
jobs:
36+
snyk:
37+
name: Snyk Scan
38+
runs-on: ubuntu-latest
39+
40+
steps:
41+
- name: Check out repository
42+
uses: actions/checkout@v4
43+
44+
- name: Set up Node.js
45+
uses: actions/setup-node@v4
46+
with:
47+
node-version: ${{ inputs.node_version }}
48+
49+
- name: Install Snyk CLI
50+
run: npm install --global snyk
51+
52+
- name: Scan dependencies
53+
shell: bash
54+
env:
55+
ALL_PROJECTS: ${{ inputs.all_projects }}
56+
SEVERITY_THRESHOLD: ${{ inputs.severity_threshold }}
57+
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
58+
run: |
59+
args=("--severity-threshold=${SEVERITY_THRESHOLD}")
60+
61+
if [[ "${ALL_PROJECTS}" == "true" ]]; then
62+
args+=("--all-projects")
63+
fi
64+
65+
snyk test "${args[@]}"
66+
67+
- name: Create issue for failure
68+
if: ${{ failure() && github.event_name == 'schedule' }}
69+
shell: bash
70+
env:
71+
GH_TOKEN: ${{ github.token }}
72+
ISSUE_LABELS: ${{ inputs.issue_labels }}
73+
run: |
74+
repository_name="${GITHUB_REPOSITORY#*/}"
75+
title="Snyk Check Failed: ${repository_name}"
76+
run_url="https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
77+
issue_number="$(
78+
gh issue list \
79+
--repo "${GITHUB_REPOSITORY}" \
80+
--state open \
81+
--search "\"${title}\" in:title" \
82+
--json number \
83+
--jq '.[0].number'
84+
)"
85+
86+
if [[ -n "${issue_number}" ]]; then
87+
gh issue comment "${issue_number}" \
88+
--repo "${GITHUB_REPOSITORY}" \
89+
--body "Snyk failed again: ${run_url}"
90+
else
91+
gh issue create \
92+
--repo "${GITHUB_REPOSITORY}" \
93+
--title "${title}" \
94+
--label "${ISSUE_LABELS}" \
95+
--body "The ${repository_name} Snyk scan failed: ${run_url}"
96+
fi

0 commit comments

Comments
 (0)