GitHub Actions — Run a Workflow When Creating a Tag

GitHub Actions provider powerful configurations. You can customize the event triggers to run a job to your own needs.

We use a handy workflow to publish our packages to the GitHub Package Registry in our repositories. The “publish workflow” only runs when creating a new tag in a repository.

GitHub Actions Series Overview

Run Workflow for New Tags

GitHub offers a package registry for a variety of modules: NPM, Docker, Gradle, Maven, NuGet, and RubyGems. This registry in the Node.js ecosystem comes with extra effort for package authors because you need to publish to different registries, NPM and GitHub.

You can automate the publishing process on GitHub’s side by using an Actions workflow. Restrict the workflow to only run when a new tag is pushed to the repository. Then, you can run the publishing process of your package to make the new version available in the GitHub Package Registry.

Here’s a workflow configuration that only runs when creating a new tag:

name: Publish in GitHub Package Registry

on:  
  push:
    tags:
      - '*'

jobs:  
  build: …

You can even customize the publishing process with tags to ignore. For example, if you’re preparing the new major release of your project, you may want to skip any public releases. Use the tags-ignore pattern to not run workflows:

name: Publish in GitHub Package Registry

on:  
  push:
    tags:
      - '1.*'
    tags-ignore:
      - '2.*'

jobs:  
  build: …

This workflow configuration publishes new versions for the 1.x release line and skips the package release for 2.x.

Example Workflow

Find an example GitHub Actions workflow in the supercharge/strings repository on GitHub. The linked workflow only runs when pushing a new tag to the repository.


Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.