Skip to content

Create Release Tag

Create Release Tag #1

Workflow file for this run

name: Create Release Tag
on:
workflow_dispatch:
inputs:
version:
description: "Release version (e.g. 1.0.0)."
required: true
default: "1.0.0"
permissions:
contents: write
jobs:
create-tag:
name: Create and push tag
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Validate and normalize version input
id: normalize
run: |
set -euo pipefail
raw_version="${{ github.event.inputs.version }}"
if [ -z "$raw_version" ]; then
echo "No version provided. Use workflow_dispatch input 'version'." >&2
exit 1
fi
# strip any leading 'v' or 'V'
normalized=$(echo "$raw_version" | sed -E 's/^[vV]//')
if [ -z "$normalized" ]; then
echo "Provided version '$raw_version' is invalid after stripping leading 'v' — please supply a semantic version like 1.2.3." >&2
exit 1
fi
# simple semver-like warning (not fully strict), continue even if it doesn't match
if ! echo "$normalized" | grep -E -q '^[0-9]+(\.[0-9]+){1,2}([-.+][0-9A-Za-z.-]+)?$'; then
echo "Warning: version '$normalized' doesn't look like semver (X.Y or X.Y.Z). Continuing anyway." >&2
fi
tag="v${normalized}"
echo "Normalized version: $normalized"
echo "tag=$tag" >> "$GITHUB_OUTPUT"
- name: Confirm tag does not already exist
run: |
tag=${{ steps.normalize.outputs.tag }}
echo "Checking if tag $tag exists on origin..."
if git ls-remote --tags origin | grep -q "refs/tags/$tag"; then
echo "Tag $tag already exists on origin — aborting to avoid overwriting." >&2
exit 1
fi
- name: Create annotated tag and push to origin
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
tag=${{ steps.normalize.outputs.tag }}
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
echo "Creating annotated tag $tag"
git tag -a "$tag" -m "Release $tag"
# Set remote URL that uses the token to push the new tag
remote_url="https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.repository }}"
git remote set-url origin "$remote_url"
git push origin "$tag"
- name: Create GitHub release for the tag
uses: actions/create-release@v1
with:
tag_name: ${{ steps.normalize.outputs.tag }}
release_name: "Release ${{ steps.normalize.outputs.tag }}"
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}