Prerequisites
Before you begin integrating LaunchDarkly feature flags with Bitbucket Pipelines, ensure you have the following:
1. LaunchDarkly Account
You need access to a LaunchDarkly project where you can create and manage launch darkly feature flags. This includes environments like Production, Staging, or Development.
2. Bitbucket Repository
Your application code must be hosted in Bitbucket. Pipelines should be enabled in your repository settings.
3. API Access Tokens
Generate a secure LaunchDarkly API access token with the correct permissions for reading, updating, or toggling flags. Store this token as a secured repository variable in Bitbucket Pipelines.
4. YAML Pipeline Configuration
Bitbucket Pipelines require a valid bitbucket-pipelines.yml file. You will embed feature flag logic or API calls directly inside it.
5. Basic Understanding of What a Feature Flag Is
A quick refresher:
A feature flag (or toggle) is a switch that controls whether specific code paths are enabled or disabled without redeploying your application.
You can use them for:
- Gradual rollouts
- Canary releases
- A/B testing
- Safe deployments
- Faster experimentation
An ImageLabeller Feature Flag Demo
To understand how feature flags LaunchDarkly work in a real pipeline, consider an example application called ImageLabeller, where a new AI-based labeling feature is being added.
Step-by-Step Overview
1. Create a Feature Flag in LaunchDarkly
Name it something like:
ai-image-labeller
Set targeting rules for environments such as:
- ON for staging
- OFF for production
- On for specific test users
2. Use the LaunchDarkly SDK in Your Code
Example (Python):
if ld_client.variation(“ai-image-labeller”, user, False):
run_ai_labelling()
else:
run_basic_labelling()
This ensures both old and new paths exist without deployment risk.
3. Expose the Flag in CI/CD
Inside Bitbucket Pipelines, API calls can toggle this flag automatically depending on pipeline stages:
- Enable the flag in staging
- Gradually rollout after tests
- Turn it off automatically if tests fail
This allows safe experimentation while maintaining stability.
How to Use Launch Darkly Feature Flags with Bitbucket Pipelines
Integrating launch darkly feature flags with Bitbucket Pipelines unlocks a controlled, automated release workflow.
Here’s how:
- Store LaunchDarkly Tokens in Bitbucket
In your repository settings:
Repository → Pipelines → Repository Variables
Add:
- LD_ACCESS_TOKEN
- LD_PROJECT_ID
- LD_ENVIRONMENT_ID
Set them to secure (locked).
- Call LaunchDarkly API in Pipeline Steps
Create or edit your bitbucket-pipelines.yml file:
pipelines:
branches:
main:
– step:
name: Enable Feature Flag in Staging
script:
– curl -X PATCH \
“https://app.launchdarkly.com/api/v2/flags/$LD_PROJECT_ID/ai-image-labeller” \
-H “Authorization: $LD_ACCESS_TOKEN” \
-H “Content-Type: application/json” \
-d ‘[
{
“op”: “replace”,
“path”: “/environments/$LD_ENVIRONMENT_ID/on”,
“value”: true
}
]’
This script automatically turns on the flag when a commit is pushed to the main branch.
- Add Conditional Rollouts
You can extend your pipeline:
- Turn OFF the feature if integration tests fail
- Do percentage rollouts
- Enable only for internal testers
- Gate deployments using feature flag states
Example: Turning the flag OFF if tests fail:
– step:
name: Disable Feature on Failure
deployment: production
script:
– if [ $? -ne 0 ]; then
curl -X PATCH \
“https://app.launchdarkly.com/api/v2/flags/$LD_PROJECT_ID/ai-image-labeller” \
-H “Authorization: $LD_ACCESS_TOKEN” \
-H “Content-Type: application/json” \
-d ‘[
{
“op”: “replace”,
“path”: “/environments/$LD_ENVIRONMENT_ID/on”,
“value”: false
}
]’;
fi
- Real-World Applications
✔ Safe Rollouts
Deploy code with new features OFF, then toggle ON later.
✔ A/B Testing
Turn on features for a subset of users using rules or percentages.
✔ Faster Development
Ship incomplete code behind flags, releasing only when ready.
✔ Zero-Downtime Deployments
Rollout new versions gradually with instant rollback via flags.
Conclusion
Using Launch Darkly feature flags with Bitbucket Pipelines creates a powerful, automated, and safe deployment workflow. You gain control over how features roll out, improve testing strategies, reduce deployment risks, and increase development velocity.
Whether you’re adding a new AI feature, running experiments, or releasing a major update, this integration helps teams ship confidently and respond instantly to issues—all without redeploying.
FAQs
1. What is a feature flag?
A feature flag is a switch in your code that lets you enable or disable functionality without redeploying your application.
2. Why use LaunchDarkly feature flags?
LaunchDarkly provides advanced targeting, gradual rollouts, and real-time toggling across environments, making releases safer and more controlled.
3. Can Bitbucket Pipelines toggle feature flags automatically?
Yes. Using API calls in your pipeline steps, you can enable, disable, or adjust LaunchDarkly flags automatically based on deployments or test results.
4. What is a feature flag example?
A feature flag could turn ON a new UI layout only for beta testers while keeping the old layout active for all other users.
