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
@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