Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@ctartist621 ctartist621 commented Feb 5, 2016

Thanks! You just saved me a lot of time!

@KidA001

This comment has been minimized.

Copy link

@KidA001 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

This comment has been minimized.

Copy link

@derekcroft derekcroft commented Feb 19, 2016

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

This comment has been minimized.

Copy link

@robert-osborne robert-osborne commented Jun 2, 2016

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

This comment has been minimized.

Copy link

@morgs32 morgs32 commented Sep 20, 2016

works for me

@ktec

This comment has been minimized.

Copy link

@ktec ktec commented Nov 25, 2016

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

@jamesr2323

This comment has been minimized.

Copy link

@jamesr2323 jamesr2323 commented May 24, 2017

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

This comment has been minimized.

Copy link

@razdvapoka razdvapoka commented Jun 6, 2017

Thank you!

@bezelga

This comment has been minimized.

Copy link

@bezelga bezelga commented Aug 30, 2017

awesomeee

@prakhar-goel

This comment has been minimized.

Copy link

@prakhar-goel prakhar-goel commented Sep 11, 2017

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

This comment has been minimized.

Copy link

@guy-roberts guy-roberts commented Jan 5, 2018

Thank you

@mibaraho

This comment has been minimized.

Copy link

@mibaraho mibaraho commented Jan 19, 2018

Awesome, thanks pal

@volkanbicer

This comment has been minimized.

Copy link

@volkanbicer volkanbicer commented Mar 21, 2018

Thank you

@msantam2

This comment has been minimized.

Copy link

@msantam2 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

This comment has been minimized.

Copy link

@PhilT 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

This comment has been minimized.

Copy link

@timdiggins timdiggins commented May 22, 2018

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

This comment has been minimized.

Copy link

@PaddyT55 PaddyT55 commented Jul 25, 2018

Thanks Tim. That was ideal for my purposes

@oscarmorrison

This comment has been minimized.

Copy link

@oscarmorrison oscarmorrison commented Sep 20, 2018

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

@srivastavayashdeep-zz

This comment has been minimized.

Copy link

@srivastavayashdeep-zz srivastavayashdeep-zz commented Jan 14, 2019

Very Nice , It is very useful .

@md-farhan-memon

This comment has been minimized.

Copy link

@md-farhan-memon md-farhan-memon commented Oct 18, 2019

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

@AbbalYouness

This comment has been minimized.

Copy link

@AbbalYouness AbbalYouness 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

This comment has been minimized.

Copy link

@julienma 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

This comment has been minimized.

Copy link

@dirkkelly dirkkelly commented Oct 4, 2021

Thanks for the one liner @julienma!

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