@@ -2,7 +2,7 @@ name: Bump Version and Create Release
22
33on :
44 pull_request :
5- types : closed
5+ types : [ closed]
66 branches :
77 - main
88 - development
@@ -19,155 +19,137 @@ jobs:
1919 steps :
2020 - name : Checkout Repository
2121 uses : actions/checkout@v4
22- env :
23- GITHUB_TOKEN : ${{ secrets.PAT }}
2422 with :
23+ token : ${{ secrets.PAT }} # ใช้ PAT ตรงนี้เพื่อให้มีสิทธิ์ Push ตลอดทั้ง Job
2524 fetch-depth : 0
2625
27- # - name: Configure Git
28- # run: |
29- # git config user.name "github-actions[bot]"
30- # git config user.email "github-actions[bot]@users.noreply.github.com"
31-
32- - name : Set up Node.js
33- uses : actions/setup-node@v4
34- with :
35- node-version : ' 22'
26+ - name : Configure Git with PAT Identity
27+ env :
28+ GITHUB_TOKEN : ${{ secrets.PAT }} # ใช้ PAT ในการยิง API เช็คตัวเอง
29+ run : |
30+ # 1. ยิง API ถาม GitHub ว่า PAT นี้เป็นของใคร
31+ USER_INFO=$(curl -s -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/user)
32+
33+ # 2. ดึง Login Name และ ID
34+ USER_LOGIN=$(echo "$USER_INFO" | jq -r .login)
35+ USER_ID=$(echo "$USER_INFO" | jq -r .id)
36+
37+ # 3. ดึง Email (ถ้าเจ้าของ PAT ซ่อน Email ไว้ ให้ใช้รูปแบบ no-reply ของ GitHub)
38+ USER_EMAIL=$(echo "$USER_INFO" | jq -r .email)
39+ if [ "$USER_EMAIL" == "null" ]; then
40+ USER_EMAIL="${USER_ID}+${USER_LOGIN}@users.noreply.github.com"
41+ fi
42+
43+ echo "Configuring Git as: $USER_LOGIN <$USER_EMAIL>"
44+
45+ # 4. Set Config
46+ git config user.name "$USER_LOGIN"
47+ git config user.email "$USER_EMAIL"
3648
37- - name : Get Current Version from GitHub Releases
49+ - name : Get Current Version
3850 id : current_version
3951 run : |
40- # Ensure we have tags locally (fetch/prune from origin)
41- git fetch --prune --tags origin
52+ # หา Tag ล่าสุดที่เป็นรูปแบบ vX.Y.Z หรือ vX.Y.Z-dev.N
53+ # ใช้ git describe เพื่อความแม่นยำ หรือ sort -V หากต้องการแค่ list
54+ LATEST_TAG=$(git tag -l 'v*' | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+' | sort -V | tail -n1)
4255
43- # Prefer latest tag (including pre-releases) matching semver prefix vMAJOR.MINOR.PATCH
44- LATEST_TAG=$(git tag -l 'v*' | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+' | sort -V | tail -n1 || true)
45-
46- # if tag latest found
47- if [ -n "$LATEST_TAG" ]; then
48- CURRENT_VERSION="${LATEST_TAG#v}"
49- echo "Found latest tag: $LATEST_TAG"
50- # if tag latest not found
56+ if [ -z "$LATEST_TAG" ]; then
57+ echo "No tags found. Defaulting to v0.0.0"
58+ CURRENT_VERSION="0.0.0"
5159 else
52- # Fallback to GitHub Releases API if no tags are present
53- LATEST_TAG=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
54- https://api.github.com/repos/${{ github.repository }}/releases/latest \
55- | jq -r '.tag_name // empty')
56-
57- # if no releases found
58- if [ -z "$LATEST_TAG" ]; then
59- CURRENT_VERSION="0.1.0"
60- echo "No tags/releases found, using initial version"
61- else
62- CURRENT_VERSION="${LATEST_TAG#v}"
63- echo "Found latest release: $LATEST_TAG"
64- fi
60+ echo "Found latest tag: $LATEST_TAG"
61+ CURRENT_VERSION="${LATEST_TAG#v}"
6562 fi
6663
6764 echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
68- echo "Current version: $CURRENT_VERSION"
6965
7066 - name : Calculate New Version
7167 id : new_version
68+ env :
69+ CURRENT_VERSION : ${{ steps.current_version.outputs.version }}
70+ TARGET_BRANCH : ${{ github.event.pull_request.base.ref }}
71+ SOURCE_BRANCH : ${{ github.event.pull_request.head.ref }}
7272 run : |
73- CURRENT=${{ steps.current_version.outputs.version }}
74- TARGET_BRANCH=${{ github.event.pull_request.base.ref }}
75- SOURCE_BRANCH=${{ github.event.pull_request.head.ref }}
73+ echo "Target: $TARGET_BRANCH | Source: $SOURCE_BRANCH | Current: $CURRENT_VERSION"
7674
77- echo "Target branch: $TARGET_BRANCH"
78- echo "Source branch: $SOURCE_BRANCH"
75+ # แยก Version เป็นส่วนๆ (Major.Minor.Patch) และส่วน Suffix (ถ้ามี)
76+ # ลบ Suffix (-dev.X) ออกก่อนเพื่อหา Base Version
77+ BASE_VERSION=${CURRENT_VERSION%%-*}
78+ IFS='.' read -r MAJOR MINOR PATCH <<< "$BASE_VERSION"
7979
80- # Split version
81- IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT"
82- # ex. 0.1.0-dev* or 0.1.0
80+ NEW_VERSION=""
8381
84- # case hotfix merge to main ex. 0.1.0 -> 0.1.1
82+ # ==========================================
83+ # LOGIC: Merge to MAIN (Production Release)
84+ # ==========================================
8585 if [ "$TARGET_BRANCH" == "main" ]; then
86- echo "On main branch"
87- # split out of text after PATCH -dev* out
88- PATCH=${PATCH%%-*}
89- # ex. 0-dev* -> 0
90- # If source is not a development/, bump patch; otherwise bump minor
91- if [[ "$SOURCE_BRANCH" != "development" ]]; then
92- echo "SOURCE_BRANCH is not development"
93- NEW_MAJOR=$MAJOR
94- NEW_MINOR=$MINOR
95- NEW_PATCH=$((PATCH + 1))
96- NEW_VERSION="$NEW_MAJOR.$NEW_MINOR.$NEW_PATCH"
97- echo "Bumping main: patch bump due to hotfix branch"
98- # if source is development/, bump minor ex. 0.1.0 -> 0.2.0
99- else
100- NEW_MAJOR=$MAJOR
86+
87+ if [ "$SOURCE_BRANCH" == "development" ]; then
88+ # Release Flow: development -> main (Minor Bump)
89+ # ex. 0.1.0 -> 0.2.0
10190 NEW_MINOR=$((MINOR + 1))
102- NEW_PATCH=0
103- NEW_VERSION="$NEW_MAJOR.$NEW_MINOR.$NEW_PATCH"
104- echo "Bumping main: minor bump"
91+ NEW_VERSION="$MAJOR.$NEW_MINOR.0"
92+ echo "Strategy: Minor bump (Release)"
93+ else
94+ # Hotfix Flow: hotfix/* -> main (Patch Bump)
95+ # ex. 0.1.0 -> 0.1.1
96+ NEW_PATCH=$((PATCH + 1))
97+ NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH"
98+ echo "Strategy: Patch bump (Hotfix)"
10599 fi
106100
101+ # ==========================================
102+ # LOGIC: Merge to DEVELOPMENT (Dev Build)
103+ # ==========================================
107104 elif [ "$TARGET_BRANCH" == "development" ]; then
108- echo "On development branch"
109- if [[ "$SOURCE_BRANCH" == feat/* ]]; then
110- echo "Bumping development: feature branch"
111- # ถ้า PATCH มี -dev.N อยู่แล้ว ให้เพิ่ม N
112- if [[ "$PATCH" == *"-dev."* ]]; then
113- echo "Feature branch on development with existing dev suffix"
114- DEV_PART=${PATCH#*-dev.}
115- DEV_NUM=${DEV_PART%%-*}
116- DEV_NUM=$((DEV_NUM + 1))
117- # เก็บ prefix ก่อน -dev.N
118- PREFIX=${PATCH%%-dev.*}
119- PATCH="$PREFIX-dev.$DEV_NUM"
120- else
121- echo "Feature branch on development without existing dev suffix"
122- # ถ้าไม่มี -dev.N ให้เริ่มที่ 1
123- PATCH="${PATCH}-dev.1"
124- fi
125-
126- NEW_MAJOR=$MAJOR
127- NEW_MINOR=$MINOR
128- NEW_PATCH=$PATCH
129- NEW_VERSION="$NEW_MAJOR.$NEW_MINOR.$NEW_PATCH"
130- echo "Base version for development branch: $NEW_MAJOR.$NEW_MINOR.$NEW_PATCH"
105+
106+ # ตรวจสอบว่าเวอร์ชันปัจจุบันเป็น dev อยู่แล้วหรือไม่
107+ if [[ "$CURRENT_VERSION" == *"-dev."* ]]; then
108+ # ถ้าเป็น dev อยู่แล้ว ให้เพิ่มเลขต่อท้าย
109+ # ex. 0.1.0-dev.1 -> 0.1.0-dev.2
110+ LAST_NUM=${CURRENT_VERSION##*.}
111+ NEXT_NUM=$((LAST_NUM + 1))
112+ NEW_VERSION="${BASE_VERSION}-dev.${NEXT_NUM}"
113+ else
114+ # ถ้ามาจาก version ปกติ ให้เริ่มนับ dev.1
115+ # ex. 0.1.0 -> 0.1.0-dev.1
116+ NEW_VERSION="${BASE_VERSION}-dev.1"
131117 fi
118+ echo "Strategy: Dev increment"
119+
132120 else
133- echo "Unknown branch: $TARGET_BRANCH"
121+ echo "Error: Unknown target branch ' $TARGET_BRANCH' "
134122 exit 1
135123 fi
136124
125+ echo "New Version Computed: $NEW_VERSION"
137126 echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
138- echo "New version: $NEW_VERSION"
139-
140127
141- - name : Push Changes and Tags
128+ - name : Push New Tag
142129 run : |
143130 NEW_VERSION=${{ steps.new_version.outputs.version }}
131+ TAG_NAME="v$NEW_VERSION"
144132
145- # ตั้งค่า Remote URL ให้แนบ PAT เข้าไปสำหรับการยืนยันตัวตน
146- git remote set-url origin https://x-access-token:${{ secrets.PAT }}@github.com/${{ github.repository }}
133+ echo "Pushing tag $TAG_NAME"
147134
148- git tag -a "v$NEW_VERSION" -m "Release v$NEW_VERSION"
149- git push origin "v$NEW_VERSION"
135+ git tag -a "$TAG_NAME" -m "Release $TAG_NAME"
136+ # ไม่ต้อง set-url เพราะใช้ token ในขั้นตอน checkout แล้ว
137+ git push origin "$TAG_NAME"
150138
151139 - name : Create GitHub Release
152140 uses : softprops/action-gh-release@v2
153141 if : github.event.pull_request.base.ref == 'main'
142+ # Release ใช้ GITHUB_TOKEN ปกติได้ หรือจะใช้ PAT ก็ได้ถ้าต้องการ trigger workflow อื่นต่อ
154143 env :
155144 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
156145 with :
157146 tag_name : v${{ steps.new_version.outputs.version }}
158- name : Release ${{ steps.new_version.outputs.version }}
147+ name : Release ${{ steps.new_version.outputs.version }}
148+ generate_release_notes : true # ให้ GitHub Gen Note อัตโนมัติจาก PR
159149 body : |
160- # Release ${{ steps.new_version.outputs.version }}
161-
162- ## Version Bump
163- - Previous Version: ${{ steps.current_version.outputs.version }}
164- - New Version: ${{ steps.new_version.outputs.version }}
165- - Target Branch: ${{ github.event.pull_request.base.ref }}
166-
167150 ## Release Details
168- - PR: #${{ github.event.pull_request.number }}
169- - Title: ${{ github.event.pull_request.title }}
170- - Merged by: @${{ github.event.pull_request.merged_by.login }}
171- - Build: [View Workflow](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
172- draft : false
173-
151+ - **Version:** ${{ steps.new_version.outputs.version }}
152+ - **Previous:** ${{ steps.current_version.outputs.version }}
153+ - **PR:** #${{ github.event.pull_request.number }}
154+ - **Branch:** ${{ github.event.pull_request.head.ref }} → ${{ github.event.pull_request.base.ref }}
155+ - **Merged by:** @${{ github.event.pull_request.merged_by.login }}
0 commit comments