gears

You received a pull-request. You’re grateful, but sigh… now you need to release a new version.

Here are all of the things you’re supposed to do:

  1. Clone the repository.
  2. Run the tests one last time. Try not to forget this step!
  3. Bump and commit a version change.
  4. Create a new git tag.
  5. Create a new GitHub Release describing what changed.

This is a bit of a chore, so I wanted to automate this entire process with GitHub Actions.

But first, a few notes about my ideal solution:

  • I want publishing a release to be a single step.
  • I want this solution to be language agnostic, because I maintain packages in a few different languages.
  • I don’t want to use structured commit messages.

The Workflow

Without further ado, this is the workflow that I came up with. It lives in .github/workflows/action.yml.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
name: Publish
on:
  release:
    types: [published]
jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/[email protected]

      - name: Setup Node.js
        uses: actions/[email protected]
        with:
          node-version: 12
          registry-url: https://registry.npmjs.org

      - name: Install dependencies
        run: yarn install --frozen-lockfile

      - name: Test
        run: yarn test --ci --coverage

      - name: Version
        run: yarn version --new-version "${GITHUB_REF:11}" --no-git-tag-version

      - name: Publish
        run: yarn publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

Now, creating a release is as simple as creating a new GitHub release:

Creating a GitHub Release
Releasing v1.2.3 of my package

Versioning

Most open source projects commit their current version to a source file. With this automated workflow, I’d recommend not doing that.

Instead, just set your version to 0.0.0. Then, when you build your package, set the version based on the current git tag.

In the example above, $GITHUB_REF will reflect the git tag associated with your GitHub Release:

1
2
$ echo "$GITHUB_REF"
refs/tags/v1.2.3

You can extract the version number from this git tag like so:

1
2
$ echo "${GITHUB_REF:11}"
1.2.3

For NPM packages, you can set the version using the following command:

1
$ yarn version --new-version "${GITHUB_REF:11}" --no-git-tag-version

But, for other types of packages, a find and replace should do the trick:

1
$ perl -pe -i "s/0\.0\.0/${GITHUB_REF:11}/" lib/mylib/version.rb

Publishing

The “Publish” step will vary based on your language. GitHub provides a few examples for this step:

Good luck and keep releasing!