1
+ name : Semgrep SAST Scan
2
+
3
+ on :
4
+ pull_request :
5
+
6
+ jobs :
7
+ semgrep :
8
+ # User definable name of this GitHub Actions job.
9
+ name : semgrep/ci
10
+ # If you are self-hosting, change the following `runs-on` value:
11
+ runs-on : ubuntu-latest
12
+ container :
13
+ # A Docker image with Semgrep installed. Do not change this.
14
+ image : returntocorp/semgrep
15
+ # Skip any PR created by dependabot to avoid permission issues:
16
+ if : (github.actor != 'dependabot[bot]')
17
+ permissions :
18
+ # required for all workflows
19
+ security-events : write
20
+ # only required for workflows in private repositories
21
+ actions : read
22
+ contents : read
23
+
24
+ steps :
25
+ # Fetch project source with GitHub Actions Checkout.
26
+ - name : Checkout repository
27
+ uses : actions/checkout@v4
28
+
29
+ - name : Perform Semgrep Analysis
30
+ # @NOTE: This is the actual semgrep command to scan your code.
31
+ # Modify the --config option to 'r/all' to scan using all rules,
32
+ # or use multiple flags to specify particular rules, such as
33
+ # --config r/all --config custom/rules
34
+ run : semgrep scan -q --sarif --config auto --config "p/secrets" . > semgrep-results.sarif
35
+
36
+ - name : Pretty-Print SARIF Output
37
+ run : |
38
+ jq . semgrep-results.sarif > formatted-semgrep-results.sarif || echo "{}"
39
+ echo "Formatted SARIF Output (First 20 lines):"
40
+ head -n 20 formatted-semgrep-results.sarif || echo "{}"
41
+
42
+ - name : Validate JSON Output
43
+ run : |
44
+ if ! jq empty formatted-semgrep-results.sarif > /dev/null 2>&1; then
45
+ echo "⚠️ Semgrep output is not valid JSON. Skipping annotations."
46
+ exit 0
47
+ fi
48
+
49
+ - name : Add PR Annotations for Semgrep Findings
50
+ run : |
51
+ total_issues=$(jq '.runs[0].results | length' formatted-semgrep-results.sarif)
52
+ if [[ "$total_issues" -eq 0 ]]; then
53
+ echo "✅ No Semgrep issues found!"
54
+ exit 0
55
+ fi
56
+
57
+ jq -c '.runs[0].results[]' formatted-semgrep-results.sarif | while IFS= read -r issue; do
58
+ file=$(echo "$issue" | jq -r '.locations[0].physicalLocation.artifactLocation.uri')
59
+ line=$(echo "$issue" | jq -r '.locations[0].physicalLocation.region.startLine')
60
+ message=$(echo "$issue" | jq -r '.message.text')
61
+
62
+ if [[ -n "$file" && -n "$line" && -n "$message" ]]; then
63
+ echo "::error file=$file,line=$line,title=Semgrep Issue::${message}"
64
+ fi
65
+ done
0 commit comments