dev-resources.site
for different kinds of informations.
Copy Config Vars from One Heroku App to Another
Published at
9/17/2024
Categories
heroku
ruby
Author
h3h
Author
3 person written this
h3h
open
Below is a quick little script I threw together to copy config vars from one Heroku app to another, e.g. when trying to create a staging or test app that [largely] mirrors another.
It relies on a Heroku app.json to specify which config vars are required, combined with a dump of your other appβs config vars via heroku config --shell -a <app-name> >foo.env
to get the <env-file>
input.
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'json'
# Usage: env-setter <app.json> <env-file> [-a <heroku-app-name>]
app_json_file = ARGV[0]
env_file = ARGV[1]
env_var_keys = []
heroku_app_name = nil
# parse command line -a switch to get Heroku app name
if ARGV.include?('-a')
heroku_app_name = ARGV[ARGV.index('-a') + 1]
end
# Pull required config vars from app.json
begin
parsed_json = JSON.parse(File.read(app_json_file))
env_var_keys = parsed_json['env'].map {|k,v| k if v.is_a?(Hash) && v['required'] }.compact
rescue JSON::ParserError, EOFError
puts "Error parsing JSON file: #{app_json_file}"
exit 1
end
# puts "Required config vars from app.json: #{env_var_keys.join(', ')}"
# Pull env vars from env file
begin
env_vars = Hash[*File.readlines(env_file).map(&:chomp).map { |line| line.split('=', 2) }.flatten]
rescue EOFError
puts "Error reading env file: #{env_file}"
exit 1
end
# puts "Loaded env var names from #{env_file}: #{env_vars.keys.join(', ')}"
# Check if any required env vars are missing
missing_vars = env_var_keys - env_vars.keys
if missing_vars.any?
puts "Missing required env vars from #{env_file}: #{missing_vars.join(', ')}"
exit 1
end
# Set config vars on Heroku if app name provided
if heroku_app_name
puts "Setting config vars on Heroku app #{heroku_app_name}..."
system('heroku', 'config:set', '--app', heroku_app_name, *env_vars.map {|k,v| "#{k}=#{v}" }.flatten)
end
heroku Article's
30 articles in total
When (Tech Service) Relationships Donβt Work Out
read article
Ferrum Doesnβt Work on Heroku?
read article
Handbook to migrate your Postgres from Heroku to Kamal
read article
Deploy your Preprod and Production Rails Application using Kamal
read article
How To Deploy Django On Heroku Using Automatic Deployment from GitHub
read article
Sherlock Holmes: The Case Of App Not Found
read article
Own Heroku Review Apps with GitHub Actions and Kamal 2
read article
Copy Config Vars from One Heroku App to Another
currently reading
Why Havenβt You Upgraded to HTTP/2?
read article
Leveling Up My GraphQL Skills: Real Time Subscriptions
read article
Self-hosted alternative to Heroku - Ptah.sh
read article
Bokeh an interesting data tool in python for data visualization
read article
How to implement Coolify, the self-hosted alternative to Heroku
read article
redis ACTION REQUIRED: Version approaching end of life
read article
Import the database from the Heroku dump
read article
Create and Connect to an Azure SQL Database on Heroku: A Step-By-Step Guide
read article
Buh-Bye Webpack and Node.js, Hello Rails and Import Maps
read article
How to Set Up Strapi and Deploy on Heroku
read article
Career Change: Corporate Ladder to Software Developer π
read article
π Corporate Ladder to Software Developer π
read article
Exploring Key Features of Heroku and AWS: A Comparative Analysis
read article
how to deploy backend
read article
Installing Playwright on Heroku for Programmatic Node.js Browser Automation
read article
5 Simple Steps to Get Your Test Suite Running in Heroku CI
read article
When You Need More Power Than a Lambda Provides
read article
Deploying NestJS Apps to Heroku: A Comprehensive Guide
read article
Heroku for ChatOps: Start and Monitor Deployments from Slack
read article
How to Setup a Project That Can Host Up to 1000 Users for Free
read article
How to Heroku: Launch Your First App Webinar
read article
Working with Heroku Logplex for Comprehensive Application Logging
read article
Featured ones: