Arthur Denner
Posted on July 17, 2020
Context
If we are using GitHub releases as part of our release workflow, we can use GitHub Actions to accomplish many tasks, such as notifying services about the new release.
This is very helpful as we can use a single step on our workflow to keep all integrated services in sync.
In this tutorial, we'll update an environment variable on Heroku. A real-world scenario for this is an API built with Node.js that has an endpoint for its current version running or uses the version on error monitoring clients like Sentry's.
Services
Heroku
Heroku is a container-based cloud Platform as a Service (PaaS) that supports several programming languages. It enables simple application development and deployment and it's very beginner-friendly.
GitHub Actions
GitHub Actions enables automation of our software workflows. It's possible to run workflows with many triggers and perform many tasks from them.
There are thousands of actions available on the marketplace and built by the community that enables us to lint, format, test, build, deploy our projects... The use cases are endless.
Setup
Heroku
In order to communicate with Heroku, we need an auth token. Generate one by running heroku authorizations:create
on the Heroku CLI or through the dashboard.
Secrets
We need to setup 2 GitHub secrets on our repo. Go to Settings > Secrets
and create the following:
-
HEROKU_API_KEY
: Heroku auth token created before; -
HEROKU_APP_NAME
: Name of the project created on Heroku.
Workflow
With all the setup finished, create a YAML file under .github/workflows
, e.g. create-releases.yml
with the following content:
name: Create releases
on:
release:
types: [published]
jobs:
update-heroku-env-variable:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@master
- name: Trim tag name
run: echo '::set-env name=API_VERSION::${{ github.ref }}'
- name: Update API_VERSION on Heroku
run: heroku config:set API_VERSION=${API_VERSION##*/} -a ${{ secrets.HEROKU_APP_NAME }}
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
And that's it! Now, every time we publish a new release on GitHub, the API_VERSION
environment variable on Heroku will be updated accordingly.
Note: The "Trim tag name" step will modify the tag name from refs/tags/x.x.x
to only x.x.x
.
Check out an example repo here. Check out the Actions tab to see details of the workflow.
Notes
The example above only updates the environment variable when our GitHub release is published. However, it's possible to run workflows on other events of a release - see GitHub's documentation.
It's possible to run other Heroku commands through the workflow. Check out their CLI documentation for more commands or check out this post for details on an entire deployment workflow.
I hope you enjoyed this post and follow me on any platform for more.
Posted on July 17, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.