优化测试版本命名格式:使用SHA前8个字符作为版本名称 #3
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: 自动Release更新流程 | ||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| - test-workflow | ||
| tags: | ||
| - 'v*.*.*' | ||
| pull_request: | ||
| branches: | ||
| - main | ||
| permissions: | ||
| # 配置必要的权限 | ||
| contents: write # 允许创建release、tag和提交更改 | ||
| pull-requests: write # 允许操作PR | ||
| jobs: | ||
| # 更新Release作业 | ||
| update-release: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: 检出代码 | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
| - name: 获取提交日志(用于更新内容) | ||
| id: get-changelog | ||
| run: | | ||
| # 获取提交日志作为更新内容 | ||
| if [ "${{ github.event_name }}" == "pull_request" ]; then | ||
| # PR事件:获取PR相关的提交日志 | ||
| PR_NUMBER=${{ github.event.pull_request.number }} | ||
| CHANGELOG=$(git log --pretty=format:"- %s" ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }}) | ||
| elif [[ "${{ github.ref }}" == refs/tags/* ]]; then | ||
| # 标签事件:获取当前标签与上一个标签之间的提交日志 | ||
| PREVIOUS_TAG=$(git describe --abbrev=0 --tags $(git rev-list --tags --skip=1 --max-count=1)) | ||
| CHANGELOG=$(git log --pretty=format:"- %s" $PREVIOUS_TAG..${{ github.ref_name }}) | ||
| else | ||
| # 分支推送事件:获取当前分支的最新提交日志 | ||
| CHANGELOG=$(git log --pretty=format:"- %s" -n 10) | ||
| fi | ||
| # 保存变更日志到环境变量 | ||
| echo "CHANGELOG<<EOF" >> $GITHUB_ENV | ||
| echo "$CHANGELOG" >> $GITHUB_ENV | ||
| echo "EOF" >> $GITHUB_ENV | ||
| # 为PR创建预发布版本 | ||
| - name: 为PR创建预发布版本 | ||
| if: github.event_name == 'pull_request' | ||
| uses: softprops/action-gh-release@v1 | ||
| with: | ||
| tag_name: pr-${{ github.event.pull_request.number }}-${{ github.sha }} | ||
| name: PR ${{ github.event.pull_request.number }} 预发布 | ||
| draft: false | ||
| prerelease: true | ||
| body: | | ||
| # PR ${{ github.event.pull_request.number }} 预发布版本 | ||
| - 基于提交:${{ github.sha }} | ||
| - 分支:${{ github.head_ref }} | ||
| ## 更新内容 | ||
| ${{ env.CHANGELOG }} | ||
| # 为main分支创建更新版本 | ||
| - name: 为main分支创建更新版本 | ||
| if: github.ref == 'refs/heads/main' && github.event_name == 'push' | ||
| uses: softprops/action-gh-release@v1 | ||
| with: | ||
| tag_name: update-${{ github.sha }} | ||
| name: 更新版本 ${{ github.sha }} | ||
| draft: false | ||
| prerelease: true | ||
| body: | | ||
| # main分支更新版本 | ||
| - 基于提交:${{ github.sha }} | ||
| - 提交者:${{ github.event.head_commit.author.name }} | ||
| - 提交时间:${{ github.event.head_commit.timestamp }} | ||
| ## 更新内容 | ||
| ${{ env.CHANGELOG }} | ||
| # 为test-workflow分支创建测试版本 | ||
| - name: 为test-workflow分支创建测试版本 | ||
| if: github.ref == 'refs/heads/test-workflow' && github.event_name == 'push' | ||
| uses: softprops/action-gh-release@v1 | ||
| with: | ||
| tag_name: test-${{ github.sha }} # 使用完整SHA作为标签确保唯一性 | ||
| name: 测试版本 ${{ substr(github.sha, 0, 8) }} # 使用SHA前缀提高可读性 | ||
| draft: false | ||
| prerelease: true | ||
| body: | | ||
| # test-workflow分支测试版本 | ||
| - 基于提交:${{ github.sha }} | ||
| - 提交者:${{ github.event.head_commit.author.name }} | ||
| - 提交时间:${{ github.event.head_commit.timestamp }} | ||
| ## 更新内容 | ||
| ${{ env.CHANGELOG }} | ||
| # 为标签创建正式发布版本 | ||
| - name: 为标签创建正式发布 | ||
| if: startsWith(github.ref, 'refs/tags/v') | ||
| uses: softprops/action-gh-release@v1 | ||
| with: | ||
| name: 版本 ${{ github.ref_name }} | ||
| draft: false | ||
| prerelease: false | ||
| body: | | ||
| # 版本 ${{ github.ref_name }} 发布说明 | ||
| - 基于标签:${{ github.ref_name }} | ||
| - 提交:${{ github.sha }} | ||
| - 发布时间:${{ github.event.head_commit.timestamp }} | ||
| ## 更新内容 | ||
| ${{ env.CHANGELOG }} | ||
| # 自动更新版本号作业 | ||
| bump-version: | ||
| needs: update-release | ||
| runs-on: ubuntu-latest | ||
| # 在main分支和test-workflow分支的push事件时都运行 | ||
| if: (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/test-workflow') && github.event_name == 'push' | ||
| steps: | ||
| - name: 检出代码 | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
| # 使用token以允许提交回仓库 | ||
| token: ${{ secrets.GITHUB_TOKEN }} | ||
| - name: 设置Git配置 | ||
| run: | | ||
| git config user.name "GitHub Actions" | ||
| git config user.email "actions@github.com" | ||
| - name: 增加版本号(补丁版本) | ||
| run: | | ||
| # 检查composer.json是否存在 | ||
| if [ -f "composer.json" ]; then | ||
| # 读取当前版本号 | ||
| CURRENT_VERSION=$(grep -oP '(?<="version": ")[0-9]+\.[0-9]+\.[0-9]+' composer.json) | ||
| if [ -n "$CURRENT_VERSION" ]; then | ||
| # 解析版本号 | ||
| IFS='.' read -r -a VERSION_PARTS <<< "$CURRENT_VERSION" | ||
| # 增加补丁版本号 | ||
| PATCH_VERSION=$((VERSION_PARTS[2] + 1)) | ||
| NEW_VERSION="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.$PATCH_VERSION" | ||
| echo "当前版本: $CURRENT_VERSION -> 新版本: $NEW_VERSION" | ||
| # 更新composer.json中的版本号 | ||
| sed -i "s/\"version\": \"$CURRENT_VERSION\"/\"version\": \"$NEW_VERSION\"/" composer.json | ||
| # 根据不同分支执行不同操作 | ||
| if [ "${{ github.ref }}" == "refs/heads/main" ]; then | ||
| # main分支:标准版本更新流程 | ||
| git add composer.json | ||
| git commit -m "自动更新版本号至 $NEW_VERSION" | ||
| # 测试环境下不推送到main分支 | ||
| # git push origin main | ||
| # 测试环境下不创建正式标签 | ||
| # git tag -a v$NEW_VERSION -m "版本 $NEW_VERSION" | ||
| # git push origin v$NEW_VERSION | ||
| else | ||
| # test-workflow分支:测试版本更新流程 | ||
| git add composer.json | ||
| git commit -m "测试:自动更新版本号至 $NEW_VERSION" | ||
| # 测试分支不推送更新 | ||
| # git push origin test-workflow | ||
| # 测试分支创建测试标签但不推送 | ||
| # git tag -a test-v$NEW_VERSION -m "测试版本 $NEW_VERSION" | ||
| # git push origin test-v$NEW_VERSION | ||
| fi | ||
| fi | ||
| fi | ||