name: 自动版本发布 on: push: branches: - main env: PROJECT_NAME: "私有仓库Action测试" DEVELOP_TOOLS: "Keil-V5.35.0.0" COMPILER: "V5.06 update 7(build960)" jobs: AutoRelease: # 如果有Release标签则运行jobs if: "contains(github.event.head_commit.message, 'Release')" # 工作在ubuntu-latest系统中 runs-on: ubuntu-latest # 以下是steps steps: - name: 检出仓库 uses: https://git.linuxacme.com/iorebuild/checkout@v4 with: fetch-depth: 0 # 启用子模块支持 submodules: false - name: 设置环境变量 id: ActionEnv run: | #设置上海时间 sudo ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime # 正确的变量赋值 UserName="${{ github.actor }}" UserEmail=$(git log -1 --pretty=format:'%ae') GitRepo="${{ github.repository }}" ServerUrl="${{ github.server_url }}" RepoUrl="${{ github.repository }}" # 将变量设置为步骤输出 echo "ENV_USER_NAME=$UserName" >> $GITHUB_OUTPUT echo "ENV_USER_EMAIL=$UserEmail" >> $GITHUB_OUTPUT echo "ENV_REPO_NAME=$GitRepo" >> $GITHUB_OUTPUT echo "ENV_SERVER_URL=$ServerUrl" >> $GITHUB_OUTPUT echo "ENV_REPO_URL=$ServerUrl/$RepoUrl" >> $GITHUB_OUTPUT - name: 检查环境变量 run: | echo "用户名 = ${{ steps.ActionEnv.outputs.ENV_USER_NAME }}" echo "邮箱= ${{ steps.ActionEnv.outputs.ENV_USER_EMAIL }}" echo "仓库名 = ${{ steps.ActionEnv.outputs.ENV_REPO_NAME }}" echo "服务器URL = ${{ steps.ActionEnv.outputs.ENV_SERVER_URL }}" echo "仓库URL = ${{ steps.ActionEnv.outputs.ENV_REPO_URL }}" - name: 配置Git用户 run: | git config user.name "${{ steps.ActionEnv.outputs.ENV_USER_NAME }}" git config user.email "${{ steps.ActionEnv.outputs.ENV_USER_EMAIL }}" - name: 替换软件源为国内软件源(USTC) run: | cat /etc/os-release sudo rm -f /etc/apt/sources.list sudo rm -f /etc/apt/sources.list.d/* sudo tee /etc/apt/sources.list <> $GITHUB_OUTPUT echo "提取到的版本号: $VERSION" - name: 验证版本号 run: | if [ -z "${{ steps.extract_version.outputs.version }}" ]; then echo "::error::提交信息中未找到有效的版本号 (示例: 'Release v1.0.1')" exit 1 fi echo "${{ steps.extract_version.outputs.version }}" - name: 创建并推送标签 run: | git tag -a ${{ steps.extract_version.outputs.version }} -m "Release ${{ steps.extract_version.outputs.version }}" git push origin ${{ steps.extract_version.outputs.version }} - name: 查找第1个.bin 文件 id: find-bin-files-1 run: | BIN_FILES=$(find ./Bin -name "*.bin" -print0 | tr '\0' ',') BIN_FILES=${BIN_FILES%,} echo "bin_files=$BIN_FILES" >> $GITHUB_OUTPUT echo "bin_files=$BIN_FILES" - name: 查找第2个.bin 文件 id: find-bin-files-2 run: | BIN_FILES=$(find ./Bin1 -name "*.bin" -print0 | tr '\0' ',') BIN_FILES=${BIN_FILES%,} echo "bin_files=$BIN_FILES" >> $GITHUB_OUTPUT - name: 查找第3个.bin 文件 id: find-bin-files-3 run: | BIN_FILES=$(find ./Bin2 -name "*.bin" -print0 | tr '\0' ',') BIN_FILES=${BIN_FILES%,} echo "bin_files=$BIN_FILES" >> $GITHUB_OUTPUT - name: 获取上次发布标签 id: get-previous-tag run: | PREVIOUS_TAG=$(git tag --sort=-creatordate | sed -n '2p') if [ -z "$PREVIOUS_TAG" ]; then PREVIOUS_TAG="HEAD~1" # 如果没有上次标签,使用上一次提交 fi echo "previous_tag=$PREVIOUS_TAG" >> $GITHUB_OUTPUT echo "$PREVIOUS_TAG" - name: 获取提交记录 id: get-commit-log run: | # 获取指定标签到当前提交之间的提交记录,并按特定格式输出,同时过滤掉包含 'Update README.md' 的记录 COMMIT_LOG=$(git log ${{ steps.get-previous-tag.outputs.previous_tag }}..HEAD --pretty=format:"- %s (%h)" | grep -v 'Update README.md' | tr '\n' '\r') # 将 COMMIT_LOG 变量的值输出到 GitHub Actions 的输出中 echo "commit_log=$COMMIT_LOG" >> $GITHUB_OUTPUT # 打印 COMMIT_LOG 变量的值,方便调试查看 echo "commit_log=$COMMIT_LOG" - name: 解析Release标题 id: parse-release run: | commit_message="${{ github.event.head_commit.message }}" title=$(echo "$commit_message" | cut -d ' ' -f 3-) if [ -z "$title" ]; then echo "未在提交信息中找到 Release 标题。" title="default_title" fi # 使用新语法设置输出 echo "title=$title" >> $GITHUB_OUTPUT echo "title=$title" - name: 获取最终Release信息 run: | echo "Repo_Name = ${{ steps.ActionEnv.outputs.ENV_REPO_NAME }}" echo "Repo_Url = ${{ steps.ActionEnv.outputs.ENV_REPO_URL }}" echo "Tag_Name = ${{ steps.extract_version.outputs.version }}" echo "Release_Name = ${{ steps.parse-release.outputs.title }}" echo "Release_version = ${{ steps.extract_version.outputs.version }}" echo "Body = ${{ steps.get-commit-log.outputs.commit_log }}" - name: 创建Gitea发布 uses: https://git.linuxacme.com/iorebuild/gitea-release-action@main with: gitea_token: ${{ secrets.AUTOGENERATE }} gitea_instance_url: ${{ steps.ActionEnv.outputs.ENV_SERVER_URL }} repository: ${{ steps.ActionEnv.outputs.ENV_REPO_NAME }} name: ${{ steps.parse-release.outputs.title }} tag_name: ${{ steps.extract_version.outputs.version }} body: | ${{ steps.get-commit-log.outputs.commit_log }} draft: false prerelease: false files: | ${{ steps.find-bin-files-1.outputs.bin_files }} ${{ steps.find-bin-files-2.outputs.bin_files }} ${{ steps.find-bin-files-3.outputs.bin_files }} AutoREADME.md: # 工作在ubuntu-latest系统中 runs-on: ubuntu-latest # 以下是steps steps: - name: 检出仓库 uses: https://git.linuxacme.com/iorebuild/checkout@v4 with: fetch-depth: 0 # 启用子模块支持 submodules: false - name: 设置环境变量 id: ActionEnv run: | #设置上海时间 sudo ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime # 正确的变量赋值 UserName="${{ github.actor }}" UserEmail=$(git log -1 --pretty=format:'%ae') GitRepo="${{ github.repository }}" ServerUrl="${{ github.server_url }}" RepoUrl="${{ github.repository }}" # 将变量设置为步骤输出 echo "ENV_USER_NAME=$UserName" >> $GITHUB_OUTPUT echo "ENV_USER_EMAIL=$UserEmail" >> $GITHUB_OUTPUT echo "ENV_REPO_NAME=$GitRepo" >> $GITHUB_OUTPUT echo "ENV_SERVER_URL=$ServerUrl" >> $GITHUB_OUTPUT echo "ENV_REPO_URL=$ServerUrl/$RepoUrl" >> $GITHUB_OUTPUT - name: 检查环境变量 run: | echo "用户名 = ${{ steps.ActionEnv.outputs.ENV_USER_NAME }}" echo "邮箱= ${{ steps.ActionEnv.outputs.ENV_USER_EMAIL }}" echo "仓库名 = ${{ steps.ActionEnv.outputs.ENV_REPO_NAME }}" echo "服务器URL = ${{ steps.ActionEnv.outputs.ENV_SERVER_URL }}" echo "仓库URL = ${{ steps.ActionEnv.outputs.ENV_REPO_URL }}" - name: 配置Git用户 run: | git config user.name "${{ steps.ActionEnv.outputs.ENV_USER_NAME }}" git config user.email "${{ steps.ActionEnv.outputs.ENV_USER_EMAIL }}" - name: 替换软件源为国内软件源(USTC) run: | cat /etc/os-release sudo rm -f /etc/apt/sources.list sudo rm -f /etc/apt/sources.list.d/* sudo tee /etc/apt/sources.list <> README.md - name: 提交和推送代码 run: | git add README.md git commit -m "Update README.md" git push https://${{ secrets.AUTOGENERATE }}@git.linuxacme.com/${{ steps.ActionEnv.outputs.ENV_REPO_NAME }}