Jenkins PR gating using Local Scanner
The Aikido Local Scanner can be used to enforce security gates in your CI pipeline.
PR gating ensures that new code meets your security standards before it is merged into the default branch. It scans only the changes introduced in the pull request. If the scan detects new issues that meet or exceed your configured severity threshold, the CI pipeline fails.
This prevents new vulnerabilities from being introduced while allowing existing findings to be addressed separately.
To enable PR gating, add the --fail-on <severity> option to select your preferred severity level. Then, add the --gating-mode pr option to signify that you wish to perform PR gating. You must also specify the base (--base-commit-id <commit-id>) and head commit (--head-commit-id <commit-id>). If there a scan was previously performed on the base commit id, the scan results will be compared to those. If not, the results will be compared to the most recent scan on your default branch.
For general information about setting up the Local Scanner in Jenkins environment, check out this article.
How it works
There are three moving parts:
Integration between your SCM and Jenkins.
A Multibranch Pipeline job with the Discover pull requests behavior. This makes Jenkins build each open pull request. On a PR build, Jenkins checks out the source branch and exposes the standard change-request environment variables (
CHANGE_ID,CHANGE_TARGET,CHANGE_BRANCH).The Aikido Local Scanner, run from your
Jenkinsfilein--gating-mode pr.
PR opened/updated ──► SCM sends webhook ──► Jenkins Multibranch PR build
│
▼
aikido-local-scanner --gating-mode pr
│
new issues ≥ --fail-on? ── yes ──► build FAILS
│ │
no ▼
▼ build status = FAILED
build PASSES │
│ ▼
▼ SCM blocks merge
SCM checks passMultibranch Pipeline setup
Pull request builds require a Multibranch Pipeline job.
In Jenkins, select New Item → Multibranch Pipeline, name it, and select OK.
Under Branch Sources, select Add source → Select your SCM.
Choose your repository.
Under Behaviors, select Add → Discover pull requests and disable Discover branches.
Under Scan Multibranch Pipeline Triggers, enable the webhook trigger → Pull request opened or source branch updated option.
Leave Build Configuration as by Jenkinsfile, with Script Path =
Jenkinsfile.Select Save. Jenkins scans the repository and starts a build for each open pull request.
Add Aikido PR gating stage to your Jenkinsfile
Add (or extend) a Jenkinsfile at the root of your repository. The stage below runs only on pull request builds, derives the base and head commits from Git, and runs the scanner in PR gating mode. The scanner's non-zero exit on a failing gate is what fails the build.
If the scanner binary is installed on the agent instead of Docker, replace the docker run … block with:
--fail-on criticalsets the severity threshold. Change it tohigh,medium, orlowto gate more strictly.
Setup the gate enforcement in your SCM to match your needs.
Last updated
Was this helpful?