Skip to content

Node script to synchronize LaunchDarkly flags from one environment to another

License

Notifications You must be signed in to change notification settings

launchdarkly-labs/sync-ld-flags

Repository files navigation

This script is not officially supported by LaunchDarkly.

To sync individual flags, use the in-application compare and copy functionality instead.

LaunchDarkly Environment Synchronizer

This Node script simplifies the task of synchronizing flag rollout rules between two different environments. It assumes that both environments are in the same project and account, and takes as input the API keys for the two environments you're syncing.

The following properties of each flag are synchronized:

Quick setup

  1. Install with npm

    npm install
    
  2. Run the script, passing in the project key, source and destination environments, and API token:

    ./sync-ld-flags --api-token API_TOKEN \
      --project-key PROJECT_KEY \
      --source-env SOURCE_ENVIRONMENT_KEY \
      --destination-env DEST_ENVIRONMENT_KEY
    

    You can find your environment keys in Account settings within the LaunchDarkly application. Click the Projects tab and select your project, then review the list of environments and keys for that project.

    You can create an API access token from the Account settings page, on the Authorization tab.

    Optionally pass a host override:

    ./sync-ld-flags --api-token API_TOKEN \
      -H https://your-launchdarkly-deploy.com
      -p PROJECT_KEY \
      -s SOURCE_ENVIRONMENT_KEY \
      -d DEST_ENVIRONMENT_KEY
    

    Use -D to enable HTTP debugging if needed.

All options

Usage:

./sync-ld-flags [options]

Options:

  -p, --project-key <key>      Project key
  -s, --source-env <key>       Source environment key
  -d, --destination-env <key>  Destination environment key
  -t, --api-token <token>      LaunchDarkly personal access token with
                               write-level access
  -f, --flag <flag>            Sync only the flag with the specified flag key
  -T, --tag <tags...>          Sync flags with the given tag(s). Only flags
                               with all tags will sync.
  -o, --omit-segments          Omit segments when syncing (default: false)
  -H, --host <host>            Hostname override (default:
                               "https://app.launchdarkly.com")
  -v, --verbose                Enable verbose logging (default: false)
  --dry-run                    Preview changes (default: false)
  -D, --debug                  Enable HTTP debugging (default: false)
  -h, --help                   Display help for command

Other

This project supports Fig autocomplete!

Add --api-token and (optionally) --host at the beginning of your commands and Fig provides suggestions for your project and environment keys.

Want to set these configuration variables more easily?

Autocomplete configuration.

You can autocomplete keys and servers by using a config file. Add the following configuration to ~/.config/ldc.json:

{
    "<NAME>": {
        "apitoken": "<your api token>",
        "server": "<[OPTIONAL] default is https://app.launchdarkly.com>"
    }
}

Example:

{
    "staging": {
        "apitoken": "api-foobarfoobarfoobarfoobar",
        "server": "https://your-launchdarkly-deploy.com"
    }
}