Skip to content

Instantly share code, notes, and snippets.

@nabucosound
Created December 30, 2013 12:40
Show Gist options
  • Save nabucosound/8181671 to your computer and use it in GitHub Desktop.
Save nabucosound/8181671 to your computer and use it in GitHub Desktop.
Script to copy environment variables from an existing heroku app to another one
#!/bin/bash
# Source: http://blog.nonuby.com/blog/2012/07/05/copying-env-vars-from-one-heroku-app-to-another/
set -e
sourceApp="$1"
targetApp="$2"
while read key value; do
key=${key%%:}
echo "Setting $key=$value"
heroku config:set "$key=$value" --app "$targetApp"
done < <(heroku config --app "$sourceApp" | sed -e '1d')
@ctartist621
Copy link

Thanks! You just saved me a lot of time!

@KidA001
Copy link

KidA001 commented Feb 15, 2016

FWIW I was not able to run this with sh heroku_env_copy.sh - I had to use bash heroku_env_copy.sh as processes substitution (that last line on 14) is only allowed in bash

@derekcroft
Copy link

This was a huge time saver. Thank you!

One caveat -- this script changes DATABASE_URL, so be sure you really want to change that variable before you run this.

@robert-osborne
Copy link

This script no longer works as the first config:set restarts the app and the second config:set fails with "Couldn't find that app".

@morgs32
Copy link

morgs32 commented Sep 20, 2016

works for me

@ktec
Copy link

ktec commented Nov 25, 2016

Here's a slightly safer option that generates the command for you.

@jamesr2323
Copy link

You can set multiple ENV with one command which is a lot quicker if you have many settings: https://gist.github.com/jamesr2323/b9243d433daf8fd98c8b88a4bbd77ea7

@razdvapoka
Copy link

Thank you!

@bezelga
Copy link

bezelga commented Aug 30, 2017

awesomeee

@prakhar-goel
Copy link

As @derekcroft rightly mentioned that some keys like DATABASE_URL should NOT be replaced, here is a modified version that allows you to ignore certain keys as provided in the defaultKeys array. The keys should not contain any spaces.

#!/bin/bash

# Source: http://blog.nonuby.com/blog/2012/07/05/copying-env-vars-from-one-heroku-app-to-another/

set -e

sourceApp="$1"
targetApp="$2"
defaultKeys=(DATABASE_URL PAPERTRAIL_API_TOKEN ROLLBAR_ACCESS_TOKEN ROLLBAR_ENDPOINT)
while read key value; do
  key=${key%%:}
  if [[ ${defaultKeys[*]} =~ $key ]];
    then
      echo "Ignoring $key=$value"
    else
      echo "Setting $key=$value"
      heroku config:set "$key=$value" --app "$targetApp"
  fi
done  < <(heroku config --app "$sourceApp" | sed -e '1d')

@guy-roberts
Copy link

Thank you

@mibaraho
Copy link

Awesome, thanks pal

@volkanbicer
Copy link

Thank you

@msantam2
Copy link

msantam2 commented Apr 16, 2018

With credit going to several previous gists seen above, I consolidated some of these approaches to fit my personal needs and thought I would share. Script below allows you to transfer multiple Config Vars at once, while also allowing you to ignore particular Config Vars from the source application:


# The script below allows you to easily transfer all Heroku Config Vars (i.e. 
# ENV vars) from an existing Heroku project to a new Heroku project from the
# command line. If you need to ignore one or more keys from the existing Heroku
# project, you may. Please see instructions below.

# Instructions
# 1. Have Heroku CLI installed on machine
#    (https://devcenter.heroku.com/articles/heroku-cli#download-and-install)
# 2. Run "heroku login" to login from command line
# 3. Run "heroku apps" to quickly see the names of your apps
# 4. Add this script file to the Desktop, "cd" into Desktop,
#    run "bash ./config_vars.sh matts-source-app matts-target-app"
# 5. Enjoy new Config Vars!
# (Note: if same key exists in new Heroku project, it will be overwritten)

# After this is done, you can add more NEW config vars to new Heroku project
# with the following command:
# "heroku config:set LOGGER_LEVEL=debug ENV=production CRYPTO_SECRET=guesswhat"


set -e

sourceApp="$1"
targetApp="$2"
# ignoredKeys=(IGNORE_THIS_KEY ALSO_IGNORE_THIS_KEY ADD_IGNORED_KEYS_HERE)
config=""

while read key value; do
  key=${key%%:}

  if [[ ${ignoredKeys[*]} =~ $key ]];
    then
      echo "Ignoring $key=$value"
    else
      config="$config $key=$value"
  fi
done < <(heroku config --app "$sourceApp" | sed -e '1d')

eval "heroku config:set $config --app $targetApp"

@PhilT
Copy link

PhilT commented May 16, 2018

For those not using bash and have Ruby:

#!/usr/bin/env ruby

require 'json'

source_app = ARGV[0]
dest_app = ARGV[1]
ignore = %w(DATABASE_URL)

vars = JSON.parse `heroku config --app #{source_app} --json`
vars.each do |key, value|
  next if ignore.include?(key)
  `heroku config:set #{key}="#{value}" --app #{dest_app}`
end

@timdiggins
Copy link

FYI much quicker to set them all in one heroku config:set a=b c=d etc

If you prefer a manual review and you do have bash/zsh:

heroku config -s -a source-heroku-app > config.txt

Now review config.txt and remove any unwanted config lines

cat config.txt | tr '\n' ' ' | xargs heroku config:set -a target-heroku-app

@PaddyT55
Copy link

Thanks Tim. That was ideal for my purposes

@oscarmorrison
Copy link

If you have them already in a .env file you can just do this
https://gist.github.com/oscarmorrison/be81140ca8d6afdf9e58667df9849a50

@srivastavayashdeep-zz
Copy link

Very Nice , It is very useful .

@md-farhan-memon
Copy link

An interactive script to add multiple config variables reading from a file to the heroku server in one line dynamic options command. https://gist.github.com/md-farhan-memon/e90e30cc0d67d0a0cd7779d6adfe62d1

@yabbal
Copy link

yabbal commented Jun 10, 2020

For those not using bash and have NodeJs:

const { exec } = require("child_process");

const args = process.argv.slice(2);

const sourceApp = args[0];
const targetApp = args[1];
const ignoredKeys = ["DATABASE_URL", "PAPERTRAIL_API_TOKEN"];

exec(`heroku config --app ${sourceApp} --json`, (err, stdout, stderr) => {
  if (err) return console.error(`Error: ${stderr}`);

  const configVars = JSON.parse(stdout);
  ignoredKeys.map((keys) => delete configVars[keys]);

  for (const key in configVars) {
    const value = configVars[key];
    exec(
      `heroku config:set ${key}=${value} --app ${targetApp}`,
      (err, stdout, stderr) => {
        if (err) return console.error(`Error: ${stderr}`);

        console.log(`${key} = ${value}`);
      }
    );
  }
});

@julienma
Copy link

julienma commented Jul 9, 2021

1-liner to copy all variables but the ones starting with DATABASE_URL or HEROKU_ (which are dynamically set by heroku):

heroku config -s -a source-heroku-app | grep -vE '^DATABASE_URL|^HEROKU_' | xargs heroku config:set --app target-heroku-app

@dirkkelly
Copy link

Thanks for the one liner @julienma!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment