Files
GC/.github/workflows/build-bot.yml
远影科技 8b4cabba28
PR 审核 / 后端编译 (pull_request) Waiting to run
PR 审核 / 前端检查(Flutter) (pull_request) Waiting to run
PR 审核 / 安全扫描 (pull_request) Waiting to run
ci(build-bot): 支持 version= 参数指定版本;mac 大小写不敏感;加速 macOS 构建
1. @build-bot build mac version=1.1.3 可指定版本(缺省仍读 Directory.Build.props)
   - 评论里 version=x.y.z 优先,大小写不敏感
   - mac / Mac / MAC 都能匹配(grep -oiE 已忽略大小写)
   - 版本透传给 build.sh / build.ps1,回帖与 Release notes 都显示版本
2. macOS 构建提速
   - 缓存 NuGet 包(~/.nuget/packages)与 CocoaPods(Pods/ + Caches)
   - runner 固定 macos-14(原生 arm64,避开 Rosetta 模拟)
   - build-bot 与 build-release 两个工作流同步优化
2026-11-22 08:09:23 +08:00

343 lines
12 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(大小写均可:Mac / MAC)
# @build-bot build mac version=1.1.3 编译 macOS 1.1.3(不写 version= 则用仓库当前版本)
# @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=x.y.z 优先(大小写不敏感),没有则用仓库当前版本。
CLI_VERSION=$(printf '%s' "$BODY" \
| grep -oiE 'version[[:space:]]*=[[:space:]]*[0-9]+\.[0-9]+\.[0-9]+' \
| head -1 \
| sed -E 's/.*=[[:space:]]*//' || true)
if [ -n "$CLI_VERSION" ]; then
VERSION="$CLI_VERSION"
echo "使用评论指定版本:$VERSION"
else
VERSION=$(sed -n 's:.*<Version>\(.*\)</Version>.*:\1:p' Directory.Build.props | head -1)
[ -n "$VERSION" ] || VERSION=1.0.0
fi
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 }}
VERSION: ${{ steps.parse.outputs.version }}
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}\`
- 版本:\`${VERSION}\`
- 实时日志:${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-14
steps:
- uses: actions/checkout@v4
with:
ref: ${{ needs.prepare.outputs.ref }}
- uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
# NuGet 包缓存:Avalonia / CommunityToolkit 等依赖每次冷下载要几分钟,
# 依赖没变时直接命中缓存。key 基于所有 csproj + .props。
- uses: actions/cache@v4
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj', '**/*.props', '**/packages.lock.json') }}
restore-keys: ${{ runner.os }}-nuget-
- uses: subosito/flutter-action@v2
with:
flutter-version: ${{ env.FLUTTER_VERSION }}
channel: stable
cache: true
# CocoaPods:Pods/ 被 gitignore,Podfile.lock 没变时复用,省掉每次 pod install
- uses: actions/cache@v4
with:
path: |
GC-UI/frontend/macos/Pods
~/Library/Caches/CocoaPods
key: ${{ runner.os }}-pods-${{ hashFiles('GC-UI/frontend/macos/Podfile.lock') }}
restore-keys: ${{ runner.os }}-pods-
- name: 编译 macOS
env:
GC_NO_PROGRESS: '1'
run: ./build.sh build --targets=mac -version=${{ needs.prepare.outputs.version }}
- 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 -Version ${{ needs.prepare.outputs.version }}
- 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} version=${VERSION}\`)。
触发人:@${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