|
| 1 | +name: Auto Tag |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + |
| 7 | +jobs: |
| 8 | + auto-tag: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + if: "!startsWith(github.event.head_commit.message, 'chore(release)') && !startsWith(github.event.head_commit.message, 'chore(homebrew)')" |
| 11 | + steps: |
| 12 | + - uses: actions/checkout@v4 |
| 13 | + with: |
| 14 | + fetch-depth: 0 |
| 15 | + token: ${{ secrets.PAT }} |
| 16 | + |
| 17 | + - name: Calculate next version |
| 18 | + id: version |
| 19 | + run: | |
| 20 | + LATEST_TAG=$(git tag --sort=-v:refname | grep '^v' | head -1) |
| 21 | + [ -z "$LATEST_TAG" ] && LATEST_TAG="v0.0.0" |
| 22 | + echo "latest=$LATEST_TAG" |
| 23 | +
|
| 24 | + VERSION="${LATEST_TAG#v}" |
| 25 | + IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION" |
| 26 | +
|
| 27 | + COMMITS=$(git log "${LATEST_TAG}..HEAD" --pretty=format:"%s" 2>/dev/null) |
| 28 | + if [ -z "$COMMITS" ]; then |
| 29 | + echo "No commits since $LATEST_TAG" |
| 30 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 31 | + exit 0 |
| 32 | + fi |
| 33 | +
|
| 34 | + BUMP="none" |
| 35 | + while IFS= read -r msg; do |
| 36 | + if echo "$msg" | grep -qiE "^[a-z]+(\(.+\))?!:|BREAKING CHANGE"; then |
| 37 | + BUMP="major" |
| 38 | + break |
| 39 | + elif echo "$msg" | grep -qE "^feat(\(.+\))?:"; then |
| 40 | + [ "$BUMP" != "major" ] && BUMP="minor" |
| 41 | + elif echo "$msg" | grep -qE "^(fix|perf|refactor|build)(\(.+\))?:"; then |
| 42 | + [ "$BUMP" = "none" ] && BUMP="patch" |
| 43 | + fi |
| 44 | + done <<< "$COMMITS" |
| 45 | +
|
| 46 | + if [ "$BUMP" = "none" ]; then |
| 47 | + echo "No releasable commits found" |
| 48 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 49 | + exit 0 |
| 50 | + fi |
| 51 | +
|
| 52 | + case "$BUMP" in |
| 53 | + major) MAJOR=$((MAJOR + 1)); MINOR=0; PATCH=0 ;; |
| 54 | + minor) MINOR=$((MINOR + 1)); PATCH=0 ;; |
| 55 | + patch) PATCH=$((PATCH + 1)) ;; |
| 56 | + esac |
| 57 | +
|
| 58 | + NEXT="${MAJOR}.${MINOR}.${PATCH}" |
| 59 | + { |
| 60 | + echo "next=$NEXT" |
| 61 | + echo "tag=v$NEXT" |
| 62 | + echo "skip=false" |
| 63 | + } >> "$GITHUB_OUTPUT" |
| 64 | + echo "Bump: $BUMP ($LATEST_TAG -> v$NEXT)" |
| 65 | +
|
| 66 | + - name: Create and push tag |
| 67 | + if: steps.version.outputs.skip != 'true' |
| 68 | + run: | |
| 69 | + git config user.name "github-actions[bot]" |
| 70 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 71 | + git tag -a "${{ steps.version.outputs.tag }}" -m "${{ steps.version.outputs.tag }}" |
| 72 | + git push origin "${{ steps.version.outputs.tag }}" |
0 commit comments