Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@swanson
Last active November 27, 2022 00:40
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swanson/527e20ea124c6896e9a0ca20a60c0d3e to your computer and use it in GitHub Desktop.
Save swanson/527e20ea124c6896e9a0ca20a60c0d3e to your computer and use it in GitHub Desktop.
name: Deploy
on:
workflow_run:
workflows: [CI Build] # change this to match your build workflow name
types: [completed]
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
environment:
name: your-environment-name-here
url: https://your-app.example.com
name: Deploy demo
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Push to Heroku
run: git push -f https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git main
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
HEROKU_APP_NAME: your-heroku-app
@swanson
Copy link
Author

swanson commented Apr 27, 2022

GitHub Actions workflow to deploy to Heroku on commits to main branch that have passed a CI Build workflow

Credit to this guide as a starting point: https://remarkablemark.org/blog/2021/03/12/github-actions-deploy-to-heroku/

A few things you'll need to change:

  • on.workflow_run.workflows: set this to be the exact name of "build" workflow that you want to wait for before trying to deploy. The code above will run the deploy job if our CI Build workflow succeeds
  • HEROKU_APP_NAME: set this to be your app name, obviously

If you want to deploy to multiple Heroku apps (demo / prod), copy-paste the job and tweak accordingly.

We are using the native GitHub Slack integration to get notifications in Slack for deployments.

@defkode
Copy link

defkode commented Apr 27, 2022

With some small tweaks, you can achieve a nicer UI:

jobs.deploy -> jobs.deployment

jobs:
  deployment:
    environment:
      name: production
      url: https://your-heroku-app.herokuapp.com

Screenshot 2022-04-27 at 19 29 33

Screenshot 2022-04-27 at 19 32 48

@mbaird
Copy link

mbaird commented Apr 27, 2022

If you're just using git push (and not interacting with Heroku in any other way), you can get away without the CLI entirely:

git push -f https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git main

@swanson
Copy link
Author

swanson commented Apr 27, 2022

If you're just using git push (and not interacting with Heroku in any other way), you can get away without the CLI entirely:

git push -f https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git main

Awesome, thank you. Switching over and then will update. Much simpler!

@swanson
Copy link
Author

swanson commented Apr 27, 2022

With some small tweaks, you can achieve a nicer UI:

jobs.deploy -> jobs.deployment

jobs:
  deployment:
    environment:
      name: production
      url: https://your-heroku-app.herokuapp.com
Screenshot 2022-04-27 at 19 29 33 Screenshot 2022-04-27 at 19 32 48

Oh, very nice! And then we can use the GitHub slack integration instead of the action. Will update.

@mbaird
Copy link

mbaird commented Apr 27, 2022

Just noticed I did sneak a force flag (-f) into the push command which you may not want. It made sense for my use case, but I guess it's safer without!

@swanson
Copy link
Author

swanson commented Apr 27, 2022

Just noticed I did sneak a force flag (-f) into the push command which you may not want. It made sense for my use case, but I guess it's safer without!

I think it's appropriate as sometimes people manually deploy different branches.

@mbaird
Copy link

mbaird commented Apr 27, 2022

Just noticed I did sneak a force flag (-f) into the push command which you may not want. It made sense for my use case, but I guess it's safer without!

I think it's appropriate as sometimes people manually deploy different branches.

Makes sense. You may want to add a concurrency key, in case you merge a couple PRs in quick succession and their CI workflows complete out of order.

Edit: Actually I'm not even sure that'd keep you safe. Here be dragons -f.

@defkode
Copy link

defkode commented Apr 27, 2022

You may want to add a concurrency key, in case you merge a couple PRs in quick succession and their CI workflows complete out of order.

Yes. Very important addition

@defkode
Copy link

defkode commented Apr 27, 2022

this is not really important, but eye catching:

naming job deployment instead of deploy adds some kind of progressbar + text: deploying to

Screenshot 2022-04-27 at 21 27 04

@swanson
Copy link
Author

swanson commented Apr 27, 2022

this is not really important, but eye catching:

naming job deployment instead of deploy adds some kind of progressbar + text: deploying to

Screenshot 2022-04-27 at 21 27 04

The progress bar + text shows up for without explicitly naming it "deployment" -- I think it's the presence of environment

image

@defkode
Copy link

defkode commented Apr 27, 2022

I think it's the presence of environment

You are right.

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