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.
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.
Here's how to set up the GitLab CI:
- Navigate to your GitLab project's CI / CD settings by clicking through Your project > Settings > CI/CD.
- Expand Variables.
- 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. - 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.
- 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. - Copy and paste the following into
.gitlab-ci.yml
. No changes to the script are needed if your pipeline runs on Alpine Linux. Ifapk
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}"
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:
There are more configuration options for ld-find-code-refs
.
To learn more, read Optional arguments.