feat/test3 #67
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: Bump Version and Create Release | |
| on: | |
| pull_request: | |
| types: [closed] | |
| branches: | |
| - main | |
| - development | |
| jobs: | |
| bump-version: | |
| name: Bump Version and Create Release | |
| runs-on: ubuntu-latest | |
| if: github.event.pull_request.merged == true | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| steps: | |
| # 1. สร้าง Token ชั่วคราวจาก GitHub App | |
| - name: Generate GitHub App Token | |
| id: app-token | |
| uses: actions/create-github-app-token@v1 | |
| with: | |
| app-id: ${{ secrets.APP_ID }} | |
| private-key: ${{ secrets.APP_PRIVATE_KEY }} | |
| # 2. Checkout โดยใช้ Token ของ App | |
| - name: Checkout Repository | |
| uses: actions/checkout@v4 | |
| with: | |
| token: ${{ steps.app-token.outputs.token }} # ใช้ Token ที่เพิ่งสร้าง | |
| fetch-depth: 0 | |
| # 3. Config Git ให้เป็นตัวตนของ App (Bot Identity) | |
| - name: Configure Git Identity | |
| env: | |
| GH_TOKEN: ${{ steps.app-token.outputs.token }} | |
| run: | | |
| # ยิง API เพื่อขอข้อมูลของ App Bot ตัวเอง | |
| # GitHub App จะมี user type เป็น "Bot" และมี login name เฉพาะ | |
| USER_INFO=$(curl -s -H "Authorization: token $GH_TOKEN" https://api.github.com/user) | |
| USER_LOGIN=$(echo "$USER_INFO" | jq -r .login) | |
| USER_ID=$(echo "$USER_INFO" | jq -r .id) | |
| # Email ของ GitHub App Bot จะมี format มาตรฐานคือ: id+name[bot]@users.noreply.github.com | |
| USER_EMAIL="${USER_ID}+${USER_LOGIN}@users.noreply.github.com" | |
| echo "Configuring Git as: $USER_LOGIN <$USER_EMAIL>" | |
| git config user.name "$USER_LOGIN" | |
| git config user.email "$USER_EMAIL" | |
| - name: Get Current Version | |
| id: current_version | |
| run: | | |
| # get only no -dev | |
| LATEST_TAG=$(git tag -l 'v*' | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n1) | |
| LATEST_TAG_DAV=$(git tag -l 'v*' | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+-dev\.[0-9]+' | sort -V | tail -n1) | |
| LATEST_TAG_DAV_REMOVED=${LATEST_TAG_DAV%%-*} | |
| same_tag=true | |
| if [[ "$LATEST_TAG_DAV_REMOVED" != "$LATEST_TAG" ]]; then | |
| same_tag=false | |
| fi | |
| echo "same_tag=$same_tag" >> $GITHUB_OUTPUT | |
| if [ -z "$LATEST_TAG" ]; then | |
| echo "No tags found. Defaulting to v0.0.0" | |
| CURRENT_VERSION="0.0.0" | |
| else | |
| echo "Found latest tag: $LATEST_TAG" | |
| if: [ same_tag == "true" ] | |
| CURRENT_VERSION="${LATEST_TAG_DAV#v}" | |
| else | |
| CURRENT_VERSION="${LATEST_TAG#v}" | |
| fi | |
| fi | |
| echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT | |
| - name: Calculate New Version | |
| id: new_version | |
| env: | |
| CURRENT_VERSION: ${{ steps.current_version.outputs.version }} | |
| TARGET_BRANCH: ${{ github.event.pull_request.base.ref }} | |
| SOURCE_BRANCH: ${{ github.event.pull_request.head.ref }} | |
| SAME_TAG: ${{ steps.current_version.outputs.same_tag }} | |
| run: | | |
| echo "Target: $TARGET_BRANCH | Source: $SOURCE_BRANCH | Current: $CURRENT_VERSION" | |
| BASE_VERSION=${CURRENT_VERSION%%-*} | |
| IFS='.' read -r MAJOR MINOR PATCH <<< "$BASE_VERSION" | |
| NEW_VERSION="" | |
| # === MAIN === | |
| if [ "$TARGET_BRANCH" == "main" ]; then | |
| if [ "$SOURCE_BRANCH" == "development" ]; then | |
| NEW_VERSION="$MAJOR.$MINOR.0" | |
| echo "Strategy: Minor bump (Release)" | |
| else | |
| NEW_PATCH=$((PATCH + 1)) | |
| NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH" | |
| echo "Strategy: Patch bump (Hotfix)" | |
| fi | |
| # === DEVELOPMENT === | |
| elif [ "$TARGET_BRANCH" == "development" ]; then | |
| if [[ "$CURRENT_VERSION" == *"-dev."* && "$SAME_TAG" == "true" ]]; then | |
| NEW_VERSION="$MAJOR.$((MINOR + 1)).0-dev.1" | |
| else if [[ "$CURRENT_VERSION" == *"-dev."* || "$SOURCE_BRANCH" == "main"]] | |
| LAST_NUM=${CURRENT_VERSION##*.} | |
| NEXT_NUM=$((LAST_NUM + 1)) | |
| NEW_VERSION="${BASE_VERSION}-dev.${NEXT_NUM}" | |
| else | |
| echo "Strategy: Dev increment" | |
| fi | |
| echo "Strategy: Dev increment" | |
| else | |
| echo "Error: Unknown target branch '$TARGET_BRANCH'" | |
| exit 1 | |
| fi | |
| echo "New Version Computed: $NEW_VERSION" | |
| echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT | |
| - name: Push New Tag | |
| run: | | |
| NEW_VERSION=${{ steps.new_version.outputs.version }} | |
| TAG_NAME="v$NEW_VERSION" | |
| npm version "$NEW_VERSION" | |
| git push --follow-tags | |
| # - name: Push New Tag | |
| # run: | | |
| # NEW_VERSION=${{ steps.new_version.outputs.version }} | |
| # TAG_NAME="v$NEW_VERSION" | |
| # echo "Pushing tag $TAG_NAME" | |
| # git tag -a "$TAG_NAME" -m "Release $TAG_NAME" | |
| # # Token ถูก set ตั้งแต่ checkout แล้ว จึง push ได้เลย | |
| # git push origin "$TAG_NAME" | |
| - name: Create GitHub Release | |
| uses: softprops/action-gh-release@v2 | |
| if: github.event.pull_request.base.ref == 'main' | |
| with: | |
| token: ${{ steps.app-token.outputs.token }} # ใช้ App Token สร้าง Release | |
| tag_name: v${{ steps.new_version.outputs.version }} | |
| name: Release ${{ steps.new_version.outputs.version }} | |
| generate_release_notes: true | |
| body: | | |
| ## Release Details | |
| - **Version:** ${{ steps.new_version.outputs.version }} | |
| - **Previous:** ${{ steps.current_version.outputs.version }} | |
| - **PR:** #${{ github.event.pull_request.number }} | |
| - **Branch:** ${{ github.event.pull_request.head.ref }} → ${{ github.event.pull_request.base.ref }} | |
| - **Merged by:** @${{ github.event.pull_request.merged_by.login }} |