Skip to content

Instantly share code, notes, and snippets.

@nabucosound
Created December 30, 2013 12:40
Show Gist options
  • Star 58 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • 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')
@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