Add Semgrep manually to CI providers
- You have gained the necessary resource access and permissions required for deployment.
- You have created a Semgrep account and organization.
- For GitHub and GitLab users: You have connected your source code manager.
- Optionally, you have set up SSO.
This guide walks you through creating a Semgrep job for CI providers for which Semgrep AppSec Platform has no explicit guidance. Without explicit guidance, you must manually make a CI configuration file yourself.
Check Add Semgrep to CI first to ensure that this guide applies to your CI provider.
Skip this guide if you have already configured a CI job.
The steps provided here are known to work with the following CI providers:
- AppVeyor
- Bamboo
- Bitrise
- Buildbot
- Codeship
- Codefresh
- Drone CI
- Nomad
- TeamCity CI
- Travis CI
General steps
The following steps provide an overview of the process. View the succeeding sections for detailed instructions.
- Create a
SEMGREP_APP_TOKEN
. - Add this token as a credential, secret, or token into your CI provider.
- Create a CI job that runs Semgrep; this step is typically achieved by committing a CI configuration file. The syntax of the configuration file depends on your CI provider..
- The CI job can automatically start to run depending on your configuration. If the job does not start, run the job through the CI provider's interface or by committing code.
- Semgrep detects the
SEMGREP_APP_TOKEN
, sends it to Semgrep AppSec Platform for verification, and if verified, findings are sent to Semgrep AppSec Platform. - Define additional environment variables to enable other Semgrep AppSec Platform features. This is done last because it is easier to troubleshoot modifications to jobs after ensuring that the base CI job runs correctly.
The next sections go over these steps in detail.
Create a SEMGREP_APP_TOKEN
To create a SEMGREP_APP_TOKEN
, follow these steps:
- Sign in to Semgrep AppSec Platform.
- Click Settings > Tokens.
- Click Create new token.
- Copy the name and value, then click Save.
- Store the token value into your CI provider. Tokens can also be referred to as
secrets
,credentials
, orsecure variables
. The steps to do this vary depending on your CI provider.
Create a Semgrep CI job
- Add Semgrep to your CI pipeline. Do either of the following:
- Reference or add the Semgrep Docker image. This is the recommended method.
- Add
pip install semgrep
into your configuration file as a step or command, depending on your CI provider's syntax.
- Add
semgrep ci
as a step or command. - Set the
SEMGREP_APP_TOKEN
environment variable within your configuration file.
The following example is a bitbucket-pipelines.yml
file that adds Semgrep through the Docker image:
Add Semgrep through the Docker image.
image: atlassian/default-image:latest
pipelines:
default:
- parallel:
- step:
name: 'Run Semgrep scan with current branch'
deployment: dev
# Reference the Semgrep Docker image:
image: semgrep/semgrep
script:
# You need to set the token as an environment variable
# (see Create a `SEMGREP_APP_TOKEN` section).
- export $SEMGREP_APP_TOKEN
# Run semgrep ci:
- semgrep ci
The next example is a Jenkinsfile
configuration that adds Semgrep by installing it:
Add Semgrep by installing it.
pipeline {
agent any
stages {
stage('Semgrep-Scan') {
environment {
// You need to set the token as an environment variable
// (see Create a `SEMGREP_APP_TOKEN` section).
SEMGREP_APP_TOKEN = credentials('SEMGREP_APP_TOKEN')
}
steps {
// Install and run Semgrep:
sh 'pip3 install semgrep'
sh 'semgrep ci'
}
}
}
}
Run the job
Depending on your CI provider and configuration, the job runs automatically. Otherwise, trigger the job by committing code or opening a PR or MR.
Verify the connection
To verify that your Semgrep CI job is connected to Semgrep AppSec Platform:
- Go to your Semgrep AppSec Platform Projects page.
- Verify that your repository is listed on the Projects page and that Semgrep AppSec Platform is running a scan.
Troubleshoot your CI job
Semgrep attempts to automatically detect certain CI values, such as your repository's name and URL. These values are used to provide context to findings in Semgrep AppSec Platform and hyperlinks to the code that generated the finding.
Refer to the following table for common issues and the corresponding environment variables you can set to fix them:
Issue | Environment variable to set | Affected CI providers |
---|---|---|
Can't establish a connection to Semgrep AppSec Platform. | SEMGREP_APP_TOKEN | Must be set for all CI providers. |
Semgrep doesn't scan your PRs or MRs. | SEMGREP_BASELINE_REF | Required for CI providers except GitHub Actions or GitLab CI/CD. | Can't click hyperlinks to your repository from Semgrep AppSec Platform, nor can Semgrep AppSec Platform create PR or MR comments. | SEMGREP_REPO_NAME | Set these environment variables as needed to troubleshoot broken links for any CI provider except GitHub Actions and GitLab CI/CD. |
SEMGREP_REPO_URL | ||
SEMGREP_BRANCH | ||
SEMGREP_JOB_URL | ||
SEMGREP_COMMIT | ||
SEMGREP_PR_ID | Required to enable hyperlinks for Azure Pipelines. |
Data collected by Semgrep AppSec Platform
When running in CI, Semgrep runs fully in the CI build environment. Unless you have explicitly granted code access to Semgrep, your code is not sent anywhere.
- Semgrep AppSec Platform collects findings data, which includes the line number of the code match, but not the code. It is hashed using a one-way hashing function.
- Findings data is used to generate line-specific hyperlinks to your source code management system and support other Semgrep functions.
Next steps
You've set up Semgrep to scan in your repository and send findings after each scan. Your core deployment is almost complete.
Remaining steps include:
- Optional: Customize your CI job.
- For software composition analysis (SCA) scans using Jenkins or Maven: Set up SCA scans for your infrastructure.
- Set up diff-aware scanning for feature branches (non-trunk branches) when a pull or merge request is open. This is a prerequisite to receiving PR or MR comments. See Set up diff-aware scans.
- Set up PR or MR comments, which post findings to developers in your SCM. This involves developers in the security process as active participants. See PR or MR comments for next steps.
Not finding what you need in this doc? Ask questions in our Community Slack group, or see Support for other ways to get help.