Skip to content

Instantly share code, notes, and snippets.

@martinjaimem
Created April 24, 2022 23:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinjaimem/625ce0ffec72707fbecd772bafcd8292 to your computer and use it in GitHub Desktop.
Save martinjaimem/625ce0ffec72707fbecd772bafcd8292 to your computer and use it in GitHub Desktop.
Heroku review app creation with Github Actions
# Github Actions should look something like:
# name: CI
# on: [pull_request]
# jobs:
# build_review_app:
# name: Review app builder
# runs-on: ubuntu-latest
# steps:
# - name: Checkout code
# uses: actions/checkout@v2
# - name: Setup Ruby
# uses: ruby/setup-ruby@v1
# with:
# bundler-cache: true
# - name: Build Review App
# env:
# GH_TOKEN: ${{ secrets.GH_TOKEN }}
# HEROKU_TOKEN: ${{ secrets.HEROKU_TOKEN }}
# PIPELINE_ID: ${{ secrets.PIPELINE_ID }}
# run: |
# bundle exec rake heroku:app_build
# Add this line to your application's Gemfile:
# gem 'platform-api', '~> 3.0.0'
# Now we create the following task:
namespace :heroku do
desc 'Create and update review heroku app'
task :app_build do
heroku_token = ENV.fetch('HEROKU_TOKEN')
github_token = ENV.fetch('GH_TOKEN')
pipleine_id = ENV.fetch('PIPELINE_ID')
github_repository = ENV.fetch('GITHUB_REPOSITORY')
github_sha = ENV.fetch('GITHUB_SHA')
branch = ENV.fetch('GITHUB_HEAD_REF')
pr_number = ENV.fetch('GITHUB_REF').remove('refs/pull/').remove('/merge').to_i
client = PlatformAPI.connect_oauth(heroku_token)
review_app = PlatformAPI::ReviewApp.new(client)
build = PlatformAPI::Build.new(client)
url = "https://user:#{github_token}@api.github.com/repos/#{github_repository}/tarball/#{branch}"
# first we check if there is already a release for that app
created_review_app = review_app.list(pipleine_id).find { |app| app['pr_number'] == pr_number }
if created_review_app.blank?
puts "Creating app on Heroku and deploying branch #{branch} with PR #{pr_number}..."
puts review_app.create(
{
branch: branch,
pr_number: pr_number,
pipeline: pipleine_id,
source_blob: {
url: url,
version: github_sha
}
}
)
else
puts "Updating review app on Heroku for branch #{branch} with PR #{pr_number}..."
puts build.create(
created_review_app['app']['id'],
{
source_blob: {
checksum: nil,
url: url,
version: nil
}
}
)
end
end
end
@martinjaimem
Copy link
Author

In collaboration with @mcondecds

@dorianmariecom
Copy link

Nice, you got a typo pipleine_id

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