This ruby script will be useful when you need to move a lots of ENV variables between two heroku apps.
You must be logged in heroku CLI, to successfully run this script.
In DEFAULT_ENV constant in script you can set ENVs which you don’t want to copy.
Usage:
ruby heroku_env.rb
'Enter app name with your env variables, e.g: my-blog-api, twitter-clone-pr-23'
my-blog-app
'Enter app name where you want to copy env variables, e.g: my-new-blog, my-new-twitter-clone-25'
my-new-blog
Setting TWITTER_API_KEY, INSTAGRAM_API_KEY and restarting ⬢ my-new-blog... !
classHerokuEnvdefself.call(from_app,to_app)new(from_app,to_app).send(:copy)endprivate# This ENVs will not be copied to destination appDEFAULT_ENV=%w(DATABASE_URL STAGING_DATABASE_URL).freeze# @from_app = 'my-blog-pr-25'# @to_app = 'my-blog-pr-26'attr_reader:from_app,:to_appdefinitialize(from_app,to_app)@from_app=from_app@to_app=to_appenddefcopyenvs=fetch_envs_from_appenvs=envs.split("\n").drop(1)text=format_variable(envs).join(' ')add_envs_to_heroku(text)enddefadd_envs_to_heroku(text)heroku_url=format_host_name(to_app)command=heroku_url.gsub('config',"config:set #{text}")system(command)enddeffetch_envs_from_appcommand=format_host_name(from_app)system(command)`#{command}`enddefformat_host_name(app)"heroku config --app #{app}"enddefformat_variable(envs)vars=[]envs.each_slice(1)do|env|str=env.join.delete(' , []').split(':').join('=')vars<<strendremove_defaults(vars)enddefremove_defaults(envs)envs.reject{|s|DEFAULT_ENV.include?(s.split('=')[0])}endendputs'Enter app name with your env variables, e.g: my-blog-api, twitter-clone-pr-23'app_name=gets.chompputs'Enter app name where you want to copy env variables, e.g: my-new-blog, my-new-twitter-clone-25'app_name2=gets.chompHerokuEnv.call(app_name,app_name2)