11.Create Release Tag #167
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: 11.Create Release Tag | |
on: | |
workflow_dispatch: | |
inputs: | |
tag: | |
type: string | |
description: Tag Version (excl. prefix; e.g. 1.2.3). Leave empty to auto-determine. | |
required: false | |
default: '' | |
jobs: | |
tag-changelog: | |
runs-on: ubuntu-22.04 | |
timeout-minutes: 10 | |
permissions: | |
contents: write | |
issues: write | |
pull-requests: write | |
steps: | |
- name: Fail on tags | |
run: exit 1 | |
if: ${{ !startsWith(github.ref, 'refs/heads/') }} | |
- uses: hmarr/debug-action@f7318c783045ac39ed9bb497e22ce835fdafbfe6 | |
- uses: actions/checkout@8edcb1bdb4e267140fa742c62e395cd74f332709 | |
with: | |
ssh-key: ${{ secrets.SSH_KEY }} | |
fetch-depth: 0 | |
fetch-tags: true | |
- name: List Tags | |
run: git tag | |
- name: Determine Tag Version | |
id: new-tag | |
run: | | |
if [ -n "${{ github.event.inputs.tag }}" ]; then | |
if ! echo "${{ github.event.inputs.tag }}" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$'; then | |
echo "Error: provided tag does not follow semantic versioning (major.minor.patch)" >&2 | |
exit 1 | |
fi | |
NEXT_VERSION="${{ github.event.inputs.tag }}" | |
NEXT_VERSION=${NEXT_VERSION//v} | |
else | |
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "") | |
if [ -z "$LAST_TAG" ]; then | |
echo "Error: no previous tag found and no tag input provided" >&2 | |
exit 1 | |
fi | |
if [[ $LAST_TAG =~ v([0-9]+)\.([0-9]+)\.([0-9]+) ]]; then | |
MAJOR="${BASH_REMATCH[1]}" | |
MINOR="${BASH_REMATCH[2]}" | |
PATCH="${BASH_REMATCH[3]}" | |
else | |
echo "Error: last tag '$LAST_TAG' does not follow expected format" >&2 | |
exit 1 | |
fi | |
echo "Last tag: $LAST_TAG" | |
COMMIT_MESSAGES=$(git log ${LAST_TAG}..HEAD --pretty=format:%B) | |
echo "Commit messages since last tag:" | |
echo "$COMMIT_MESSAGES" | |
if echo "$COMMIT_MESSAGES" | grep -q -E 'BREAKING CHANGE:' || \ | |
echo "$COMMIT_MESSAGES" | grep -q -E '^(feat|fix|refactor|chore|docs|style|test)(\(.+\))?!:'; then | |
BUMP="major" | |
elif echo "$COMMIT_MESSAGES" | grep -q -E '^feat(\(.+\))?:'; then | |
BUMP="minor" | |
else | |
BUMP="patch" | |
fi | |
echo "Bump type: $BUMP" | |
case "$BUMP" in | |
major) | |
MAJOR=$((MAJOR + 1)) | |
MINOR=0 | |
PATCH=0 | |
;; | |
minor) | |
MINOR=$((MINOR + 1)) | |
PATCH=0 | |
;; | |
patch) | |
PATCH=$((PATCH + 1)) | |
;; | |
esac | |
NEXT_VERSION="${MAJOR}.${MINOR}.${PATCH}" | |
echo "Determined next version: ${NEXT_VERSION}" | |
fi | |
echo "tag=${NEXT_VERSION}" >> $GITHUB_OUTPUT | |
- name: Setup Tools | |
uses: ./.github/actions/setup-tools | |
- name: Setup NPM Packages | |
uses: ./.github/actions/setup-npm | |
- name: Create a tag and update changelog | |
run: | | |
git config user.name "$GITHUB_ACTOR" | |
git config user.email "[email protected]" | |
git config commit.gpgsign false | |
branch=changelog/${{ steps.new-tag.outputs.tag }} | |
# Delete the branch locally if it exists | |
if git show-ref --verify --quiet "refs/heads/$branch"; then | |
git branch -D "$branch" | |
fi | |
# Delete the branch remotely if it exists | |
if git ls-remote --exit-code --heads origin "$branch"; then | |
git push origin --delete "$branch" | |
fi | |
git checkout -b "$branch" | |
git push --set-upstream origin "$branch" | |
git status | |
git reset --hard | |
# Pass the computed version WITHOUT a leading 'v'. | |
# Release-it will prepend 'v' itself, producing 'vX.Y.Z'. | |
./node_modules/.bin/release-it ${{ steps.new-tag.outputs.tag }} --ci | |
- name: Set output variables | |
id: vars | |
run: | | |
pr_title="chore(release): release candidate v${{ steps.new-tag.outputs.tag }}" | |
pr_body=$(git tag -l --format='%(contents)' v${{ steps.new-tag.outputs.tag }}) | |
echo "pr_title=$pr_title" >> $GITHUB_OUTPUT | |
echo "base=$GITHUB_REF_NAME" >> $GITHUB_OUTPUT | |
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) | |
echo "pr_body<<$EOF" >> "$GITHUB_OUTPUT" | |
echo "$pr_body" >> $GITHUB_OUTPUT | |
echo "$EOF" >> "$GITHUB_OUTPUT" | |
- name: Create Pull Request | |
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const { repo, owner } = context.repo; | |
const result = await github.rest.pulls.create({ | |
owner, | |
repo, | |
head: `changelog/${{ steps.new-tag.outputs.tag }}`, | |
base: `${{ steps.vars.outputs.base }}`, | |
title: `chore(release): release candidate v${{ steps.new-tag.outputs.tag }}`, | |
body: `### Commits | |
${{ steps.vars.outputs.pr_body }} | |
### GitHub Action | |
- [Build and deploy Apps in Test Environment](https://github.com/bcgov/platform-services-registry/actions/workflows/deploy-test.yml) | |
`, | |
}); | |
github.rest.issues.addLabels({ | |
owner, | |
repo, | |
issue_number: result.data.number, | |
labels: ['changelog', `v${{ steps.new-tag.outputs.tag }}`] | |
}); |