|
| 1 | +# Run Semgrep Action |
| 2 | + |
| 3 | +## 🧭 Summary |
| 4 | + |
| 5 | +Runs a Semgrep scan normalizing the baseline for diff scans depending on push vs PR context. Outputs scan results and summaries for downstream steps. |
| 6 | + |
| 7 | +## Scope/Limitations |
| 8 | + |
| 9 | +- Supports both push and pull request events. |
| 10 | +- Requires Semgrep to be installed and available in the runner environment. |
| 11 | +- Expects environment variables for configuration (see below). |
| 12 | + |
| 13 | +## 🔒 Permissions |
| 14 | + |
| 15 | +The following GHA permissions are required to use this step: |
| 16 | + |
| 17 | +```yaml |
| 18 | +permissions: |
| 19 | + contents: read |
| 20 | +``` |
| 21 | +
|
| 22 | +## Dependencies |
| 23 | +
|
| 24 | +- `semgrep` — must be installed in the runner environment. |
| 25 | +- `node-fetch` — required Node.js dependency (see package.json). |
| 26 | +- `reviewdog` — for annotation output (optional, for downstream steps). |
| 27 | + |
| 28 | +## ⚙️ Inputs |
| 29 | + |
| 30 | +This action is environment-driven. The following environment variables are required: |
| 31 | + |
| 32 | +| Name | Required | Description | |
| 33 | +| ------------------- | -------- | ------------------------------------------------------------------------------------------- | |
| 34 | +| `HAS_PR` | ✅ | Whether the current context has an associated PR (true/false) | |
| 35 | +| `PR_NUMBER` | ❌ | PR number if applicable | |
| 36 | +| `PR_URL` | ❌ | PR URL if applicable | |
| 37 | +| `INPUT_BASELINE` | ✅ | Baseline ref to use for diffing (e.g., origin/main) | |
| 38 | +| `GITHUB_EVENT_NAME` | ✅ | GitHub provided environment variable for event name (e.g., push, pull_request) | |
| 39 | +| `GITHUB_REF_NAME` | ✅ | GitHub provided environment variable for the branch or tag name that triggered the workflow | |
| 40 | +| `GITHUB_BASE_REF` | ❌ | GitHub provided environment variable for the base ref of a PR (if applicable) | |
| 41 | +| `GITHUB_REPOSITORY` | ✅ | GitHub provided environment variable for the repository (e.g., owner/repo) | |
| 42 | +| `GITHUB_TOKEN` | ✅ | GitHub token for API access | |
| 43 | +| `SCAN_MODE` | ✅ | 'diff' or 'full' scan mode | |
| 44 | +| `SEMGREP_CONFIG` | ✅ | Semgrep ruleset(s) to use | |
| 45 | +| `SEMGREP_TARGETS` | ✅ | Targets to scan (default: current directory) | |
| 46 | +| `FAIL_LEVEL` | ✅ | Severity level to fail on (e.g., ERROR, WARNING) | |
| 47 | +| `EXTRA_ARGS` | ❌ | Additional arguments to pass to Semgrep | |
| 48 | + |
| 49 | +## 📤 Outputs |
| 50 | + |
| 51 | +Along with writing files for reviewdog annotations and inputs, this action provides the following outputs: |
| 52 | + |
| 53 | +| Name | Description | |
| 54 | +| -------------------- | --------------------------------------------------- | |
| 55 | +| `normalizedBaseline` | The resolved baseline ref | |
| 56 | +| `scanSummary` | Summary of findings in markdown format | |
| 57 | +| `configSummary` | Summary of scan config in markdown format | |
| 58 | +| `scanStatus` | 'success' or 'failure' based on findings/fail level | |
| 59 | +| `totalFindings` | Total number of findings | |
| 60 | +| `numErrors` | Number of ERROR severity findings | |
| 61 | +| `numWarnings` | Number of WARNING severity findings | |
| 62 | +| `numInfo` | Number of INFO severity findings | |
| 63 | + |
| 64 | +## 🚀 Usage |
| 65 | + |
| 66 | +Basic usage example: |
| 67 | + |
| 68 | +```yaml |
| 69 | +- name: Run Semgrep |
| 70 | + id: semgrep |
| 71 | + uses: OpenSesame/core-github-actions/.github/actions/run-semgrep@actions/run-semgrep/1.0.0 |
| 72 | + env: |
| 73 | + HAS_PR: ${{ env.HAS_PR }} |
| 74 | + INPUT_BASELINE: ${{ env.INPUT_BASELINE }} |
| 75 | + GITHUB_EVENT_NAME: ${{ github.event_name }} |
| 76 | + GITHUB_REF_NAME: ${{ github.ref_name }} |
| 77 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 78 | + GITHUB_REPOSITORY: ${{ github.repository }} |
| 79 | + SEMGREP_CONFIG: 'p/default' |
| 80 | + SEMGREP_TARGETS: '.' |
| 81 | + SCAN_MODE: 'full' |
| 82 | + FAIL_LEVEL: 'error' |
| 83 | + EXTRA_ARGS: '' |
| 84 | +``` |
| 85 | + |
| 86 | +Example outputs: |
| 87 | + |
| 88 | +```yaml |
| 89 | +steps.semgrep.outputs.scanStatus |
| 90 | +steps.semgrep.outputs.totalFindings |
| 91 | +``` |
| 92 | + |
| 93 | +Example usage of outputs in later steps: |
| 94 | + |
| 95 | +```yaml |
| 96 | +if: steps.semgrep.outputs.scanStatus == 'failure' |
| 97 | + run: echo "Semgrep scan failed at or above threshold." |
| 98 | +``` |
| 99 | + |
| 100 | +## 🧠 Notes |
| 101 | + |
| 102 | +- This action writes a file for reviewdog annotations (`reviewdog_input.txt`). |
| 103 | +- Unit tests for the script are included in `run-semgrep.unit.test.js` (not used by the action, but kept for maintainability). |
| 104 | + |
| 105 | +## Versioning |
| 106 | + |
| 107 | +This action uses namespaced tags for versioning and is tracked in the CHANGELOG. |
| 108 | + |
| 109 | +```text |
| 110 | +actions/run-semgrep/vX.Y.Z |
| 111 | +``` |
| 112 | + |
| 113 | +See the repository's versioning documentation for details on how tags are validated and created. |
0 commit comments