@@ -25,28 +25,119 @@ env:
2525 TZ : Asia/Shanghai
2626
2727jobs :
28+ job_auto_tag :
29+ runs-on : ubuntu-latest
30+ # 只在 main 分支推送且包含 version.mk 文件变更时运行
31+ if : github.ref == 'refs/heads/main' && github.event_name == 'push'
32+ outputs :
33+ new_tag : ${{ steps.create_tag.outputs.tag_name }}
34+ tag_created : ${{ steps.create_tag.outputs.tag_created }}
35+
36+ steps :
37+ - name : Checkout code
38+ uses : actions/checkout@v4
39+ with :
40+ fetch-depth : 0
41+ token : ${{ secrets.GITHUB_TOKEN }}
42+
43+ - name : Check if version.mk changed
44+ id : check_version_change
45+ run : |
46+ # 检查version.mk是否在这次提交中被修改
47+ if git diff --name-only HEAD~1 HEAD | grep -q "^version\.mk$"; then
48+ echo "version.mk was modified in this commit"
49+ echo "version_changed=true" >> $GITHUB_OUTPUT
50+ else
51+ echo "version.mk was not modified in this commit"
52+ echo "version_changed=false" >> $GITHUB_OUTPUT
53+ fi
54+
55+ - name : Extract version from version.mk
56+ id : extract_version
57+ if : steps.check_version_change.outputs.version_changed == 'true'
58+ run : |
59+ # 从version.mk文件中提取QMODEM_VERSION
60+ version=$(grep "^QMODEM_VERSION" version.mk | sed 's/QMODEM_VERSION:=//' | xargs)
61+ echo "Current QMODEM_VERSION: $version"
62+ echo "version=$version" >> $GITHUB_OUTPUT
63+
64+ # 检查version是否为空
65+ if [ -z "$version" ]; then
66+ echo "Error: Could not extract version from version.mk"
67+ exit 1
68+ fi
69+
70+ # 生成tag名称
71+ tag_name="v$version"
72+ echo "tag_name=$tag_name" >> $GITHUB_OUTPUT
73+ echo "Generated tag name: $tag_name"
74+
75+ - name : Check if tag already exists
76+ id : check_tag
77+ if : steps.check_version_change.outputs.version_changed == 'true'
78+ run : |
79+ tag_name="${{ steps.extract_version.outputs.tag_name }}"
80+
81+ # 检查tag是否已经存在
82+ if git tag -l | grep -q "^${tag_name}$"; then
83+ echo "Tag $tag_name already exists, skipping tag creation"
84+ echo "tag_exists=true" >> $GITHUB_OUTPUT
85+ else
86+ echo "Tag $tag_name does not exist, will create it"
87+ echo "tag_exists=false" >> $GITHUB_OUTPUT
88+ fi
89+
90+ - name : Create and push tag
91+ id : create_tag
92+ if : steps.check_version_change.outputs.version_changed == 'true' && steps.check_tag.outputs.tag_exists == 'false'
93+ run : |
94+ tag_name="${{ steps.extract_version.outputs.tag_name }}"
95+ version="${{ steps.extract_version.outputs.version }}"
96+
97+ # 配置git用户信息
98+ git config user.name "github-actions[bot]"
99+ git config user.email "github-actions[bot]@users.noreply.github.com"
100+
101+ # 创建带注释的tag
102+ git tag -a "$tag_name" -m "Release version $version - Auto-generated tag when QMODEM_VERSION was updated to $version"
103+
104+ # 推送tag到远程仓库
105+ git push origin "$tag_name"
106+
107+ echo "✅ Successfully created and pushed tag: $tag_name"
108+ echo "tag_name=$tag_name" >> $GITHUB_OUTPUT
109+ echo "tag_created=true" >> $GITHUB_OUTPUT
110+
28111 job_prepare :
29112 runs-on : ubuntu-latest
113+ needs : [job_auto_tag]
114+ if : always() # 总是运行,不管 auto_tag 是否运行或是否成功
30115 outputs :
31- branch_name : ${{ env .branch_name }}
32- tag_name : ${{ env .tag_name }}
33- push_type : ${{ env .push_type }}
34- is_pr : ${{ env .is_pr }}
116+ branch_name : ${{ steps.determine_push.outputs .branch_name }}
117+ tag_name : ${{ steps.determine_push.outputs .tag_name }}
118+ push_type : ${{ steps.determine_push.outputs .push_type }}
119+ is_pr : ${{ steps.determine_push.outputs .is_pr }}
35120
36121 steps :
37122 - name : Determine push type
123+ id : determine_push
38124 run : |
39- if [[ "${{ github.ref }}" == refs/tags/* ]]; then
125+ # 如果 auto_tag job 创建了新标签,使用新标签
126+ if [[ "${{ needs.job_auto_tag.outputs.tag_created }}" == "true" ]]; then
127+ echo "New tag created: ${{ needs.job_auto_tag.outputs.new_tag }}"
128+ echo "tag_name=${{ needs.job_auto_tag.outputs.new_tag }}" >> $GITHUB_OUTPUT
129+ echo "push_type=tag" >> $GITHUB_OUTPUT
130+ elif [[ "${{ github.ref }}" == refs/tags/* ]]; then
40131 echo "This is a tag push."
41- echo "tag_name=${{ github.ref_name }}" >> $GITHUB_ENV
42- echo "push_type=tag" >> $GITHUB_ENV
132+ echo "tag_name=${{ github.ref_name }}" >> $GITHUB_OUTPUT
133+ echo "push_type=tag" >> $GITHUB_OUTPUT
43134 else
44135 echo "This is a branch push."
45- echo "branch_name=${{ github.ref_name }}" >> $GITHUB_ENV
46- echo "push_type=branch" >> $GITHUB_ENV
136+ echo "branch_name=${{ github.ref_name }}" >> $GITHUB_OUTPUT
137+ echo "push_type=branch" >> $GITHUB_OUTPUT
47138 fi
48139 is_pr=$([[ "${{ github.event_name }}" == "pull_request" ]] && echo 1 || echo 0)
49- echo "is_pr=${is_pr}" >> $GITHUB_ENV
140+ echo "is_pr=${is_pr}" >> $GITHUB_OUTPUT
50141
51142 - name : Checkout
52143 if :
70161
71162 job_build_qmodem :
72163 name : Build QModem
73- needs : job_prepare
164+ needs : [ job_prepare]
74165 continue-on-error : true
75166 strategy :
76167 matrix :
0 commit comments