Skip to content

Instantly share code, notes, and snippets.

@mpvosseller
Last active January 2, 2023 19:34
Show Gist options
  • Save mpvosseller/a2dc96cf1bb5f9de0db9d6963c3e7a97 to your computer and use it in GitHub Desktop.
Save mpvosseller/a2dc96cf1bb5f9de0db9d6963c3e7a97 to your computer and use it in GitHub Desktop.
Ruby function to create a new Heroku app with the Heroku Platform API
# Function to create a new Heroku app instance using the ruby Heroku Platform API
#
# Documentation:
# https://devcenter.heroku.com/articles/setting-up-apps-using-the-heroku-platform-api
# https://devcenter.heroku.com/articles/platform-api-reference#app-setup
# https://github.com/heroku/platform-api
# https://heroku.github.io/platform-api
#
# Add the gem 'platform-api'
require "platform-api"
def create_heroku_app(app_name:, heroku_org:, branch:, github_account:, github_repo:, config_vars:)
# requires a github token with "repo" access. Create one here: https://github.com/settings/tokens
github_token = ENV["GITHUB_ACCESS_TOKEN"]
raise "GITHUB_ACCESS_TOKEN" unless github_token
heroku_token = ENV["HEROKU_TOKEN"] || `heroku auth:token`.strip.presence
raise "You are not logged into heroku cli. Log in with heroku login" unless heroku_token
client = PlatformAPI.connect_oauth(heroku_token)
app_setup_client = PlatformAPI::AppSetup.new(client)
app_setup_client.create(
{
app: {
name: app_name,
organization: heroku_org
},
source_blob: {
url:
"https://user:#{github_token}@api.github.com/repos/#{github_account}/#{github_repo}/tarball/#{branch}"
},
overrides: {
env: config_vars
}
}
)
end
@chaadow
Copy link

chaadow commented Apr 26, 2022

It worked, thanks again!

@chaadow
Copy link

chaadow commented Jan 1, 2023

Thanks to you, I've recreated an entire review app generation system that integrates with github through a webhook. and Even though the github integration came back, I just stuck with my system since it's better :D

The platform API is really cool and allows you to customize a LOT. The heroku github integration has its own limitations, ( cannot execute scripts at certain levels/states of the deployment)

I can launch a db restoration process, shutdown/startup dynos on the fly, remap the dyno formation, create addons on the fly by adding enabling/disabling github labels etc..
It also allowed us to optimize drastically our dyno/addon costs!

Thanks again, your gist in addition to the def wait_for_app_setup(setup_id) ( i've opted for a clock process that checks on the status every minute and saves the build state in a db table ) were more than enough to get me started 👌

@mpvosseller
Copy link
Author

@chaadow Awesome! Glad to hear it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment