GitHub Actions: Find Code References in Pull Request
Read time: 3 minutes
Last edited: Aug 28, 2023
Overview
This topic explains how to use the GitHub Actions Find Code References in Pull Request (PR) integration. The GitHub Actions Find Code References in Pull Request integration lets you determine if you modified any feature flags in a PR as a step in your GitHub workflow jobs and use those values in later steps.
The action will post a comment to the PR that lists the flags found in the diff:

Prerequisites
To complete this procedure, you must have the following prerequisites:
- An API access token with read permissions for the specified project. The token should be stored as a repository secret titled
LD_ACCESS_TOKEN
. To learn more, read API access tokens.
Setting up Find Code References in Pull Request GitHub Action
To set up the Find Code References in Pull Request GitHub Action:
- Navigate to your GitHub repo.
- Navigate to Settings, then Secrets.
- Click Add a new secret.
- Enter your access token into the field.
- Click Save secret.
- Return to your GitHub repository to create a new Actions workflow.
If you already have a workflow file that runs on pull_request
you can add the new find flags job to that file.
If you don't already have a workflow file, create a new file titled pull_request.yml
in the .github/workflows
directory of your repository. Paste the find-flags
job declaration below into the Edit file section.
on: pull_requestjobs:find-flags:runs-on: ubuntu-latestname: Find LaunchDarkly feature flags in diffsteps:- name: Checkoutuses: actions/checkout@v3- name: Find flagsuses: launchdarkly/find-code-references-in-pull-request@v1.0.0id: find-flagswith:project-key: defaultenvironment-key: productionaccess-token: ${{ secrets.LD_ACCESS_TOKEN }}repo-token: ${{ secrets.GITHUB_TOKEN }}
We strongly recommend that you update the uses
attribute value to reference the most recent tag in the launchdarkly/find-code-references-in-pull-request repository. This pins your workflow to a particular version of the launchdarkly/find-code-references-in-pull-request
action.
- Commit this file to the default branch of your repository.
Using output in subsequent steps
In addition to posting a comment on the PR, the action will output details about how many and which flags were identified in the diff. GitHub actions cast all value types to string in the output.
Read more about Find Code References in Pull Request outputs.
on: pull_requestjobs:find-feature-flags:runs-on: ubuntu-latestname: Find LaunchDarkly feature flags in diffsteps:- name: Checkoutuses: actions/checkout@v3- name: Find flagsuses: launchdarkly/find-code-references-in-pull-request@v1.0.0id: find-flagswith:project-key: defaultenvironment-key: productionaccess-token: ${{ secrets.LD_ACCESS_TOKEN }}repo-token: ${{ secrets.GITHUB_TOKEN }}# Add or remove labels on PRs if any flags have changed- name: Add labelif: steps.find-flags.outputs.any-changed == 'true'run: gh pr edit $PR_NUMBER --add-label ld-flagsenv:GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}PR_NUMBER: ${{ github.event.pull_request.number }}- name: Remove labelif: steps.find-flags.outputs.any-changed == 'false'run: gh pr edit $PR_NUMBER --remove-label ld-flagsenv:GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}PR_NUMBER: ${{ github.event.pull_request.number }}
Troubleshooting
After you create the workflow, you can confirm that it's working correctly by pushing an empty commit 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.