-
-
Save mpvosseller/a2dc96cf1bb5f9de0db9d6963c3e7a97 to your computer and use it in GitHub Desktop.
| # 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 Sorry, I have not run into that issue. Are you sure you passing in the correct setup_id? Would look something like this:
setup_response = create_heroku_app(...)
setup_id = setup_response["id"]
wait_for_app_setup(setup_id)It worked, thanks again!
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 👌
@chaadow Awesome! Glad to hear it.
app_setup_client.info(setup_id)sometimes throws a 500 error ( the app still is created so thankfully not that blocking) i haven't still investigated the issue, but have u encountered the error, and if so do u know what might cause the issue