diff --git a/.github/workflows/build-alpha.yml b/.github/workflows/build-alpha.yml index e35de9b4b..ea42c76af 100644 --- a/.github/workflows/build-alpha.yml +++ b/.github/workflows/build-alpha.yml @@ -1,6 +1,13 @@ name: Build alpha on: workflow_dispatch: + inputs: + build_num: + required: true + type: choice + default: "1" + options: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 114514] + push: branches: - 'dev' @@ -12,14 +19,14 @@ on: - '!.github/workflows/build-alpha.yml' jobs: - update_version: - name: Read latest version + prepare_publish_info: + name: Prepare publish info runs-on: ubuntu-latest outputs: - # 定义输出变量 version,以便在其他job中引用 - new_version: ${{ steps.update-version.outputs.new_version }} - last_commit: ${{ steps.get-last-commit.outputs.last_commit }} + new_version: ${{ steps.update_version.outputs.new_version }} + included_commits: ${{ steps.analyze_push_event.outputs.included_commits }} + last_commit_hash: ${{ steps.analyze_push_event.outputs.last_commit_hash }} steps: - name: Checkout code @@ -27,33 +34,89 @@ jobs: with: fetch-depth: 0 - - name: 获取最后一次提交 - id: get-last-commit + - name: 解析push事件信息 + id: analyze_push_event run: | - last_commit=$(git log -1 --pretty="%h %s" --first-parent) - echo "last_commit=$last_commit" >> $GITHUB_OUTPUT + included_commits=$(git log ${{ github.event.before }}..HEAD --pretty="%h %s" --first-parent | jq -Rs '.') + last_commit_hash=$(git log -1 --pretty="%H" --first-parent) + echo "included_commits=$included_commits" >> $GITHUB_OUTPUT + echo "last_commit_hash=$last_commit_hash" >> $GITHUB_OUTPUT + + - name: Get previous workflow run + id: get_previous_run + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + # TODO: 获取上一个workflow run运行状态以等待其artifact + workflow_runs=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ + "https://api.github.com/repos/${{ github.repository }}/actions/workflows/build-alpha.yml/runs?status=completed&per_page=2") + previous_run_id=$(echo $workflow_runs | jq -r '.workflow_runs[0].id') + previous_run_success=0 + if (echo $workflow_runs | jq -r '.workflow_runs[1].conclusion' | grep -q 'success'); then + previous_run_success=1 + fi + echo "Previous run ID: $previous_run_id" + echo "previous_run_id=$previous_run_id" >> $GITHUB_OUTPUT + echo "Previous run success: $previous_run_success" + echo "previous_run_success=$previous_run_success" >> $GITHUB_OUTPUT + + - name: 从artifact获取上次构建信息 + if: ${{ ! inputs.build_num }} + uses: actions/download-artifact@v4 + with: + name: build_info + github-token: ${{ secrets.GITHUB_TOKEN }} + run-id: ${{ steps.get_previous_run.outputs.previous_run_id }} - name: 生成alpha版本号 - id: update-version + id: update_version run: | - version_name=$(yq e .version pubspec.yaml | cut -d "+" -f 1) last_tag=$(git tag --sort=committerdate | tail -1) + version_name=$(yq e .version pubspec.yaml | cut -d "+" -f 1) + version_code=$(echo $last_tag | cut -d "+" -f 2) - if (echo $last_tag | grep -v "+"); then - echo "Illegal tag! ($last_tag)" - exit 1 - elif (echo $last_tag | grep -v $version_name); then - echo "No tags for current version in the repo, please add one manually." + if [[ ! -e build_info.yml && -z "${{ inputs.build_num }}" ]]; then + echo "Neither build_info.yml exists nor specified build_num manually!" exit 1 fi + + # pubspec.yaml中版本号比tag新,则发布pre版,否则发布pre版 + alpha_or_pre=pre + if (echo $last_tag | grep $version_name); then + alpha_or_pre=alpha + fi - version_code=$(echo $last_tag | cut -d "+" -f 2) - datetime=$(date -u -d "8 hours" +%m%d%H%M) - echo "new_version=${version_name}-alpha.${datetime}+${version_code}" >> $GITHUB_OUTPUT + # 如果上次workflow run失败,且与当前版本号一致,不递增build_num。 + build_num=1 + if [[ -n "${{ inputs.build_num }}" ]]; then + build_num=${{ inputs.build_num }} + elif [[ $(yq -r .version build_info.yml) == $version_name ]]; then + let build_num=$(yq -r .build_num build_info.yml)+${{ steps.get_previous_run.outputs.previous_run_success }} + fi + + echo "new_version=${version_name}-${alpha_or_pre}.${build_num}+${version_code}" >> $GITHUB_OUTPUT + echo "new_build_num=$build_num" >> $GITHUB_OUTPUT + + - name: 生成新build_info.yml + run: | + version=$(yq -er .version pubspec.yaml | cut -d "+" -f 1) + build_num=${{ steps.update_version.outputs.new_build_num}} + + rm -f build_info.yml + cat << EOF > build_info.yml + version: $version + build_num: $build_num + EOF + + - name: 上传build_info.yml + uses: actions/upload-artifact@v4 + with: + name: build_info + path: ./build_info.yml build_matrix: name: Build CI (${{ matrix.target_platform }}) - needs: update_version + needs: prepare_publish_info runs-on: ${{ matrix.build_os }} strategy: matrix: @@ -104,7 +167,7 @@ jobs: - name: 更新版本号 id: version run: | - yq ".version=\"${{ needs.update_version.outputs.new_version }}\"" pubspec.yaml > tmp.yaml + yq ".version=\"${{ needs.prepare_publish_info.outputs.new_version }}\"" pubspec.yaml > tmp.yaml mv tmp.yaml pubspec.yaml - name: flutter build apk (universal) @@ -145,41 +208,38 @@ jobs: if: matrix.target_platform == 'iOS' run: | for file in app.ipa; do - new_file_name="build/Pili-${{ needs.update_version.outputs.new_version }}.ipa" + new_file_name="build/Pili-${{ needs.prepare_publish_info.outputs.new_version }}.ipa" mv "$file" "$new_file_name" done - name: 上传至artifact (${{ matrix.target_platform }}) if: startsWith(matrix.target_platform, 'android') - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: - name: PiliPalaX-alpha + name: PiliPalaX-${{ matrix.target_platform }} path: | build/app/outputs/flutter-apk/Pili-*.apk - name: 上传至artifact (iOS) if: matrix.target_platform == 'iOS' - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: - name: PiliPalaX-alpha + name: PiliPalaX-iOS path: | build/Pili-*.ipa upload: runs-on: ubuntu-latest needs: - - update_version + - prepare_publish_info - build_matrix steps: - name: 从artifact下载 - uses: actions/download-artifact@v3 - with: - name: PiliPalaX-alpha - path: ./PiliPalaX-alpha + uses: actions/download-artifact@v4 - name: 发送到Telegram频道 - uses: xireiki/channel-post@v1.0.7 + uses: xireiki/channel-post@v1.0.10 with: bot_token: ${{ secrets.BOT_TOKEN }} chat_id: ${{ secrets.CHAT_ID }} @@ -187,6 +247,6 @@ jobs: api_hash: ${{ secrets.TELEGRAM_API_HASH }} large_file: true method: sendFile - path: PiliPalaX-alpha/* + path: PiliPalaX-*/* parse_mode: Markdown - context: "*v${{ needs.update_version.outputs.new_version }}*\n${{ needs.update_version.outputs.last_commit }}" + context: "*v${{ needs.prepare_publish_info.outputs.new_version }}:*\n${{ fromJson(needs.prepare_publish_info.outputs.included_commits) }}"