Deploy to Render
Deploy a Ruby on Rails application to Render
Used 63 times
C
Collin Jilbert
Usage
This railsbyte assumes that you are using Postgres and Puma. If that is true then this should work out just fine.
Run this command in your Rails app directory in the terminal:
rails app:template LOCATION="https://railsbytes.com/script/zJosEW"
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",
"bundle exec rails assets:precompile\n",
"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",
ipAllowList: []
},
services: [{
type: "web",
name: "rails",
plan: "free",
env: "ruby",
buildCommand: BUILD_SCRIPT_FILE_PATH,
startCommand: "bundle exec rails s",
envVars: [{
key: "RAILS_MASTER_KEY",
sync: false
}, {
key: "DATABASE_URL",
fromDatabase: {
name: "postgres",
property: "connectionString"
}
}]
}, {
type: "redis",
name: "redis",
ipAllowList: [],
plan: "free",
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
Comments
Collin Jilbert