-
-
Save nabucosound/8181671 to your computer and use it in GitHub Desktop.
#!/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') |
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
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.
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".
works for me
Here's a slightly safer option that generates the command for you.
You can set multiple ENV with one command which is a lot quicker if you have many settings: https://gist.github.com/jamesr2323/b9243d433daf8fd98c8b88a4bbd77ea7
Thank you!
awesomeee
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')
Thank you
Awesome, thanks pal
Thank you
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"
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
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
Thanks Tim. That was ideal for my purposes
If you have them already in a .env file you can just do this
https://gist.github.com/oscarmorrison/be81140ca8d6afdf9e58667df9849a50
Very Nice , It is very useful .
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
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}`);
}
);
}
});
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
Thanks for the one liner @julienma!
Thanks! You just saved me a lot of time!