LaunchDarkly Developer Documentation

Get started in under 30 minutes.
LaunchDarkly provides feature flags as a service for Java · Python · Ruby · Go · Node.js · PHP · .NET. Control feature launches -- who sees what and when -- without multiple code deploys. Easy dashboard for phased rollouts, targeting and segmenting.
Need more help? write us at support@launchdarkly.com

Get Started    Documentation

GitLab CI

Overview

This topic explains how to set up and configure the Gitlab CI to use with LaunchDarkly.

You can use the ld-find-code-refs utility with GitLab CI to automatically populate code references in LaunchDarkly.

Follow the procedure below to create a GitLab CI configuration using LaunchDarkly's code references executable.

Prerequisites

To complete this procedure, you must have the following prerequisites:

Setting up GitLab CI

Here's how to set up the GitLab CI:

  1. Navigate to your GitLab project's CI / CD settings by clicking through Your project > Settings > CI/CD.
  2. Expand Variables.
  3. Create a variable called LD_ACCESS_TOKEN. Use the same value as your LaunchDarkly access token. Click the toggle to set the variable to Masked.
  4. Create a variable called LD_PROJECT_KEY. Use your LaunchDarkly project's key as the value. To learn more about setting variables, read GitLab's documentation.
The GitLab Variables screen with masked `LD_ACCESS_TOKEN` and `LD_PROJECT_KEY` created.

The GitLab Variables screen with masked LD_ACCESS_TOKEN and LD_PROJECT_KEY created.

  1. Open your .gitlab-ci.yml file. This file defines your project's CI/CD pipeline. To learn more about getting started with GitLab CI, read GitLab's documentation.
  2. Copy and paste the following into .gitlab-ci.yml. No changes to the script are needed if your pipeline runs on Alpine Linux. If apk is unavailable in your environment then you'll need to modify the first three steps to use a different package manager.
find-launchdarkly-code-refs:
  stage: deploy
  script:
    # Update package manager
    - apk update
    # Install packages needed to download the Find Code Refs tool
    - apk add curl grep wget 
    # Install packages used by the Find Code Refs tool
    - apk add the_silver_searcher git
    # Download the Find Code Refs tool
    - curl -s https://api.github.com/repos/launchdarkly/ld-find-code-refs/releases/latest
       | grep browser_download_url | grep linux | grep amd64
       | cut -d '"' -f 4
       | wget -qi -
    # Unpackage the Find Code Refs tool
    - tar xvzf ld-find-code-refs*.tar.gz
    # Find LaunchDarkly code references in your source code
    - ./ld-find-code-refs
       -accessToken $LD_ACCESS_TOKEN
       -projKey $LD_PROJECT_KEY
       -dir $CI_PROJECT_DIR
       -repoName $CI_PROJECT_NAME
       -repoUrl https://gitlab.com/$CI_PROJECT_PATH
       -branch $CI_COMMIT_REF_NAME
       -updateSequenceId $CI_PIPELINE_IID
       -commitUrlTemplate https://gitlab.com/${CI_PROJECT_PATH}/commit/"\${sha}"
       -hunkUrlTemplate https://gitlab.com/${CI_PROJECT_PATH}/blob/"\${sha}/\${filePath}#L\${lineNumber}"

How the script works

When executed, this script downloads and runs the ld-find-code-refs utility.

This script:

  • Installs the necessary dependencies,
  • Downloads and unpacks the utility, and
  • Runs the utility with the previously-set variables, as well as GitLab-specific configurations.

The find-launchdarkly-code-refs script runs in GitLab's deploy phase. As written, find-launchdarkly-code-refs runs concurrent to other scripts in the deploy stage. We positioned the script this way so problems running ld-find-code-refs won't block the deployment pipeline.

In the example .gitlab-ci.yml below, the find-launchdarkly-code-refs script runs as a part of a project's pipeline.

image: alpine:latest

build1:
  stage: build
  script:
    - echo "Build something"

test1:
  stage: test
  script:
    - echo "Test something"
    
deploy1:
  stage: deploy
  script:
    - echo "Deploy something"
    
find-launchdarkly-code-refs:
  stage: deploy
  script:
    # Update package manager
    - apk update
    # Install packages needed to download the Find Code Refs tool
    - apk add curl grep wget 
    # Install packages used by the Find Code Refs tool
    - apk add the_silver_searcher git
    # Download the Find Code Refs tool
    - curl -s https://api.github.com/repos/launchdarkly/ld-find-code-refs/releases/latest
       | grep browser_download_url | grep linux | grep amd64
       | cut -d '"' -f 4
       | wget -qi -
    # Unpackage the Find Code Refs tool
    - tar xvzf ld-find-code-refs*.tar.gz
    # Find LaunchDarkly code references in your source code
    - ./ld-find-code-refs
       -accessToken $LD_ACCESS_TOKEN
       -projKey $LD_PROJECT_KEY
       -dir $CI_PROJECT_DIR
       -repoName $CI_PROJECT_NAME
       -repoUrl https://gitlab.com/$CI_PROJECT_PATH
       -branch $CI_COMMIT_REF_NAME
       -updateSequenceId $CI_PIPELINE_IID
       -commitUrlTemplate https://gitlab.com/${CI_PROJECT_PATH}/commit/"\${sha}"
       -hunkUrlTemplate https://gitlab.com/${CI_PROJECT_PATH}/blob/"\${sha}/\${filePath}#L\${lineNumber}"

When the jobs run in the pipeline, they display like this:

A screenshot of the jobs running.

A screenshot of the jobs running.

Additional configuration options

There are more configuration options for ld-find-code-refs.

To learn more, read Optional arguments.


GitLab CI


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.