Last active
January 2, 2023 19:34
-
-
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
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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@chaadow Awesome! Glad to hear it.