Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cicirello/ade1d559a89104140557389365154bc1 to your computer and use it in GitHub Desktop.
Save cicirello/ade1d559a89104140557389365154bc1 to your computer and use it in GitHub Desktop.
Workflow to automate maintaining major release tag for GitHub Actions (e.g., from v3.1.2 to v3)

Automated Update of Major Release Tag

Purpose

GitHub's documentation for developers of GitHub Actions recommend that in addition to specific tagged releases with SemVer release numbers that a major release tag is maintained and moved to point to the most recent specific version. For example, let's say that you just release v3.1.2 (with corresponding tag). The recommendation is to then move a v3 tag to point to that new version. This gives users of the action the option to use specific release version, such as v3.1.2, or to just use the major release number, v3, which enables them to automatically gain the benefit of any bug fixes without updating their workflows.

What This Workflow Does

When you create a release, this workflow runs. It assumes that the release tag is of the form, vX.Y.Z (where X, Y, and Z are the major, minor, and patch numbers of a SemVer version). From this, it extracts the major version forming a string of the form vX. It then adds (if it doesn't exist) or moves (if it does exist) the tag with major version only to the new released version. For example, if the new release has a tag of v3.1.2, it will move the v3 tag to also point to the v3.1.2 release. Or for example, let's say you just released a new major version, with tag v4.0.0, this workflow will add a new tag v4 that also points to v4.0.0.

Usage

To use this workflow, drop the workflow file into the .github/workflows directory. And then edit your name and username where indicated.

# Copyright (c) 2021 Vincent A. Cicirello
# MIT License
name: Update Major Release Tag
on:
release:
types: [created]
jobs:
movetag:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Get major version num and update tag
run: |
VERSION=${GITHUB_REF#refs/tags/}
MAJOR=${VERSION%%.*}
git config --global user.name 'YOUR NAME HERE'
git config --global user.email 'USERNAME@users.noreply.github.com'
git tag -fa "${MAJOR}" -m 'Update major version tag'
git push origin "${MAJOR}" --force
@GMZwinge
Copy link

Minor improvement: add double quotes around ${MAJOR} to avoid ShellCheck SC2086 failure, eg in a repo based on actions/typescript-action during the GITHUB_ACTIONS linting in the Lint Codebase workflow. Also, single quote should be enough around Update major version tag.

@cicirello
Copy link
Author

@GMZwinge thanks for the suggestion. I updated the workflow with the suggestions. I also updated the version of the checkout action, which has had multiple updates since I originally posted this gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment