Automated Package Publishing with GitHub Actions
You received a pull-request. You're grateful, but sigh...now you need to release a new version.
by Ray Zane
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:
- Clone the repository.
- Run the tests one last time. Try not to forget this step!
- Bump and commit a version change.
- Create a new git tag.
- 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
.
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/setup-[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:
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:
$ echo "$GITHUB_REF"
refs/tags/v1.2.3
You can extract the version number from this git tag like so:
$ echo "${GITHUB_REF:11}"
1.2.3
For NPM packages, you can set the version using the following command:
$ 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:
$ 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!