DEV Community

Mike Coutermarsh
Mike Coutermarsh

Posted on • Edited on

8

GitHub Actions - Deploy to Heroku

Here's how to deploy to Heroku using GitHub Actions. Without adding any new dependencies.

First, you need a Heroku auth token. You can generate one on your machine using the following:

heroku authorizations:create

Copy the Token value and add it as a secret to your repository (Go to your repository Settings -> Secrets. Name it HEROKU_API_TOKEN)

Next, add this to your workflow.

- name: Deploy to Heroku
  env:
    HEROKU_API_TOKEN: ${{ secrets.HEROKU_API_TOKEN }}
    HEROKU_APP_NAME: "your-app-name-here"
  if: github.ref == 'refs/heads/master' && job.status == 'success'
  run: git push https://heroku:$HEROKU_API_TOKEN@git.heroku.com/$HEROKU_APP_NAME.git origin/master:master

This will only run for builds on the Master branch and if previous steps have worked properly.

Here's a full example for a Rails app. This runs your tests, and if successful, deploys to Heroku.

name: Ruby Test and Deploy
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: Set up Ruby
      uses: actions/setup-ruby@v1
      with:
        ruby-version: 2.6.3
    - uses: actions/cache@v1
      with:
        path: vendor/bundle
        key: ${{ runner.os }}-gem-${{ hashFiles('**/Gemfile.lock') }}
        restore-keys: |
          ${{ runner.os }}-gem-
    - name: Install Bundler
      run: |
        gem install bundler
    - name: Install Gems
      run: bundle install --path vendor/bundle --jobs 4 --retry 3
    - name: Run Tests
      run: |
        bundle exec rake test
    - name: Deploy to Heroku
      env:
        HEROKU_API_TOKEN: ${{ secrets.HEROKU_API_TOKEN }}
        HEROKU_APP_NAME: "your-app-name-here"
      if: github.ref == 'refs/heads/master' && job.status == 'success'
      run: git push https://heroku:$HEROKU_API_TOKEN@git.heroku.com/$HEROKU_APP_NAME.git origin/master:master

Neon image

Build better on Postgres with AI-Assisted Development Practices

Compare top AI coding tools like Cursor and Windsurf with Neon's database integration. Generate synthetic data and manage databases with natural language.

Read more →

Top comments (1)

Collapse
 
marckohlbrugge profile image
Marc Köhlbrugge

Thanks for sharing this, Mike! Been experimenting with GitHub Actions lately, but wasn't able to figure out how to deploy to Heroku. Also nice to see the cache action. That will speed up builds dramatically.

In case anyone wonders how to run database migrations as part of your deployment process, you can simply add the following to your Procfile: release: bin/rails db:migrate. Heroku will then take care of it for you. (docs)

Alternatively, you could probably use the Heroku action, but the above feels much simpler.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay