Update Heroku environment variable after GitHub release
Tags: github, heroku, workflow, tutorial
Published: 2020-07-17 23:50:11

---
title: Update Heroku environment variable after GitHub release
published: true
description: Using GitHub Actions to automate integration
tags: github, heroku, workflow, tutorial
cover_image: https://res.cloudinary.com/arthurdenner/image/upload/v1594143886/posts/github_actions_heroku.png
---

## Context
 
If we are using [GitHub releases](https://docs.github.com/en/github/administering-a-repository/releasing-projects-on-github) 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](https://sentry.io/welcome/)'s.

## Services

### Heroku

[Heroku](https://www.heroku.com/) 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](https://docs.github.com/en/actions/reference/events-that-trigger-workflows) and perform many tasks from them.

There are thousands of actions available on the [marketplace](https://github.com/marketplace?type=actions) 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](https://dashboard.heroku.com/account/applications/authorizations/new).

### 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:

```yml
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](https://github.com/arthurdenner/releases-sentry-heroku-github-actions). Check out the [Actions tab](https://github.com/arthurdenner/releases-sentry-heroku-github-actions/actions) 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](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#release-event-release).

It's possible to run other Heroku commands through the workflow. Check out their CLI [documentation](https://devcenter.heroku.com/categories/command-line) for more commands or check out [this post](https://dev.to/heroku/deploying-to-heroku-from-github-actions-29ej) for details on an entire deployment workflow.

---

I hope you enjoyed this post and [follow me](https://linktr.ee/arthurdenner) on any platform for more.