• HOME
  • INTEGRATIONS
  • SDK
  • API DOCS
No results for ""
EXPAND ALL
CLOSE
launchdarkly.com

EDIT ON GITHUB

GitLab CI

Read time: 1 minute
Last edited: Feb 3, 2020

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:

  • A personal API access token with writer permissions. To learn more, read Personal API access tokens.

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.
1find-launchdarkly-code-refs:
2 stage: deploy
3 script:
4 # Update package manager
5 - apk update
6 # Install packages needed to download the Find Code Refs tool
7 - apk add curl grep wget
8 # Install packages used by the Find Code Refs tool
9 - apk add the_silver_searcher git
10 # Download the Find Code Refs tool
11 - curl -s https://api.github.com/repos/launchdarkly/ld-find-code-refs/releases/latest
12 | grep browser_download_url | grep linux | grep amd64
13 | cut -d '"' -f 4
14 | wget -qi -
15 # Unpackage the Find Code Refs tool
16 - tar xvzf ld-find-code-refs*.tar.gz
17 # Find LaunchDarkly code references in your source code
18 - ./ld-find-code-refs
19 -accessToken $LD_ACCESS_TOKEN
20 -projKey $LD_PROJECT_KEY
21 -dir $CI_PROJECT_DIR
22 -repoName $CI_PROJECT_NAME
23 -repoUrl https://gitlab.com/$CI_PROJECT_PATH
24 -branch $CI_COMMIT_REF_NAME
25 -updateSequenceId $CI_PIPELINE_IID
26 -commitUrlTemplate https://gitlab.com/${CI_PROJECT_PATH}/commit/${sha}
27 -hunkUrlTemplate https://gitlab.com/${CI_PROJECT_PATH}/blob/${sha}/

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.

1image: alpine:latest
2
3build1:
4 stage: build
5 script:
6 - echo "Build something"
7
8test1:
9 stage: test
10 script:
11 - echo "Test something"
12
13deploy1:
14 stage: deploy
15 script:
16 - echo "Deploy something"
17
18find-launchdarkly-code-refs:
19 stage: deploy
20 script:
21 # Update package manager
22 - apk update
23 # Install packages needed to download the Find Code Refs tool
24 - apk add curl grep wget
25 # Install packages used by the Find Code Refs tool
26 - apk add the_silver_searcher git
27 # Download the Find Code Refs tool
28 - curl -s https://api.github.com/repos/launchdarkly/ld-find-code-refs/releases/latest
29 | grep browser_download_url | grep linux | grep amd64
30 | cut -d '"' -f 4
31 | wget -qi -
32 # Unpackage the Find Code Refs tool
33 - tar xvzf ld-find-code-refs*.tar.gz
34 # Find LaunchDarkly code references in your source code
35 - ./ld-find-code-refs
36 -accessToken $LD_ACCESS_TOKEN
37 -projKey $LD_PROJECT_KEY
38 -dir $CI_PROJECT_DIR
39 -repoName $CI_PROJECT_NAME
40 -repoUrl https://gitlab.com/$CI_PROJECT_PATH
41 -branch $CI_COMMIT_REF_NAME
42 -updateSequenceId $CI_PIPELINE_IID
43 -commitUrlTemplate https://gitlab.com/${CI_PROJECT_PATH}/commit/${sha}
44 -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.