Last active
May 5, 2022 12:25
-
-
Save timm-oh/79e847cbb94b69d3f80339e8301afa2f to your computer and use it in GitHub Desktop.
Heroku 'Review app' setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
pipeline = "PIPELINE_UUID" | |
pipeline_owner = "enterprise_team_name or email_address" | |
name = "heroku_app_name" | |
# CREATE APP | |
`heroku apps:create #{name} --team=#{pipeline_owner} --region=eu` # using eu region, otherwise it defaults to us | |
info_output = `heroku apps:info #{name} --json` | |
app_info = JSON.parse(info_output) | |
# SET ENV VARS | |
`heroku config:set APPNAME="#{name}" -a #{name}` | |
app_config = JSON.parse(File.read('app.json')) | |
# ADD TO PIPELINE | |
`heroku pipelines:add #{pipeline} -a #{name} --stage=staging` | |
# ADD BUILDPACKS | |
app_config["buildpacks"].map do |buildpack| | |
buildpack["url"] | |
end.each.with_index do |buildpack, index| | |
`heroku buildpacks:add --index #{index + 1} #{buildpack} -a #{name}` | |
end | |
# CREATE ADDONS | |
app_config["addons"].each do |addon| | |
parsed_addon = if addon.is_a? String | |
addon | |
else | |
options = (addon["options"] || {}).map do |key, value| | |
"--#{key}=#{value}" | |
end.join(" ") | |
"#{addon["plan"]} #{options}" | |
end | |
`heroku addons:create #{parsed_addon} -a #{name}` | |
end | |
# DEPLOY CODE | |
current_branch = `git rev-parse --abbrev-ref HEAD` | |
`git push #{app_info.dig('app', 'git_url')} #{current_branch}:main` | |
# SCALE DYNOS | |
dyno_scale = app_config['formation'].map do |dyno, config| | |
quantity = [config["quantity"].to_i, 1].max | |
type = (config["size"] || "Standard-1X").downcase | |
"#{dyno}=#{quantity}:#{type}" | |
end.join(" ") | |
`heroku ps:scale #{dyno_scale} -a #{name}` | |
# POST DEPLOY STUFF | |
postdeploy = app_config.dig("scripts", "postdeploy") | |
`heroku run '#{postdeploy}' -a #{name}` if postdeploy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
pipeline = "PIPELINE_UUID" | |
prefix = "add_cool_prefix_here" | |
pipeline_owner = "enterprise_owner or email_address" | |
current_branch = `git rev-parse --abbrev-ref HEAD`.strip | |
name = "#{prefix}-#{current_branch}"[0...30] | |
app_config = JSON.parse(File.read('app.json')) | |
def app_info(name) | |
result = `heroku apps:info #{name} --json` | |
JSON.parse(result) | |
rescue JSON::ParserError | |
nil | |
end | |
def create_application(name, pipeline_owner, pipeline) | |
`heroku apps:create #{name} --team=#{pipeline_owner} --region=eu` | |
`heroku pipelines:add #{pipeline} -a #{name} --stage=staging` | |
end | |
def set_env_vars(name) | |
raise "NOT IMPLEMENTED" | |
# example: `heroku config:set APP_NAME=#{name} -a #{name}` | |
end | |
def add_buildpacks(name, app_config) | |
app_config["buildpacks"].map do |buildpack| | |
buildpack["url"] | |
end.each.with_index do |buildpack, index| | |
`heroku buildpacks:add --index #{index + 1} #{buildpack} -a #{name}` | |
end | |
end | |
def add_addons(name, app_config) | |
app_config["addons"].each do |addon| | |
parsed_addon = if addon.is_a? String | |
addon | |
else | |
options = (addon["options"] || {}).map do |key, value| | |
"--#{key}=#{value}" | |
end.join(" ") | |
"#{addon["plan"]} #{options}" | |
end | |
`heroku addons:create #{parsed_addon} -a #{name}` | |
end | |
end | |
def deploy_latest_commit(name, branch, git_remote) | |
`git push --force #{git_remote} #{branch}:main` | |
end | |
def scale_dynos(name, app_config) | |
dyno_scale = app_config['formation'].map do |dyno, config| | |
quantity = [config["quantity"].to_i, 1].max | |
type = (config["size"] || "Standard-1X").downcase | |
"#{dyno}=#{quantity}:#{type}" | |
end.join(" ") | |
`heroku ps:scale #{dyno_scale} -a #{name}` | |
end | |
def post_deploy(name, app_config) | |
postdeploy = app_config.dig("scripts", "postdeploy") | |
`heroku run '#{postdeploy}' -a #{name}` if postdeploy | |
end | |
# SECRET SAUCE | |
if app_info = app_info(name) | |
deploy_latest_commit(name, current_branch, app_info.dig('app', 'git_url')) | |
else | |
create_application(name, pipeline_owner, pipeline) | |
set_env_vars(name) | |
add_buildpacks(name, app_config) | |
add_addons(name, app_config) | |
app_info = app_info(name) | |
deploy_latest_commit(name, current_branch, app_info.dig('app', 'git_url')) | |
scale_dynos(name, app_config) | |
post_deploy(name, app_config) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name = "heroku_app_name" | |
info_output = `heroku apps:info #{name} --json` | |
app_info = JSON.parse(info_output) | |
current_branch = `git rev-parse --abbrev-ref HEAD`.strip | |
`git push --force #{app_info.dig('app', 'git_url')} #{current_branch}:main` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this should work too without sending e-mails on each transfer to the org