For the complete documentation index, see llms.txt. This page is also available as Markdown.

Integrate with GitLab CI/CD

How to integrate Snyk API and Web with GitLab CI/CD

This guide provides step-by-step instructions for integrating Snyk API & Web into your GitLab CI/CD pipelines.

Overview

This guide focuses on using the Snyk CLI to run scans. The following examples cover the complete end-to-end journey, from configuring your targets to running scans in different scenarios.

Prerequisites

Configure your scan targets and credentials in the Snyk application before you begin.

Create a target in Snyk

In the Snyk app, navigate to the Targets menu and click Add. Fill out the form and click Add to create the new target.

During this process, Snyk checks connectivity. If your target is internal or not yet deployed, you can bypass any warnings and add the target. For more details, visit How to add a Target.

Before you configure the integration in GitLab, retrieve the unique target ID from Snyk.

  1. In your Snyk dashboard, select Targets.

  2. From the target list, select the target you want to integrate.

  3. In your browser's address bar, copy the target ID. This is the string of characters immediately following /target/ in the URL.

After you create a target, you must verify the target's domain. Otherwise, your scans are limited to lightning scans. To learn more, visit the importance of domain ownership verification.

Create a Snyk API key

You need an API key with permissions to start a scan on your target. For instructions, visit How to generate an API key.

Step 1: Add your API key and target ID to GitLab

To run a scan, your pipeline must authenticate with Snyk and know which target to scan. Configure your Snyk API key and the specific target ID as secure CI/CD variables in your GitLab project.

  1. From your GitLab project side menu, navigate to Settings > CI/CD.

  2. Find the Variables section and expand it.

  3. Click Add variable and create an entry for your Snyk API key (for example, PROBELY_API_KEY).

  4. Click Add variable again to create a second entry for your target ID (for example, TARGET_ID).

Step 2: Configure your pipeline

Create a .gitlab-ci.yml file at the root of your repository and add one of the following code examples based on your use case. You can also find all of them in the Snyk API & Web CI/CD examples repository on GitHub.

Important note on these examples

The following YAML configurations are scanning steps to incorporate into your existing .gitlab-ci.yml file.

For example, your pipeline might already have steps to build your code, deploy to a QA environment, and run automated tests. You can add the Snyk scan as another step at any point that makes sense for your workflow, such as after you deploy to QA or staging.

Run a scan on a target in non-blocking mode

This is the simplest way to get started. It runs a scan on a remotely accessible environment without blocking the pipeline.

Run a scan on a target in blocking mode

This configuration builds on the first example by adding steps to poll the scan status and block the pipeline if high-severity vulnerabilities are found.

Run a scan on an ephemeral (dynamic) target in blocking mode

This is a more advanced configuration for building, deploying, and scanning an application in a temporary environment. The pipeline creates the environment for a specific purpose and then automatically destroys it during the run.

Ephemeral environments require an agent token, target hostname, and target URL. Store them as variables for better security.

You also need to create a scanning agent in Snyk and configure your target to use it. This process requires the scanning-agent/farcasterd-linux-amd64-0.4.3 file. For detailed instructions, visit How to install a Scanning Agent and How to scan internal applications.

This code example uses Docker to create ephemeral environments. However, you can use any other solution to create your environment.

Step 3: Run the pipeline and view the results

After you commit your .gitlab-ci.yml file, you can run the pipeline in GitLab to test the integration.

After the scan completes, you can view the detailed findings in your Snyk dashboard.

Last updated

Was this helpful?