Blocking insecure code before it hits production is the whole point of DevSecOps, yet many teams fail at the execution stage. They either flood pull requests with noisy low-priority warnings or let scans run asynchronously without enforcing a hard merge stop. To block insecure pull requests using Snyk DeepCode AI, you need two synchronized components: a GitHub Actions workflow executing snyk code test with an explicit severity threshold, and a GitHub branch protection rule that requires this job to pass before merging.
Snyk DeepCode AI powers Snyk Code—Snyk's Static Application Security Testing (SAST) engine. Unlike naive LLM-based scanners that hallucinate security flaws, DeepCode AI relies on a hybrid engine combining symbolic AI (deterministic taint analysis and abstract syntax graphs) with machine learning. At 365Finds, we favor this architecture because symbolic AI enforces mathematical rigor on data flows while machine learning adapts to real-world coding idioms. When DeepCode AI detects an unhandled vulnerability, snyk code test exits with code 1, causing the GitHub Actions step to fail.
Here is the exact implementation path to turn that failure into an impenetrable merge gate.
Step 1: Store the Snyk API Token in GitHub Secrets
Your pipeline needs authenticated access to the Snyk Code analysis engine.
- Generate an API token from your Snyk account under Account Settings > General > API Token, or provision a dedicated Service Account under Organization Settings > Service Accounts (recommended for production environments to avoid user-bound expiry).
- Ensure that Snyk Code is toggled Enabled under Organization Settings > Snyk Code. If disabled, static code scanning commands will fail.
- In your GitHub repository, navigate to Settings > Secrets and variables > Actions.
- Click New repository secret.
- Name the secret
SNYK_TOKENand paste your Snyk API token as the value.
Step 2: Configure the GitHub Actions Workflow
Create a dedicated workflow file at .github/workflows/snyk-code.yml.
We configure the action to trigger on every pull request targeting main (or your production branch), install the Snyk CLI via the official setup action, and run snyk code test.
name: Snyk DeepCode AI Security Gate
on:
pull_request:
branches: [ "main", "master" ]
jobs:
security-sast:
name: Snyk DeepCode SAST Check
runs-on: ubuntu-latest
permissions:
contents: read
security-events: write # Required if uploading SARIF to GitHub Code Scanning
steps:
- name: Check out repository code
uses: actions/checkout@v4
- name: Install Snyk CLI
uses: snyk/actions/setup@master
- name: Run Snyk DeepCode AI Scan
run: snyk code test --severity-threshold=high
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
Understanding the Key Parameters
snyk code test: Directs Snyk specifically to run static source code analysis powered by DeepCode AI. Runningsnyk testby mistake tests only third-party package dependencies (SCA), leaving your proprietary application code uninspected.--severity-threshold=high: Only vulnerabilities classified as High or Critical will trigger exit code1and fail the job.- Exit Codes: The command returns
0when clean,1when vulnerabilities matching or exceeding your threshold are detected, and2on execution error. GitHub Actions treats any non-zero exit as a job failure.
Optional: Generating Inline PR Annotations via SARIF
If your team has GitHub Advanced Security or runs an open-source public repository, you can publish the DeepCode AI scan directly to GitHub's Security > Code scanning tab and surface inline diff comments:
- name: Run Snyk Code and Export SARIF
run: snyk code test --sarif-file-output=snyk-code.sarif || true
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
- name: Upload SARIF to GitHub Code Scanning
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: snyk-code.sarif
- name: Enforce High Severity Gate
run: snyk code test --severity-threshold=high
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
Notice the || true on the SARIF generation step: it prevents the workflow from aborting before the SARIF report uploads, while the subsequent enforcement step guarantees the pipeline fails if high-severity flaws remain.
Step 3: Enforce GitHub Branch Protection Rules
A failing workflow step stops the CI job, but Git will still allow repository admins or collaborators to merge the pull request unless branch protection is strictly enforced.
- In your GitHub repository, open Settings > Branches.
- Under Branch protection rules, click Add branch protection rule (or edit your existing rule for
main). - Set Branch name pattern to
main(or your release branch). - Check Require status checks to pass before merging.
- Check Require branches to be up to date before merging. This ensures the pull request branch is tested against the latest destination code before merge.
- In the search box under Status checks that are required, search for your job name:
Snyk DeepCode SAST Check(or the identifier defined in your YAMLnameattribute). Check the box next to it. - Click Save changes.
Once this rule is saved, any pull request containing high-risk flaws flagged by Snyk DeepCode AI displays a red status badge in GitHub, and the Merge pull request button is permanently locked until the vulnerable code is corrected.
365Finds Evaluation: Pitfalls and Best Practices
At 365Finds, we evaluate security tooling by its operational viability in day-to-day engineering. Implementing a hard merge block comes with operational risks you must anticipate:
1. The Threshold Mistake: Avoid Blocking on Low and Medium Issues Early On
The fastest way to turn developers against security tooling is blocking a sprint release over minor informational warnings. Snyk DeepCode AI catches an enormous breadth of style and minor hygiene quirks. We strongly advise setting --severity-threshold=high for pipeline blocks during the initial rollout. Keep low and medium findings visible in the Snyk Web Dashboard or via SARIF comments, but do not gate merges on them until the codebase reaches an established baseline.
2. Fork PRs and Secret Masking
GitHub Actions intentionally does not expose repository secrets (${{ secrets.SNYK_TOKEN }}) to pull requests initiated from public fork repositories to prevent secret exfiltration. If your project is open source and relies on external contributors, snyk code test will fail with an authentication error. For open-source repos, Snyk's native GitHub App integration handles pull request checks without exposing workflow secrets, whereas internal private repos should rely on the Actions workflow outlined above.
3. Snyk CLI vs. Native GitHub App Integration
Snyk provides a native GitHub App integration that scans PRs without YAML maintenance. However, 365Finds recommends the GitHub Actions CLI method for teams that require unified pipeline orchestration. Running Snyk DeepCode AI inside GitHub Actions gives you complete programmatic control over scan timing, branch logic, customized build matrices, and multi-stage compliance verification.