Toggle LaunchDarkly Feature Flag
Overviewโ
This guide will help you implement a self-service action in Port that allows you to toggle LaunchDarkly feature flags directly from Port. This functionality streamlines feature management by enabling users to control feature flags without leaving Port.
You can implement this action in two ways:
- GitHub workflow: A more flexible approach that allows for complex workflows and custom logic, suitable for teams that want to maintain their automation in Git.
- Synced webhooks: A simpler approach that directly interacts with LaunchDarkly's API through Port, ideal for quick implementation and minimal setup.
Prerequisitesโ
-
Complete the onboarding process.
-
Access to your LaunchDarkly organization with permissions to manage feature flags.
-
A LaunchDarkly API token with permission to toggle feature flags. Learn more
-
Optional - Install Port's LaunchDarkly integration learn more
LaunchDarkly IntegrationThis step is not required for this example, but it will create all the blueprint boilerplate for you, and also ingest and update the catalog in real time with your LaunchDarkly Feature Flags.
Set up data modelโ
If you haven't installed the LaunchDarkly integration, you'll need to create a blueprint for LaunchDarkly feature flags. However, we highly recommend you install the LaunchDarkly integration to have these automatically set up for you.
Create the LaunchDarkly feature flag blueprint
-
Go to your Builder page.
-
Click on
+ Blueprint
. -
Click on the
{...}
button in the top right corner, and choose "Edit JSON". -
Add this JSON schema:
LaunchDarkly Feature Flag Blueprint (Click to expand)
{
"identifier": "launchDarklyFeatureFlag",
"description": "This blueprint represents a feature flag in LaunchDarkly.",
"title": "LaunchDarkly Feature Flag",
"icon": "Launchdarkly",
"schema": {
"properties": {
"kind": {
"type": "string",
"title": "Flag Kind",
"description": "The type of the feature flag (e.g., boolean)."
},
"description": {
"type": "string",
"title": "Description",
"description": "A description of what the flag controls."
},
"creationDate": {
"type": "string",
"format": "date-time",
"title": "Creation Date",
"description": "The date and time when the flag was created."
},
"clientSideAvailability": {
"type": "object",
"title": "Client-Side Availability",
"description": "Availability of the flag for client-side applications."
},
"temporary": {
"type": "boolean",
"title": "Temporary Flag",
"description": "Indicates if the flag is temporary."
},
"tags": {
"type": "array",
"title": "Tags",
"description": "Tags associated with the feature flag."
},
"maintainer": {
"type": "string",
"title": "Maintainer",
"description": "Email address of the maintainer of the flag."
},
"customProperties": {
"type": "object",
"title": "Custom Properties",
"description": "Custom properties associated with the flag."
},
"archived": {
"type": "boolean",
"title": "Archived",
"description": "Indicates if the flag is archived."
},
"deprecated": {
"type": "boolean",
"title": "Deprecated",
"description": "Indicates if the flag is deprecated."
},
"variations": {
"type": "array",
"title": "Variations",
"description": "An array of possible variations for the flag"
}
},
"required": []
},
"mirrorProperties": {},
"calculationProperties": {},
"aggregationProperties": {},
"relations": {
"environments": {
"title": "Environments",
"target": "launchDarklyEnvironment",
"required": false,
"many": true
}
}
} -
Click "Save" to create the blueprint.
Implementationโ
- Synced webhook
- GitHub workflow
You can toggle LaunchDarkly feature flags by leveraging Port's synced webhooks and secrets to directly interact with the LaunchDarkly's API. This method simplifies the setup by handling everything within Port.
Add Port secrets
If you have already installed Port's LaunchDarkly integration, these secrets should already exist in your portal.
To view your existing secrets:
- Click on the
...
button in the top right corner of your Port application. - Choose Credentials, then click on the
Secrets
tab.
To add these secrets to your portal:
-
Click on the
...
button in the top right corner of your Port application. -
Click on Credentials.
-
Click on the
Secrets
tab. -
Click on
+ Secret
and add the following secrets:LAUNCHDARKLY_ACCESS_TOKEN
: Your LaunchDarkly API token
Set up self-service action
Follow these steps to create the self-service action:
-
Head to the self-service page.
-
Click on the
+ New Action
button. -
Click on the
{...} Edit JSON
button. -
Copy and paste the following JSON configuration into the editor.
Toggle LaunchDarkly Feature Flag (Webhook) (Click to expand)
{
"identifier": "toggle_feature_flag_webhook",
"title": "Toggle Feature Flag (Webhook)",
"icon": "Launchdarkly",
"description": "Toggle a LaunchDarkly feature flag using a webhook",
"trigger": {
"type": "self-service",
"operation": "DAY-2",
"userInputs": {
"properties": {
"project_key": {
"description": "LaunchDarkly Project Key",
"title": "Project Key",
"icon": "Launchdarkly",
"type": "string"
},
"flag_key": {
"description": "LaunchDarkly Flag Key (without project suffix)",
"title": "Flag Key",
"icon": "Launchdarkly",
"type": "string"
},
"environment_key": {
"description": "LaunchDarkly Environment Key where the flag exists",
"title": "Environment Key",
"icon": "Launchdarkly",
"type": "string"
},
"flag_state": {
"title": "Flag State",
"description": "Desired state of the feature flag (true for enabled, false for disabled)",
"icon": "Launchdarkly",
"type": "boolean",
"default": true
}
},
"required": [
"project_key",
"flag_key",
"environment_key"
],
"order": [
"project_key",
"flag_key",
"environment_key",
"flag_state"
]
},
"blueprintIdentifier": "launchDarklyFeatureFlag"
},
"invocationMethod": {
"type": "WEBHOOK",
"url": "https://app.launchdarkly.com/api/v2/flags/{{.inputs.project_key}}/{{.inputs.flag_key}}",
"agent": false,
"synchronized": true,
"method": "PATCH",
"headers": {
"Authorization": "{{.secrets.LAUNCHDARKLY_ACCESS_TOKEN}}",
"Content-Type": "application/json"
},
"body": [
{
"op": "replace",
"path": "/environments/{{.inputs.environment_key}}/on",
"value": "{{.inputs.flag_state}}"
}
]
},
"requiredApproval": false
} -
Click
Save
.
Now you should see the Toggle Feature Flag (Webhook)
action in the self-service page. ๐
Create an automation to update entity in port
To keep your catalog updated with the latest feature flag state, you can create an automation that will update the LaunchDarkly feature flag entity in Port immediately after the webhook action completes successfully.
Follow these steps to add the automation:
-
Head to the automation page.
-
Click on the
+ Automation
button. -
Copy and paste the following JSON configuration into the editor.
Update LaunchDarkly feature flag in Port automation (Click to expand)
{
"identifier": "launchDarklyFeatureFlag_sync_after_toggle",
"title": "Sync LaunchDarkly Feature Flag After Toggle",
"description": "Update LaunchDarkly feature flag data in Port after toggling",
"trigger": {
"type": "automation",
"event": {
"type": "RUN_UPDATED",
"actionIdentifier": "toggle_feature_flag_webhook"
},
"condition": {
"type": "JQ",
"expressions": [
".diff.after.status == \"SUCCESS\""
],
"combinator": "and"
}
},
"invocationMethod": {
"type": "UPSERT_ENTITY",
"blueprintIdentifier": "launchDarklyFeatureFlag",
"mapping": {
"identifier": "{{.event.diff.after.entity.identifier}}",
"title": "{{.event.diff.after.entity.title}}",
"properties": {
"kind": "{{.event.diff.after.entity.properties.kind}}",
"description": "{{.event.diff.after.entity.properties.description}}",
"temporary": "{{.event.diff.after.entity.properties.temporary}}",
"tags": "{{.event.diff.after.entity.properties.tags}}",
"archived": "{{.event.diff.after.entity.properties.archived}}",
"deprecated": "{{.event.diff.after.entity.properties.deprecated}}"
}
}
},
"publish": true
} -
Click
Save
.
Now when you execute the webhook action, the feature flag data in Port will be automatically updated with the latest information from LaunchDarkly.
To implement this use-case using a GitHub workflow, follow these steps:
Add GitHub secrets
In your GitHub repository, go to Settings > Secrets and add the following secrets:
LAUNCHDARKLY_ACCESS_TOKEN
- A LaunchDarkly API token with permission to toggle feature flags.PORT_CLIENT_ID
- Your portclient id
How to get the credentials.PORT_CLIENT_SECRET
- Your portclient secret
How to get the credentials.
Add GitHub workflow
Create the file .github/workflows/toggle-feature-flag.yaml
in the .github/workflows
folder of your repository.
We recommend creating a dedicated repository for the workflows that are used by Port actions.
GitHub Workflow (Click to expand)
name: Toggle LaunchDarkly Feature Flag
on:
workflow_dispatch:
inputs:
project_key:
description: 'LaunchDarkly Project Key'
required: true
type: string
environment_key:
description: 'LaunchDarkly Environment Key where the flag exists'
required: true
type: string
flag_state:
description: 'Desired state of the feature flag (true for enabled, false for disabled)'
required: true
type: boolean
port_context:
required: true
description: includes blueprint, run ID, and entity identifier from Port.
jobs:
toggle-feature-flag:
runs-on: ubuntu-latest
steps:
- name: Log Before Toggling
uses: port-labs/port-github-action@v1
with:
clientId: ${{ secrets.PORT_CLIENT_ID }}
clientSecret: ${{ secrets.PORT_CLIENT_SECRET }}
baseUrl: https://api.getport.io
operation: PATCH_RUN
runId: ${{fromJson(inputs.port_context).run_id}}
logMessage: "Attempting to toggle feature flag '${{fromJson(inputs.port_context).entity}}' in '${{ github.event.inputs.environment_key }}' environment to ${{ github.event.inputs.flag_state }}."
- name: Toggle Feature Flag in LaunchDarkly
id: "toggle_feature_flag"
uses: fjogeleit/http-request-action@v1
with:
url: 'https://app.launchdarkly.com/api/v2/flags/${{ github.event.inputs.project_key }}/${{fromJson(inputs.port_context).entity}}'
method: 'PATCH'
customHeaders: '{"Authorization": "${{ secrets.LAUNCHDARKLY_ACCESS_TOKEN }}", "Content-Type": "application/json"}'
data: >-
[{
"op": "replace",
"path": "/environments/${{ github.event.inputs.environment_key }}/on",
"value": ${{ github.event.inputs.flag_state }}
}]
- name: Convert CreationDate to date-time format
id: format_date
run: |
timestamp="${{ fromJson(steps.toggle_feature_flag.outputs.response).creationDate }}"
epoch_seconds=$(($timestamp / 1000))
formatted_date=$(date -u -d "@${epoch_seconds}" +"%Y-%m-%dT%H:%M:%SZ")
echo "creationDate=${formatted_date}" >> $GITHUB_OUTPUT
echo "creationDate=${formatted_date}"
- name: Log Before Upserting Entity
uses: port-labs/port-github-action@v1
with:
clientId: ${{ secrets.PORT_CLIENT_ID }}
clientSecret: ${{ secrets.PORT_CLIENT_SECRET }}
baseUrl: https://api.getport.io
operation: PATCH_RUN
runId: ${{fromJson(inputs.port_context).run_id}}
logMessage: "Moving on to upsert updates to Port"
- name: UPSERT Entity
uses: port-labs/port-github-action@v1
with:
identifier: "${{ fromJson(steps.toggle_feature_flag.outputs.response).key }}"
title: "${{ fromJson(steps.toggle_feature_flag.outputs.response).description }}"
blueprint: ${{fromJson(inputs.port_context).blueprint}}
properties: |-
{
"kind": "${{ fromJson(steps.toggle_feature_flag.outputs.response).kind }}",
"description": "${{ fromJson(steps.toggle_feature_flag.outputs.response).description }}",
"creationDate": "${{ steps.format_date.outputs.creationDate }}",
"includeInSnippet": ${{ fromJson(steps.toggle_feature_flag.outputs.response).includeInSnippet }},
"clientSideAvailability": ${{ toJson(fromJson(steps.toggle_feature_flag.outputs.response).clientSideAvailability) }},
"temporary": ${{ fromJson(steps.toggle_feature_flag.outputs.response).temporary }},
"tags": ${{ toJson(fromJson(steps.toggle_feature_flag.outputs.response).tags) }},
"maintainer": ${{ toJson(fromJson(steps.toggle_feature_flag.outputs.response)._maintainer) }},
"environments": ${{ toJson(fromJson(steps.toggle_feature_flag.outputs.response).environments) }},
"variations": ${{ toJson(fromJson(steps.toggle_feature_flag.outputs.response).variations) }},
"customProperties": ${{ toJson(fromJson(steps.toggle_feature_flag.outputs.response).customProperties) }},
"archived": ${{ fromJson(steps.toggle_feature_flag.outputs.response).archived }},
"projectKey": "${{ github.event.inputs.project_key }}"
}
relations: "${{ toJson(fromJson(inputs.port_context).relations) }}"
clientId: ${{ secrets.PORT_CLIENT_ID }}
clientSecret: ${{ secrets.PORT_CLIENT_SECRET }}
baseUrl: https://api.getport.io
operation: UPSERT
runId: ${{fromJson(inputs.port_context).run_id}}
- name: Log After Toggling
uses: port-labs/port-github-action@v1
with:
clientId: ${{ secrets.PORT_CLIENT_ID }}
clientSecret: ${{ secrets.PORT_CLIENT_SECRET }}
baseUrl: https://api.getport.io
operation: PATCH_RUN
runId: ${{fromJson(inputs.port_context).run_id}}
logMessage: "Feature flag '${{fromJson(inputs.port_context).entity}}' in '${{ github.event.inputs.environment_key }}' environment set to ${{ github.event.inputs.flag_state }}."
Set up self-service action
We will create a self-service action to handle toggling LaunchDarkly feature flags. To create a self-service action follow these steps:
-
Head to the self-service page.
-
Click on the
+ New Action
button. -
Click on the
{...} Edit JSON
button. -
Copy and paste the following JSON configuration into the editor.
Toggle LaunchDarkly Feature Flag (Click to expand)
Modification RequiredMake sure to replace
<GITHUB_ORG>
and<GITHUB_REPO>
with your GitHub organization and repository names respectively.{
"identifier": "launchDarklyFeatureFlag_toggle_a_feature_flag",
"title": "Toggle LaunchDarkly Feature Flag",
"icon": "Launchdarkly",
"description": "Toggle a Feature Flag in launchdarkly",
"trigger": {
"type": "self-service",
"operation": "DAY-2",
"userInputs": {
"properties": {
"project_key": {
"description": "LaunchDarkly Project Key",
"title": "project_key",
"icon": "Launchdarkly",
"type": "string"
},
"flag_key": {
"description": "LaunchDarkly Flag Key (without project suffix)",
"title": "Flag Key",
"icon": "Launchdarkly",
"type": "string"
},
"environment_key": {
"description": "LaunchDarkly Environment Key where the flag exists",
"title": "environment_key",
"icon": "Launchdarkly",
"type": "string"
},
"flag_state": {
"title": "flag_state",
"description": "Desired state of the feature flag (true for enabled, false for disabled)",
"icon": "Launchdarkly",
"type": "boolean",
"default": true
}
},
"required": [
"project_key",
"flag_key",
"environment_key"
],
"order": [
"project_key",
"flag_key",
"environment_key",
"flag_state"
]
},
"blueprintIdentifier": "launchDarklyFeatureFlag"
},
"invocationMethod": {
"type": "GITHUB",
"org": "<GITHUB_ORG>",
"repo": "<GITHUB_REPO>",
"workflow": "toggle-feature-flag.yaml",
"workflowInputs": {
"project_key": "{{.inputs.\"project_key\"}}",
"flag_key": "{{.inputs.\"flag_key\"}}",
"environment_key": "{{.inputs.\"environment_key\"}}",
"flag_state": "{{.inputs.\"flag_state\"}}",
"port_context": {
"blueprint": "{{.action.blueprint}}",
"entity": "{{.entity.identifier}}",
"run_id": "{{.run.id}}",
"relations": "{{.entity.relations}}"
}
},
"reportWorkflowStatus": true
},
"requiredApproval": false
} -
Click
Save
.
Now you should see the Toggle LaunchDarkly Feature Flag
action in the self-service page. ๐
Let's test it!โ
-
Head to the self-service page of your portal
-
Choose either the GitHub workflow or webhook implementation:
- For GitHub workflow: Click on
Toggle LaunchDarkly Feature Flag
- For webhook: Click on
Toggle Feature Flag (Webhook)
- For GitHub workflow: Click on
-
Select the LaunchDarkly feature flag you want to toggle
-
Enter the required information:
- Project Key: The LaunchDarkly project key where the flag is located
- Flag Key: The LaunchDarkly flag key (without project suffix)
- Environment Key: The environment where you want to toggle the flag
- Flag State: Set to true to enable the flag or false to disable it
-
Click on
Execute
-
Done! Wait for the feature flag's status to be changed in LaunchDarkly