Skip to content

feat(github-actions): add script to get latest tag for current commit #12

feat(github-actions): add script to get latest tag for current commit

feat(github-actions): add script to get latest tag for current commit #12

name: Auto Tagging After Tests
on:
push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: main
fetch-depth: 0 # Fetch all history
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.22.5
- name: Run tests
run: |
go test -race -v ./...
tagging:
needs: [ test ]
if: success()
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: main
fetch-depth: 0 # Fetch all history
- name: Get latest release
id: get_latest_release
run: |
LATEST_RELEASE=$(curl -s -H "Authorization: token ${{ env.GITHUB_TOKEN }}" https://api.github.com/repos/${{ github.repository }}/releases/latest | jq -r '.tag_name')
echo "LATEST_RELEASE=$LATEST_RELEASE" >> $GITHUB_OUTPUT
- name: Determine next version
id: determine_next_version
run: |
chmod +x ./scripts/git/commit_tag.sh
LATEST_TAG=$(./scripts/git/commit_tag.sh)
if [[ -z "$LATEST_TAG" ]]; then
NEXT_TAG="v0.0.1"
else
echo "LATEST_TAG=$LATEST_TAG" >> $GITHUB_OUTPUT
# Remove 'v' prefix using bash parameter expansion
LATEST_TAG=${LATEST_TAG#v}
IFS='.' read -r -a VERSION_ARRAY <<< "$LATEST_TAG"
MAJOR=${VERSION_ARRAY[0]}
MINOR=${VERSION_ARRAY[1]}
PATCH=${VERSION_ARRAY[2]}
(( PATCH++ ))
NEXT_TAG=v$MAJOR.$MINOR.$PATCH
echo "NEXT_TAG=$NEXT_TAG" >> $GITHUB_OUTPUT
fi
- name: Generate changelog
id: generate_changelog
run: |
LATEST_RELEASE=${{ steps.get_latest_release.outputs.LATEST_RELEASE }}
if [[ -z "$LATEST_RELEASE" ]]; then
# If no release, start from the first commit
CHANGELOG=$(git log --pretty=format:"* %s (%an, %ar)" --reverse)
else
# Get all submission information since the last release
CHANGELOG=$(git log "$LATEST_RELEASE"..HEAD --pretty=format:"* %s (%an, %ar)")
fi
echo "CHANGELOG=$CHANGELOG" >> $GITHUB_OUTPUT
- name: Create release
if: ${{ steps.determine_next_version.outputs.NEXT_TAG }}
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ steps.determine_next_version.outputs.NEXT_TAG }}
body: |
# Changelog
${{ steps.generate_changelog.outputs.CHANGELOG }}
draft: false
prerelease: false