Logo

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
Categories
2 categories in total
heroku
open
ruby
open
Author
3 person written this
h3h
open
Copy Config Vars from One Heroku App to Another

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
Enter fullscreen mode Exit fullscreen mode
heroku Article's
30 articles in total
Favicon
When (Tech Service) Relationships Don’t Work Out
Favicon
Ferrum Doesn’t Work on Heroku?
Favicon
Handbook to migrate your Postgres from Heroku to Kamal
Favicon
Deploy your Preprod and Production Rails Application using Kamal
Favicon
How To Deploy Django On Heroku Using Automatic Deployment from GitHub
Favicon
Sherlock Holmes: The Case Of App Not Found
Favicon
Own Heroku Review Apps with GitHub Actions and Kamal 2
Favicon
Copy Config Vars from One Heroku App to Another
Favicon
Why Haven’t You Upgraded to HTTP/2?
Favicon
Leveling Up My GraphQL Skills: Real Time Subscriptions
Favicon
Self-hosted alternative to Heroku - Ptah.sh
Favicon
Bokeh an interesting data tool in python for data visualization
Favicon
How to implement Coolify, the self-hosted alternative to Heroku
Favicon
redis ACTION REQUIRED: Version approaching end of life
Favicon
Import the database from the Heroku dump
Favicon
Create and Connect to an Azure SQL Database on Heroku: A Step-By-Step Guide
Favicon
Buh-Bye Webpack and Node.js, Hello Rails and Import Maps
Favicon
How to Set Up Strapi and Deploy on Heroku
Favicon
Career Change: Corporate Ladder to Software Developer 🌟
Favicon
🌟 Corporate Ladder to Software Developer 🌟
Favicon
Exploring Key Features of Heroku and AWS: A Comparative Analysis
Favicon
how to deploy backend
Favicon
Installing Playwright on Heroku for Programmatic Node.js Browser Automation
Favicon
5 Simple Steps to Get Your Test Suite Running in Heroku CI
Favicon
When You Need More Power Than a Lambda Provides
Favicon
Deploying NestJS Apps to Heroku: A Comprehensive Guide
Favicon
Heroku for ChatOps: Start and Monitor Deployments from Slack
Favicon
How to Setup a Project That Can Host Up to 1000 Users for Free
Favicon
How to Heroku: Launch Your First App Webinar
Favicon
Working with Heroku Logplex for Comprehensive Application Logging

Featured ones: