GitHub code references
Read time: 2 minutes
Last edited: May 23, 2022
Code references is available to customers on an Enterprise plan. To learn more, read about our pricing. To upgrade your plan, contact Sales.
Overview
This topic explains how to use the ld-find-code-refs utility with GitHub Actions to automatically populate code references in LaunchDarkly. To learn more about the utility and check for the latest version, read LaunchDarkly Code References with GitHub code references.
Prerequisites
To complete this procedure, you must have the following prerequisites:
- An API access token with Writer permissions, stored as a repository secret titled
LD_ACCESS_TOKEN. To learn more, read API access tokens.
Setting up GitHub code references
To set up GitHub code references:
- Log into GitHub and navigate to your repo.
- Navigate to Settings > Secrets and click Add a new secret.
- Paste in your access token to the field that appears and click Save secret.
- Return to your GitHub repository to create a new Actions workflow.
- If you already have an
action.ymlfile: Copy and paste thelaunchDarklyCodeReferencesjob declaration below into the jobs section in youraction.ymlfile. - If you don't already have a workflow file: Create a new file titled
action.ymlin the.github/workflowsdirectory of your repository. Paste the following code in the Edit file section:
on: pushname: Example Workflowjobs:launchDarklyCodeReferences:name: LaunchDarkly Code Referencesruns-on: ubuntu-lateststeps:- uses: actions/checkout@v2with:fetch-depth: 11 # This value must be set if the lookback configuration option is not disabled for find-code-references. Read more: https://github.com/launchdarkly/ld-find-code-refs#searching-for-unused-flags-extinctions- name: LaunchDarkly Code Referencesuses: launchdarkly/find-code-references@v2.5.7with:githubToken: ${{ secrets.GITHUB_TOKEN }}accessToken: ${{ secrets.LD_ACCESS_TOKEN }}projKey: YOUR_PROJECT_KEY
We strongly recommend that you update the second uses attribute value to reference the most recent tag in the launchdarkly/find-code-references repository. This pins your workflow to a particular version of the launchdarkly/find-code-references action.
- Commit this file under a new branch and submit as a PR to your code reviewers to be merged into your main branch.
You do not need to have this branch merged into the main branch for code references to appear in the LaunchDarkly user interface (UI). Code references will appear for this newly created branch.
As shown in the example above, the workflow should run on the push event and contain an action provided by the launchdarkly/find-code-references repository.
Include the LD_ACCESS_TOKEN as a secret, and include a new environment variable containing your LaunchDarkly project key. This is represented by YOUR_PROJECT_KEY in the above example. You can find your project key on the Projects tab under Account Settings, immediately following the project name. Do not use an environment's SDK key, mobile key, or client-side ID for this purpose.
Troubleshooting
After you create the workflow, you can confirm that it's working correctly by creating a new pull request with the workflow file and verifying that the newly created action succeeds.
If the action fails, there may be a problem with your configuration. To investigate, review the action's logs to view any error messages.
Additional configuration options
You can configure ld-find-code-refs with additional inputs to enable more functionality.
In addition to inputs, you can skip searching files and directories using a .ldignore file.
You can also use the .launchdarkly/coderefs.yaml file for advanced configuration options, such as configuring custom delimiters and aliases for your flag keys.
This table explains the configuration variables:
| Variable | Description | Required |
|---|---|---|
accessToken | A LaunchDarkly access token with Writer-level access, or access to the code-reference-repository custom role resource. Supply this as a GitHub repository secret. | Yes |
projKey | A LaunchDarkly project key. Found under Account Settings > Projects in the flag dashboard. | Yes |
repoName | Sets the repository name for this project. This is useful if you have a single monorepo containing multiple YAML configurations each mapping to its own projKey. Each YAML configuration must have a unique LD_PROJ_KEY and LD_REPO_NAME combination so it displays correctly in the flag dashboard. Defaults to the current Github repository. | No |
contextLines | The number of context lines above and below a code reference for the flag finder to send to LaunchDarkly. If < 0, no source code will be sent to LaunchDarkly. If 0, only the lines containing flag references will be sent. If > 0, will send that number of context lines above and below the flag reference. A maximum of 5 context lines may be provided. Defaults to 2. | No |
baseUri | Set the base uniform resource identifier (URI) of the LaunchDarkly server for this configuration. Defaults to https://app.launchdarkly.com. | No |
allowTags | Enables storing references for tags. The tag is listed as a branch. Defaults to false. | No |
debug | Enables verbose default logging. Defaults to false. | No |
ignoreServiceErrors | If enabled, the scanner will terminate with exit code 0 when the LaunchDarkly API is unreachable or returns an unexpected response. Defaults to false. | No |
lookback | Sets the number of Git commits to search in history for whether a feature flag was removed from code. May be set to 0 to disabled this feature. Setting this option to a high value will increase search time. Defaults to 10. | No |