Saltar al contenido
mypipelines
Shared (cross-cutting)·Reusable workflow·on: workflow_call

Shared Tag Release

Shared - Tag Release

.github/workflows/shared-tag-release.yml

.github/workflows/shared-tag-release.yml
name: Shared - Tag Release
on:
workflow_call:
inputs:
tag_prefix:
description: 'Prefix for version tags'
required: false
type: string
default: 'v'
major_pattern:
description: 'Regex pattern for major version bump'
required: false
type: string
default: '(MAJOR|BREAKING CHANGE)'
minor_pattern:
description: 'Regex pattern for minor version bump'
required: false
type: string
default: '(feat)'
annotated:
description: >-
Create an annotated tag. false creates a lightweight tag, which is what a
repository must publish when its tags are pinned as reusable-workflow refs:
through an annotated tag GitHub cannot resolve a called workflow's nested
./ calls on push events, and the caller aborts at startup.
required: false
type: boolean
default: true
outputs:
version:
description: 'Semantic version (e.g. 1.2.3)'
value: ${{ jobs.tag.outputs.version }}
version_tag:
description: 'Version tag (e.g. v1.2.3)'
value: ${{ jobs.tag.outputs.version_tag }}
# Least privilege for what this workflow actually does. Without it the
# job inherits whatever the calling repository's default happens to be,
# which on older repositories is write-all.
permissions:
contents: write
jobs:
tag:
name: Tag Release
runs-on: ubuntu-latest
timeout-minutes: 10
outputs:
version: ${{ steps.semver.outputs.version }}
version_tag: ${{ steps.semver.outputs.version_tag }}
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
- name: Fetch all tags
run: git fetch --tags --force
- name: Calculate semantic version
id: semver
env:
TAG_PREFIX: ${{ inputs.tag_prefix }}
MAJOR_PATTERN: ${{ inputs.major_pattern }}
MINOR_PATTERN: ${{ inputs.minor_pattern }}
run: |
LAST_TAG=$(git tag -l "${TAG_PREFIX}*" --sort=-v:refname | head -n1)
if [ -z "$LAST_TAG" ]; then
LAST_TAG="${TAG_PREFIX}0.0.0"
echo "No previous tag found, starting from $LAST_TAG"
else
echo "Last tag: $LAST_TAG"
fi
LAST_VERSION="${LAST_TAG#"${TAG_PREFIX}"}"
IFS='.' read -r MAJOR MINOR PATCH <<< "$LAST_VERSION"
if git rev-parse "$LAST_TAG" >/dev/null 2>&1; then
COMMITS=$(git log "${LAST_TAG}..HEAD" --pretty=format:'%s%n%b')
else
COMMITS=$(git log --pretty=format:'%s%n%b')
fi
if [ -z "$COMMITS" ]; then
echo "No new commits since $LAST_TAG"
VERSION="${MAJOR}.${MINOR}.${PATCH}"
elif echo "$COMMITS" | grep -qE "$MAJOR_PATTERN"; then
MAJOR=$((MAJOR + 1)); MINOR=0; PATCH=0
VERSION="${MAJOR}.${MINOR}.${PATCH}"
elif echo "$COMMITS" | grep -qE "$MINOR_PATTERN"; then
MINOR=$((MINOR + 1)); PATCH=0
VERSION="${MAJOR}.${MINOR}.${PATCH}"
else
PATCH=$((PATCH + 1))
VERSION="${MAJOR}.${MINOR}.${PATCH}"
fi
echo "Calculated version: $VERSION"
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
echo "version_tag=${TAG_PREFIX}${VERSION}" >> "$GITHUB_OUTPUT"
- name: Check if tag exists
id: check-tag
run: |
TAG="${{ inputs.tag_prefix }}${{ steps.semver.outputs.version }}"
if git rev-parse "$TAG" >/dev/null 2>&1; then
echo "exists=true" >> "$GITHUB_OUTPUT"
echo "Tag $TAG already exists, skipping"
else
echo "exists=false" >> "$GITHUB_OUTPUT"
fi
- name: Create and push tag
if: steps.check-tag.outputs.exists == 'false'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ANNOTATED: ${{ inputs.annotated }}
run: |
TAG="${{ inputs.tag_prefix }}${{ steps.semver.outputs.version }}"
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
if [ "$ANNOTATED" = "true" ]; then
git tag -a "$TAG" -m "Release $TAG"
else
git tag "$TAG"
fi
git push origin "$TAG"
echo "### Tag Created: $TAG" >> $GITHUB_STEP_SUMMARY
- name: Create GitHub Release
if: steps.check-tag.outputs.exists == 'false'
env:
GH_TOKEN: ${{ github.token }}
TAG_PREFIX: ${{ inputs.tag_prefix }}
run: |
TAG="${{ inputs.tag_prefix }}${{ steps.semver.outputs.version }}"
# Get previous tag globally (skip the one we just created)
PREV_TAG=$(git tag -l "${TAG_PREFIX}*" --sort=-v:refname | grep -v "^${TAG}$" | head -n1)
if [ -n "$PREV_TAG" ] && git rev-parse "$PREV_TAG" >/dev/null 2>&1; then
CHANGELOG=$(git log --pretty=format:'- %s (%h)' "${PREV_TAG}..HEAD" | head -30)
else
CHANGELOG=$(git log --pretty=format:'- %s (%h)' | head -30)
fi
gh release create "$TAG" \
--title "Release $TAG" \
--notes "## Changes
${CHANGELOG}" \
--latest