Find Code References in Pull Request
Read time: 3 minutes
Last edited: Jul 03, 2024
Overview
This topic explains how to use the Find Code References in Pull Request (PR) GitHub Actions integration. The 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:
The action will optionally create a GitHub pull request flag link for any modified flags. To learn more, read Flag links.
Prerequisites
To complete this procedure, you must have the following prerequisites:
This action requires a LaunchDarkly access token with:
- Read access for the designated
project-key
- (Optional) the
createFlagLink
action, if you have set thecreate-flag-links
input totrue
Set up Find Code References in Pull Request
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@v4- name: Find flagsuses: launchdarkly/find-code-references-in-pull-request@v2id: 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.
Use 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@v4- name: Find flagsuses: launchdarkly/find-code-references-in-pull-request@v2id: 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.