Toggle LaunchDarkly Feature Flag
Overview
This GitHub action allows you to quickly toggle LaunchDarkly Feature Flags via Port Actions with ease.
Prerequisites
-
Install the Ports GitHub app from here.
-
Create the following GitHub action secrets
LAUNCHDARKLY_ACCESS_TOKEN
- a token with permission to toggle a feature flag in LaunchDarkly Learn morePORT_CLIENT_ID
- Port Client ID Learn morePORT_CLIENT_SECRET
- Port Client Secret. 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.
-
In Case you decided not to install the LaunchDarkly integration, you will need to create a blueprint for LaunchDarkly Feature Flag in Port.
LaunchDarkly Feature Flag Blueprint
{
"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
}
}
}
GitHub Workflow
- Create a workflow file under
.github/workflows/toggle-feature-flag.yaml
with the following content:
We recommend creating a dedicated repository for the workflows that are used by Port actions.
GitHub Workflow
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 }}."
Port Configuration
Create a new self service action using the following JSON configuration.
Toggle A Feature Flag (Click to expand)
Make 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"
},
"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",
"environment_key"
],
"order": [
"project_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\"}}",
"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,
"publish": true
}
Now you should see the Toggle LaunchDarkly Feature Flag
action in the self-service page. 🎉
Let's test it!
- Head to the Self Service hub.
- Click on the
Toggle LaunchDarkly Feature Flag
action. - Choose the feature flag you want to toggle (In case you didn't install the LaunchDarkly integration, it means you don't have any feature flags in Port yet, so you will need to create one manually in Port to test this action).
- Enter the associated
projectKey
andenvironmentKey
for the flag and toggleflagState
(ON by default). - Click on
Execute
. - Done! wait for the feature flag's status to be changed in LaunchDarkly.
Congrats 🎉 You've toggled your first LaunchDarkly feature flag from Port!