Deploy to Render with yaml fix
Deploy a Ruby on Rails application to Render
Used 12 times
S
Stephane Liu
Usage
Run this command in your Rails app directory in the terminal:
rails app:template LOCATION="https://railsbytes.com/script/XvEsMb"
Template Source
Review the code before running this template on your machine.
# Code for Render build script file
#
#
#
#
# These are the default steps that the script takes
BUILD_STEPS = [
"#!/usr/bin/env bash\n\n",
"# exit on error\n",
"set -o errexit\n\n",
"bundle install\n",
"SECRET_KEY_BASE_DUMMY=1 bundle exec rails assets:precompile\n",
"SECRET_KEY_BASE_DUMMY=1 bundle exec rails assets:clean\n",
"bundle exec rails db:migrate"
]
# Render will look for this file in the application structure
BUILD_SCRIPT_FILE_PATH = "./bin/render-build.sh"
# Method for creating the build script file
def generate_build_script_file(build_script_path)
File.open(build_script_path, "w") do |f|
BUILD_STEPS.each do |step|
f.write(step)
end
end
end
# Ask user if they want to overwrite the render-build.sh file
# if it already exists. If yes, overwrite the current file. Otherwise
# do nothing but ensure that the file is executable.
begin
if File.exist?(BUILD_SCRIPT_FILE_PATH)
if yes?("Detected bin/render-build, would like you to overwrite it? [y/n]")
generate_build_script_file(BUILD_SCRIPT_FILE_PATH)
end
else
generate_build_script_file(BUILD_SCRIPT_FILE_PATH)
end
ensure
# Make the render-build.sh file executable
FileUtils.chmod("a+x", BUILD_SCRIPT_FILE_PATH)
end
# Code for Render yaml config file
#
#
#
#
# This sets up a render configuration hash that is later
# output to a file as yaml
RENDER_CONFIG = {
"databases" => {
"name" => "postgres",
"databaseName" => "",
"user" => "",
"plan" => "free"
},
services: [{
"type" => "web",
"name" => "rails",
"plan" => "free",
"env" => "ruby",
"buildCommand" => BUILD_SCRIPT_FILE_PATH,
"startCommand" => "bundle exec rails s",
"envVars" => [
{
"key" => "DATABASE_URL",
"fromDatabase" => {
"name" => "postgres",
"property" => "connectionString"
}
},
{
"key" => "RAILS_MASTER_KEY",
"sync" => false
},
{
"key" => "WEB_CONCURRENCY",
"value" => 2
}
]
},
{
"type" => "redis",
"name" => "redis",
"plan" => "free",
"ipAllowList" => [],
"maxmemoryPolicy" => "noeviction"
}]
}
# Render will look for this file in the application structure
RENDER_YAML_FILE = "./render.yaml"
# Method for creating the Render configuration file
def generate_render_yaml_file(render_yaml_filename_str)
File.open(render_yaml_filename_str, "w") do |f|
YAML.dump(RENDER_CONFIG, f)
end
end
# Here is where we output the above configuration
# as yaml to the "./render.yaml" file
if File.exist? RENDER_YAML_FILE
if yes?("Detected render.yaml, would like you to overwrite it? [y/n]")
generate_render_yaml_file(RENDER_YAML_FILE)
end
else
generate_render_yaml_file(RENDER_YAML_FILE)
end
# Code for modifying puma.rb file
#
#
#
#
# Uncomment the necessary lines inside ./config/puma.rb
File.open("./config/puma.rb", "r+") do |f|
lines = f.readlines
updated_puma_config = lines.map do |line|
line.sub!(/^# /, "").sub!("2", "4") if line.match?(/^# (workers|preload)/)
line
end
f.rewind
f.write(updated_puma_config.join)
end
say("****** Update databaseName and user in render.yaml ******")