GitLab CI
Read time: 1 minute
Last edited: Mar 04, 2021
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:
- An API access token with
writer
permissions. To learn more, read API access tokens.
Setting up GitLab CI
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. Find the project key in Account Settings > Projects.
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.
1find-launchdarkly-code-refs:2 stage: deploy3 image:4 name: launchdarkly/ld-find-code-refs:2.1.05 entrypoint: [""]6 script:7 - ld-find-code-refs8 --accessToken $LD_ACCESS_TOKEN9 --projKey $LD_PROJECT_KEY10 --dir $CI_PROJECT_DIR11 --repoName $CI_PROJECT_NAME12 --repoUrl $CI_PROJECT_URL13 --branch $CI_COMMIT_REF_NAME14 --updateSequenceId $CI_PIPELINE_IID15 --commitUrlTemplate https://gitlab.com/${CI_PROJECT_PATH}/commit/"\${sha}"16 --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
docker image.
This script runs the docker image with 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:latest23build1:4 stage: build5 script:6 - echo "Build something"78test1:9 stage: test10 script:11 - echo "Test something"1213deploy1:14 stage: deploy15 script:16 - echo "Deploy something"1718find-launchdarkly-code-refs:19 stage: deploy20 image:21 name: launchdarkly/ld-find-code-refs:2.1.022 entrypoint: [""]23 script:24 - ld-find-code-refs25 --accessToken $LD_ACCESS_TOKEN26 --projKey $LD_PROJECT_KEY27 --dir $CI_PROJECT_DIR28 --repoName $CI_PROJECT_NAME29 --repoUrl $CI_PROJECT_URL30 --branch $CI_COMMIT_REF_NAME31 --updateSequenceId $CI_PIPELINE_IID32 --commitUrlTemplate https://gitlab.com/${CI_PROJECT_PATH}/commit/"\${sha}"33 --hunkUrlTemplate https://gitlab.com/${CI_PROJECT_PATH}/blob/"\${sha}/\${filePath}#L\${lineNumber}"
When the jobs run in the pipeline, they display like this:
Additional configuration options
There are more configuration options for ld-find-code-refs
.
You can exclude files and directories from searches with an .ldignore
file.
You can use the .launchdarkly/config.yaml
file for advanced configuration, such as configuring custom delimiters and aliases for your flag keys.
To learn more, read the configuration documentation.