Skip to main content

Run Semgrep in Jenkins when using Bitbucket as the source code manager

To scan your code hosted by Bitbucket with Semgrep using a Jenkins project or pipeline, you must:

  1. Set up webhooks to connect Jenkins to Bitbucket.
  2. Configure the Jenkins project or pipeline to run Semgrep.

Set up webhooks to allow triggering events from Bitbucket to Jenkins

Webhooks are required to connect your Bitbucket source code manager (SCM) to Jenkins.

Prerequisites

You must install the Bitbucket Push and Pull Request plugin on your Jenkins server. This method requires that your Jenkins instance be compatible with this plugin.

  1. Log in to Bitbucket, and go to your repository.
  2. In your Bitbucket repository, go to Repository Settings > Webhooks > Add webhook.
  3. Enter a Title for your webhook.
  4. Enter the URL for your Jenkins instance using the following pattern: https://<YOUR_JENKINS_SERVER>/bitbucket-hook/.
  5. Add the following Triggers:
    1. In the Repository list, select Push.
    2. In the Pull request list, select Created and Updated.

Configure Jenkins to run Semgrep

  1. Sign in to Jenkins.
  2. From the Jenkins Dashboard click on create a New Item.
  3. Enter a project name, select Pipeline option, and click OK.
  4. In the General > Triggers section, select Build with BitBucket Push and Pull Request Plugin.
  5. Create the Triggers:
    1. Click Add.
    2. Select one of the following: Bitbucket Cloud Pull Request, Bitbucket Server Pull Request, or Push.
    3. In Select an Action, select Created.
    4. Click Add again, and select the same trigger as before: Bitbucket Cloud Pull Request, Bitbucket Server Pull Request, or Push.
    5. In Select an Action, select Updated.
  6. Go to the Pipeline section. In Definition, select Pipeline script from SCM.
    1. In SCM, select Git.
    2. In Repositories > Repository URL, enter your Bitbucket repository URL.
    3. In Branch Specifier (blank for 'any'), enter the name of your main branch.
    4. In Script Path, enter Jenkinsfile.
  7. Click Save.

Create and add the Jenkinsfile to your repository

Create the Jenkinsfile in your Bitbucket repository. The file must define the logic to start:

  • Diff-aware scans if the scan is started in the context of a pull request
  • Full scans if you push changes to the main branch.

The following code snippets are sample Jenkinsfile that defines both of these actions. Choose the file for your deployment based on whether you're using Bitbucket Cloud or Bitbucket Data Center.

pipeline {
agent any
environment {
SEMGREP_APP_TOKEN = credentials('SEMGREP_APP_TOKEN')
SEMGREP_BASELINE_REF = "origin/main"
}
stages {
stage('Semgrep-Scan') {
steps {
script {
if (env.BITBUCKET_PULL_REQUEST_ID) {
echo "Semgrep diff scan"
sh '''git checkout ${BITBUCKET_PULL_REQUEST_LATEST_COMMIT_FROM_SOURCE_BRANCH}'''
sh '''git fetch origin +ref/heads/*:refs/remotes/origin/*'''
sh '''docker run \
-e SEMGREP_APP_TOKEN=$SEMGREP_APP_TOKEN \
-e SEMGREP_PR_ID=${BITBUCKET_PULL_REQUEST_ID} \
-e SEMGREP_BASELINE_REF=$SEMGREP_BASELINE_REF \
-v "$(pwd):$(pwd)" --workdir $(pwd) \
semgrep/semgrep semgrep ci'''
}
else {
echo "Semgrep full scan"
sh '''docker run \
-e SEMGREP_APP_TOKEN=$SEMGREP_APP_TOKEN \
-v "$(pwd):$(pwd)" --workdir $(pwd) \
semgrep/semgrep semgrep ci'''
}
}
}
}
}
}

Note that:

Test the implementation

To ensure that Semgrep scans correctly in your Jenkins pipeline or project:

  1. Commit a change to your repository, and create a pull request. This automatically runs a Semgrep diff-aware scan in Jenkins. Note that the job can fail if there are blocking findings as a result of the scan.
  2. Merge the pull request to commit the changes to main. This triggers a full scan in Jenkins.

Not finding what you need in this doc? Ask questions in our Community Slack group, or see Support for other ways to get help.