name: build-bot # 在 Issue / PR 里评论触发编译: # @build-bot build mac 编译 macOS # @build-bot build win-amd 编译 Windows amd64 # @build-bot build win-arm 编译 Windows arm64 # @build-bot build all 三个平台全编 # 产物发布到 ip-prerelease 预发布(同一 tag 滚动更新,不删别的平台资产)。 on: issue_comment: types: [created] concurrency: group: release-ip-prerelease cancel-in-progress: false permissions: contents: write issues: write pull-requests: write env: TZ: Asia/Shanghai BOT_TAG: ip-prerelease DOTNET_VERSION: '8.0.x' FLUTTER_VERSION: '3.24.5' jobs: prepare: # 只处理带 @build-bot build 的评论,且只允许仓库成员触发 if: | contains(github.event.comment.body, '@build-bot') && (github.event.comment.author_association == 'OWNER' || github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'COLLABORATOR') runs-on: ubuntu-latest outputs: target: ${{ steps.parse.outputs.target }} ref: ${{ steps.parse.outputs.ref }} is_pr: ${{ steps.parse.outputs.is_pr }} version: ${{ steps.parse.outputs.version }} ok: ${{ steps.parse.outputs.ok }} steps: - uses: actions/checkout@v4 with: fetch-depth: 0 - id: parse name: 解析指令 env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} BODY: ${{ github.event.comment.body }} ISSUE_NUMBER: ${{ github.event.issue.number }} run: | set -e # 抓 @build-bot build ,target 白名单外一律忽略 TARGET=$(printf '%s' "$BODY" \ | grep -oiE '@build-bot[[:space:]]+build[[:space:]]+(mac|win-amd|win-arm|all)' \ | head -1 \ | awk '{print tolower($NF)}' || true) if [ -z "$TARGET" ]; then echo "ok=false" >> "$GITHUB_OUTPUT" echo "没有匹配到合法目标,忽略" exit 0 fi echo "ok=true" >> "$GITHUB_OUTPUT" echo "target=$TARGET" >> "$GITHUB_OUTPUT" VERSION=$(sed -n 's:.*\(.*\).*:\1:p' Directory.Build.props | head -1) [ -n "$VERSION" ] || VERSION=1.0.0 echo "version=$VERSION" >> "$GITHUB_OUTPUT" # Issue 评论挂在 PR 上时,用 PR 的 head 提交;否则用默认分支最新 IS_PR=$(gh api "repos/${{ github.repository }}/issues/${ISSUE_NUMBER}" \ --jq '.pull_request != null' 2>/dev/null || echo false) echo "is_pr=$IS_PR" >> "$GITHUB_OUTPUT" if [ "$IS_PR" = "true" ]; then # 只有「还开着」的 PR 才编它的 head;已合并/已关闭的 PR 编默认分支, # 否则会编到一份过期的代码。 PR_STATE=$(gh api "repos/${{ github.repository }}/pulls/${ISSUE_NUMBER}" --jq '.state') if [ "$PR_STATE" = "open" ]; then REF=$(gh api "repos/${{ github.repository }}/pulls/${ISSUE_NUMBER}" --jq '.head.sha') else REF="${{ github.event.repository.default_branch }}" echo "PR 已 $PR_STATE,改为编译默认分支" fi else REF="${{ github.event.repository.default_branch }}" fi echo "ref=$REF" >> "$GITHUB_OUTPUT" echo "目标=$TARGET 提交=$REF 是否PR=$IS_PR" - name: 回复「开始编译」 if: steps.parse.outputs.ok == 'true' env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} ISSUE: ${{ github.event.issue.number }} TARGET: ${{ steps.parse.outputs.target }} REF: ${{ steps.parse.outputs.ref }} RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} run: | set -e case "$TARGET" in mac) PLATFORMS="macOS" ;; win-amd) PLATFORMS="Windows amd64" ;; win-arm) PLATFORMS="Windows arm64" ;; *) PLATFORMS="macOS + Windows amd64 + Windows arm64" ;; esac cat > ack.md </dev/null)" ] || { echo "没有匹配 $PATTERN 的产物"; exit 1; } ls -lh out - id: publish_step name: 发布到 ip-prerelease env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} VERSION: ${{ needs.prepare.outputs.version }} TARGET: ${{ needs.prepare.outputs.target }} REF: ${{ needs.prepare.outputs.ref }} TAG: ${{ env.BOT_TAG }} ACTOR: ${{ github.event.comment.user.login }} ISSUE: ${{ github.event.issue.number }} run: | set -e cat > notes.md < 预发布版本:仅供测试,不保证稳定。 EOF if gh release view "$TAG" >/dev/null 2>&1; then # 已有预发布:只覆盖本次编译出来的资产,别的平台保留 gh release upload "$TAG" out/*.zip --clobber gh release edit "$TAG" --prerelease --notes-file notes.md else gh release create "$TAG" out/*.zip \ --prerelease \ --target "$REF" \ --title "Prerelease ${VERSION}" \ --notes-file notes.md fi echo "https://github.com/${{ github.repository }}/releases/tag/$TAG" >> "$GITHUB_STEP_SUMMARY" # 结果回帖独立成一个 job:编译失败 / 发布失败也要有回音, # 不能因为 publish 被跳过就静默。 report: needs: [prepare, build-mac, build-win, publish] if: always() && needs.prepare.outputs.ok == 'true' runs-on: ubuntu-latest steps: - name: 回帖结果 env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # 本 job 不 checkout(省时间),gh 需要显式指定仓库 GH_REPO: ${{ github.repository }} ISSUE: ${{ github.event.issue.number }} TARGET: ${{ needs.prepare.outputs.target }} REF: ${{ needs.prepare.outputs.ref }} MAC: ${{ needs.build-mac.result }} WIN: ${{ needs.build-win.result }} PUB: ${{ needs.publish.result }} RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} run: | set -e REPO="${{ github.repository }}" line() { case "$2" in success) echo "- $1:✅ 成功" ;; skipped) echo "- $1:⏭ 未请求(跳过)" ;; cancelled) echo "- $1:⛔ 已取消" ;; *) echo "- $1:❌ 失败" ;; esac } { if [ "$PUB" = "success" ]; then echo "✅ **编译完成**:\`${TARGET}\`" else echo "❌ **编译未完成**:\`${TARGET}\`" fi echo line "macOS" "$MAC" line "Windows" "$WIN" echo echo "- 来源提交:\`${REF:0:7}\`" if [ "$PUB" = "success" ]; then echo "- 产物:https://github.com/${REPO}/releases/tag/ip-prerelease" else echo "- 日志:${RUN_URL}" fi } > result.md gh issue comment "$ISSUE" --body-file result.md