Files
Morris 5b82a885f9 fix(build-bot): report job 加 GH_REPO;已合并 PR 改为编默认分支
- report job 不 checkout,gh 没有 git 上下文会报
  'fatal: not a git repository' → 补 GH_REPO 环境变量
- 只有 state=open 的 PR 才编它的 head.sha;
  已合并/已关闭的 PR 编译默认分支,避免编到过期代码
- 回执里提示「PR 已 merged,改为编译默认分支」
2026-11-21 19:56:13 +08:00

313 lines
10 KiB
YAML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 白名单外一律忽略
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:.*<Version>\(.*\)</Version>.*:\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 <<EOF
🔨 **开始编译**:${PLATFORMS}
- 来源提交:\`${REF:0:7}\`
- 目标:\`${TARGET}\`
- 实时日志:${RUN_URL}
编译完成后会在这里回复结果。
EOF
gh issue comment "$ISSUE" --body-file ack.md
build-mac:
needs: prepare
if: needs.prepare.outputs.ok == 'true' && (needs.prepare.outputs.target == 'mac' || needs.prepare.outputs.target == 'all')
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ needs.prepare.outputs.ref }}
- uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- uses: subosito/flutter-action@v2
with:
flutter-version: ${{ env.FLUTTER_VERSION }}
channel: stable
cache: true
- name: 编译 macOS
env:
GC_NO_PROGRESS: '1'
run: ./build.sh build --targets=mac
- uses: actions/upload-artifact@v4
with:
name: mac-zip
path: dist/mac-arm64/*.zip
if-no-files-found: error
retention-days: 3
build-win:
needs: prepare
if: needs.prepare.outputs.ok == 'true' && (needs.prepare.outputs.target == 'win-amd' || needs.prepare.outputs.target == 'win-arm' || needs.prepare.outputs.target == 'all')
runs-on: windows-latest
defaults:
run:
shell: pwsh
steps:
- uses: actions/checkout@v4
with:
ref: ${{ needs.prepare.outputs.ref }}
- uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- uses: subosito/flutter-action@v2
with:
flutter-version: ${{ env.FLUTTER_VERSION }}
channel: stable
cache: true
- name: 编译 Windows
run: ./build.ps1 build
- uses: actions/upload-artifact@v4
with:
name: win-zip
path: |
dist/win-amd64/*.zip
dist/win-arm64/*.zip
if-no-files-found: error
retention-days: 3
publish:
needs: [prepare, build-mac, build-win]
if: |
always() &&
needs.prepare.outputs.ok == 'true' &&
(needs.build-mac.result == 'success' || needs.build-win.result == 'success')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
path: artifacts
- name: 汇总产物
env:
TARGET: ${{ needs.prepare.outputs.target }}
run: |
set -e
mkdir -p out
case "$TARGET" in
mac) PATTERN='*-mac-arm64.zip' ;;
win-amd) PATTERN='*-win-amd64.zip' ;;
win-arm) PATTERN='*-win-arm64.zip' ;;
*) PATTERN='*.zip' ;;
esac
# Windows 作业一次会编译 amd64 + arm64,这里只挑本次请求的平台
find artifacts -name '*.zip' | while read -r f; do
case "$f" in
$PATTERN) cp "$f" out/ ;;
esac
done
[ -n "$(ls -A out 2>/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
手动触发构建(\`@build-bot build ${TARGET}\`)。
触发人:@${ACTOR}
来源:#${ISSUE}
提交:\`${REF:0:7}\`,版本号 ${VERSION}
> 预发布版本:仅供测试,不保证稳定。
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