-
Notifications
You must be signed in to change notification settings - Fork 2
116 lines (101 loc) · 4.65 KB
/
Copy pathrelease.yml
File metadata and controls
116 lines (101 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
name: Prepare and Publish Release
on:
push:
branches:
- main
jobs:
release:
if: github.actor != 'dependabot[bot]' && github.actor != 'dependabot-preview[bot]'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # full history
- name: Install GH CLI
run: |
sudo apt update && sudo apt install curl -y
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list
sudo apt update && sudo apt install gh -y
gh auth login --with-token <<< ${{ secrets.DEMO_GITHUB_TOKEN }}
- name: Generate Release and Publish (latest tag by time, auto notes w/ SemVer previous)
env:
GH_TOKEN: ${{ secrets.DEMO_GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.DEMO_GITHUB_TOKEN }}
run: |
set -euo pipefail
# Ensure tags are present locally
git fetch --tags --force
# Get newest tag by creation time (not ancestry)
mapfile -t TAGS < <(git for-each-ref --sort=-creatordate --format '%(refname:short)' refs/tags)
RAW_LAST_TAG="${TAGS[0]:-}"
if [ -z "$RAW_LAST_TAG" ]; then
# No tags at all yet: start from v1.0.0 baseline
LAST_TAG_NORM="v1.0.0"
else
# Normalize newest tag to vX.Y.Z for version parsing
case "$RAW_LAST_TAG" in v*) LAST_TAG_NORM="$RAW_LAST_TAG" ;; *) LAST_TAG_NORM="v$RAW_LAST_TAG" ;; esac
fi
echo "Newest tag by time: ${RAW_LAST_TAG:-<none>}"
# Parse current version numbers from LAST_TAG_NORM (vX.Y.Z)
CUR_MAJ="$(echo "$LAST_TAG_NORM" | sed -n 's/^v\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\)$/\1/p')"
CUR_MIN="$(echo "$LAST_TAG_NORM" | sed -n 's/^v\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\)$/\2/p')"
CUR_PAT="$(echo "$LAST_TAG_NORM" | sed -n 's/^v\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\)$/\3/p')"
if [ -z "$CUR_MAJ" ] || [ -z "$CUR_MIN" ] || [ -z "$CUR_PAT" ]; then
echo "Last tag not in vX.Y.Z format. Aborting."
exit 1
fi
NEW_MAJ="$CUR_MAJ"; NEW_MIN="$CUR_MIN"; NEW_PAT="$CUR_PAT"
# Determine bump from merged PR title for this push (if any)
PR_TITLE="$(gh api -H "Accept: application/vnd.github+json" \
"repos/${{ github.repository }}/commits/${{ github.sha }}/pulls" \
--jq '.[0].title' || true)"
echo "PR title: ${PR_TITLE:-<none>}"
BUMP="patch"
case "${PR_TITLE:-}" in
feat!:*) BUMP="major" ;;
feat:*) BUMP="minor" ;;
fix:*|refactor:*|chore:*|docs:*|test:*) BUMP="patch" ;;
*) BUMP="patch" ;;
esac
echo "Bump type: $BUMP"
if [ "$BUMP" = "major" ]; then
NEW_MAJ=$((CUR_MAJ + 1)); NEW_MIN=0; NEW_PAT=0
elif [ "$BUMP" = "minor" ]; then
NEW_MIN=$((CUR_MIN + 1)); NEW_PAT=0
else
NEW_PAT=$((CUR_PAT + 1))
fi
TAG="v${NEW_MAJ}.${NEW_MIN}.${NEW_PAT}"
# Ensure tag uniqueness
while git show-ref --tags --verify --quiet "refs/tags/$TAG"; do
echo "Tag $TAG exists, bumping patch…"
NEW_PAT=$((NEW_PAT + 1))
TAG="v${NEW_MAJ}.${NEW_MIN}.${NEW_PAT}"
done
echo "New tag: $TAG"
# --- NEW: find the immediate previous SemVer tag (greatest version < TAG) ---
NEW_SEMVER="${TAG#v}"
PREV_TAG="$(
git tag --list \
| sed 's/^v//' \
| grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' \
| sort -V \
| awk -v t="$NEW_SEMVER" '$0 < t { last=$0 } END { if (last != "") print "v" last }'
)"
echo "Previous SemVer tag: ${PREV_TAG:-<none>}"
# Create and push the tag
git tag "$TAG"
git push origin "$TAG"
# Create the release with GitHub's auto-generated notes between PREV_TAG and TAG
if [ -n "${PREV_TAG:-}" ]; then
gh release create "$TAG" \
--title "Release ${NEW_MAJ}.${NEW_MIN}.${NEW_PAT}" \
--generate-notes \
--notes-start-tag "$PREV_TAG"
else
# First release ever (or no SemVer tag below new tag)
gh release create "$TAG" \
--title "Release ${NEW_MAJ}.${NEW_MIN}.${NEW_PAT}" \
--generate-notes
fi