1+ name : Universal Auto Build and Publish
2+
3+ on :
4+ push :
5+ branches :
6+ - ' **'
7+
8+ # 🌟 全域設定:強制使用 Node.js 24 以消除棄用警告
9+ env :
10+ FORCE_JAVASCRIPT_ACTIONS_TO_NODE24 : true
11+
12+ jobs :
13+ prepare-info :
14+ runs-on : ubuntu-latest
15+ outputs :
16+ branch : ${{ steps.vars.outputs.branch }}
17+ project_name : ${{ steps.vars.outputs.project_name }}
18+ version : ${{ steps.vars.outputs.version }}
19+ should_publish : ${{ steps.vars.outputs.should_publish }}
20+ steps :
21+ - name : Checkout code
22+ uses : actions/checkout@v4
23+
24+ - name : Extract Project Info
25+ id : vars
26+ run : |
27+ BRANCH_NAME="${GITHUB_REF#refs/heads/}"
28+ echo "branch=$BRANCH_NAME" >> $GITHUB_OUTPUT
29+
30+ P_NAME=$(grep "rootProject.name" settings.gradle | cut -d"'" -f2 | cut -d'"' -f2 || echo "Project")
31+ echo "project_name=$P_NAME" >> $GITHUB_OUTPUT
32+
33+ COMMIT_MSG="${{ github.event.head_commit.message }}"
34+ REGEX="update ([0-9a-zA-Z.-]+)"
35+
36+ if [[ "$COMMIT_MSG" =~ $REGEX ]]; then
37+ echo "version=${BASH_REMATCH[1]}" >> $GITHUB_OUTPUT
38+ echo "should_publish=true" >> $GITHUB_OUTPUT
39+ else
40+ echo "should_publish=false" >> $GITHUB_OUTPUT
41+ fi
42+
43+ build-and-artifact :
44+ needs : prepare-info
45+ runs-on : ubuntu-latest
46+ steps :
47+ - name : Checkout code
48+ uses : actions/checkout@v4
49+ with :
50+ fetch-depth : 0
51+
52+ - name : Set up JDK
53+ uses : actions/setup-java@v4
54+ with :
55+ java-version : ' 25'
56+ distribution : ' temurin'
57+
58+ - name : Setup Gradle
59+ uses : gradle/actions/setup-gradle@v3
60+ with :
61+ # 🌟 如果快取一直報錯且讓你很煩,可以暫時加上這行關閉快取寫入
62+ # cache-read-only: false
63+ gradle-version : wrapper
64+
65+ - name : Build Project
66+ run : |
67+ chmod +x ./gradlew
68+ ./gradlew build
69+
70+ - name : Upload Build Artifact
71+ uses : actions/upload-artifact@v4
72+ with :
73+ name : ${{ needs.prepare-info.outputs.project_name }}-${{ needs.prepare-info.outputs.branch }}
74+ path : build/libs/*.jar
75+ retention-days : 7
76+
77+ publish-release :
78+ needs : [prepare-info, build-and-artifact]
79+ if : |
80+ needs.prepare-info.outputs.should_publish == 'true' &&
81+ needs.prepare-info.outputs.branch == 'main'
82+ runs-on : ubuntu-latest
83+ permissions :
84+ contents : write
85+
86+ steps :
87+ - name : Checkout code
88+ uses : actions/checkout@v4
89+ with :
90+ fetch-depth : 0
91+
92+ - name : Download Artifact
93+ uses : actions/download-artifact@v4
94+ with :
95+ name : ${{ needs.prepare-info.outputs.project_name }}-${{ needs.prepare-info.outputs.branch }}
96+ path : build/libs/
97+
98+ - name : Generate Changelog
99+ id : changelog
100+ run : |
101+ PREVIOUS_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
102+ if [ -z "$PREVIOUS_TAG" ]; then
103+ COMMITS=$(git log --pretty=format:"* %s (%h)")
104+ else
105+ COMMITS=$(git log ${PREVIOUS_TAG}..HEAD --pretty=format:"* %s (%h)")
106+ fi
107+
108+ EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
109+ echo "notes<<$EOF" >> $GITHUB_OUTPUT
110+ echo "$COMMITS" >> $GITHUB_OUTPUT
111+ echo "$EOF" >> $GITHUB_OUTPUT
112+
113+ - name : Create GitHub Release
114+ uses : softprops/action-gh-release@v2
115+ with :
116+ tag_name : v${{ needs.prepare-info.outputs.version }}
117+ name : " ${{ needs.prepare-info.outputs.project_name }} v${{ needs.prepare-info.outputs.version }}"
118+ body : |
119+ ## 🚀 Automated Release (Main Branch)
120+
121+ ### 📝 更新日誌 (Changelog)
122+ ${{ steps.changelog.outputs.notes }}
123+ files : build/libs/*.jar
0 commit comments