$npx -y skills add BagelHole/DevOps-Security-Agent-Skills --skill feature-flagsImplement feature flags for progressive feature rollout using LaunchDarkly, Unleash, or custom solutions. Control feature visibility, perform A/B testing, and enable trunk-based development. Use when implementing gradual rollouts, feature toggles, or experimentation platforms.
| 1 | # Feature Flags |
| 2 | |
| 3 | Control feature releases and enable progressive rollout with feature flag systems. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | Use this skill when: |
| 8 | - Implementing gradual feature rollouts |
| 9 | - Enabling trunk-based development |
| 10 | - Running A/B tests and experiments |
| 11 | - Managing feature lifecycles |
| 12 | - Implementing kill switches for production |
| 13 | |
| 14 | ## Prerequisites |
| 15 | |
| 16 | - Application code access |
| 17 | - Feature flag service or self-hosted solution |
| 18 | - Basic understanding of deployment patterns |
| 19 | |
| 20 | ## Feature Flag Types |
| 21 | |
| 22 | | Type | Purpose | Example | |
| 23 | |------|---------|---------| |
| 24 | | Release | Control feature visibility | New checkout flow | |
| 25 | | Experiment | A/B testing | Button color test | |
| 26 | | Ops | Runtime configuration | Rate limiting | |
| 27 | | Permission | User access control | Premium features | |
| 28 | | Kill Switch | Emergency disable | Third-party integration | |
| 29 | |
| 30 | ## LaunchDarkly |
| 31 | |
| 32 | ### SDK Setup (Node.js) |
| 33 | |
| 34 | ```javascript |
| 35 | const LaunchDarkly = require('launchdarkly-node-server-sdk'); |
| 36 | |
| 37 | const client = LaunchDarkly.init(process.env.LAUNCHDARKLY_SDK_KEY); |
| 38 | |
| 39 | await client.waitForInitialization(); |
| 40 | |
| 41 | // Evaluate flag |
| 42 | const user = { |
| 43 | key: 'user-123', |
| 44 | email: 'user@example.com', |
| 45 | custom: { |
| 46 | plan: 'premium', |
| 47 | company: 'acme' |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | const showNewFeature = await client.variation('new-checkout', user, false); |
| 52 | |
| 53 | if (showNewFeature) { |
| 54 | // New feature code |
| 55 | } else { |
| 56 | // Existing code |
| 57 | } |
| 58 | ``` |
| 59 | |
| 60 | ### React SDK |
| 61 | |
| 62 | ```javascript |
| 63 | import { withLDProvider, useFlags, useLDClient } from 'launchdarkly-react-client-sdk'; |
| 64 | |
| 65 | // Provider setup |
| 66 | export default withLDProvider({ |
| 67 | clientSideID: 'your-client-side-id', |
| 68 | user: { |
| 69 | key: 'user-123', |
| 70 | email: 'user@example.com' |
| 71 | } |
| 72 | })(App); |
| 73 | |
| 74 | // Using flags in component |
| 75 | function FeatureComponent() { |
| 76 | const { newCheckout, experimentVariant } = useFlags(); |
| 77 | const ldClient = useLDClient(); |
| 78 | |
| 79 | // Track events |
| 80 | const handleClick = () => { |
| 81 | ldClient.track('checkout-started'); |
| 82 | }; |
| 83 | |
| 84 | if (newCheckout) { |
| 85 | return <NewCheckout onClick={handleClick} />; |
| 86 | } |
| 87 | return <OldCheckout onClick={handleClick} />; |
| 88 | } |
| 89 | ``` |
| 90 | |
| 91 | ### Targeting Rules |
| 92 | |
| 93 | ```yaml |
| 94 | # LaunchDarkly targeting configuration |
| 95 | flag: new-checkout |
| 96 | targeting: |
| 97 | # Individual users |
| 98 | targets: |
| 99 | - variation: true |
| 100 | values: ['user-123', 'user-456'] |
| 101 | |
| 102 | # Rules |
| 103 | rules: |
| 104 | # Beta users |
| 105 | - variation: true |
| 106 | clauses: |
| 107 | - attribute: email |
| 108 | op: endsWith |
| 109 | values: ['@company.com'] |
| 110 | |
| 111 | # Premium plan |
| 112 | - variation: true |
| 113 | clauses: |
| 114 | - attribute: plan |
| 115 | op: in |
| 116 | values: ['premium', 'enterprise'] |
| 117 | |
| 118 | # Percentage rollout |
| 119 | - variation: true |
| 120 | rollout: |
| 121 | variations: |
| 122 | - variation: true |
| 123 | weight: 20000 # 20% |
| 124 | - variation: false |
| 125 | weight: 80000 # 80% |
| 126 | |
| 127 | # Default |
| 128 | fallthrough: |
| 129 | variation: false |
| 130 | ``` |
| 131 | |
| 132 | ## Unleash |
| 133 | |
| 134 | ### Server Setup |
| 135 | |
| 136 | ```yaml |
| 137 | # docker-compose.yml |
| 138 | version: '3.8' |
| 139 | |
| 140 | services: |
| 141 | unleash: |
| 142 | image: unleashorg/unleash-server:latest |
| 143 | ports: |
| 144 | - "4242:4242" |
| 145 | environment: |
| 146 | - DATABASE_URL=postgres://postgres:password@db/unleash |
| 147 | - DATABASE_SSL=false |
| 148 | depends_on: |
| 149 | - db |
| 150 | |
| 151 | db: |
| 152 | image: postgres:15 |
| 153 | environment: |
| 154 | - POSTGRES_PASSWORD=password |
| 155 | - POSTGRES_DB=unleash |
| 156 | volumes: |
| 157 | - postgres-data:/var/lib/postgresql/data |
| 158 | |
| 159 | volumes: |
| 160 | postgres-data: |
| 161 | ``` |
| 162 | |
| 163 | ### SDK Setup (Node.js) |
| 164 | |
| 165 | ```javascript |
| 166 | const { initialize } = require('unleash-client'); |
| 167 | |
| 168 | const unleash = initialize({ |
| 169 | url: 'http://localhost:4242/api', |
| 170 | appName: 'my-app', |
| 171 | customHeaders: { |
| 172 | Authorization: 'your-api-token' |
| 173 | } |
| 174 | }); |
| 175 | |
| 176 | unleash.on('ready', () => { |
| 177 | // Check feature |
| 178 | const isEnabled = unleash.isEnabled('new-checkout'); |
| 179 | |
| 180 | // With context |
| 181 | const context = { |
| 182 | userId: 'user-123', |
| 183 | properties: { |
| 184 | plan: 'premium' |
| 185 | } |
| 186 | }; |
| 187 | |
| 188 | const isEnabledForUser = unleash.isEnabled('new-checkout', context); |
| 189 | |
| 190 | // Get variant |
| 191 | const variant = unleash.getVariant('experiment-flag', context); |
| 192 | console.log(variant.name); // 'control' or 'treatment' |
| 193 | }); |
| 194 | ``` |
| 195 | |
| 196 | ### Activation Strategies |
| 197 | |
| 198 | ```yaml |
| 199 | # Standard strategies |
| 200 | strategies: |
| 201 | - name: default |
| 202 | # On/off for everyone |
| 203 | |
| 204 | - name: userWithId |
| 205 | parameters: |
| 206 | userIds: 'user-1,user-2,user-3' |
| 207 | |
| 208 | - name: gradualRolloutUserId |
| 209 | parameters: |
| 210 | percentage: 25 |
| 211 | groupId: 'new-feature' |
| 212 | |
| 213 | - name: gradualRolloutRandom |
| 214 | parameters: |
| 215 | percentage: 50 |
| 216 | |
| 217 | - name: flexibleRollout |
| 218 | parameters: |
| 219 | rollout: 30 |
| 220 | stickiness: userId |
| 221 | groupId: 'checkout-exp' |
| 222 | ``` |
| 223 | |
| 224 | ## Custom Implementation |
| 225 | |
| 226 | ### Database-Backed Flags |
| 227 | |
| 228 | ```python |
| 229 | # models.py |
| 230 | from django.db import models |
| 231 | |
| 232 | class FeatureFlag(models.Model): |
| 233 | name = models.Cha |