build_release #41
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build and Release TrollScript | |
| on: | |
| workflow_dispatch: # 手动触发 | |
| repository_dispatch: | |
| types: [build_release] | |
| permissions: | |
| contents: write # 允许创建 Release 和 Tag | |
| jobs: | |
| build: | |
| runs-on: macos-15 | |
| steps: | |
| # 1. 拉取当前 Release 仓库 | |
| - name: Checkout Release Repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| token: ${{ secrets.GH_TOKEN }} | |
| # 2. 使用 GH_TOKEN 拉取私有仓库 TROLLSTORE | |
| - name: Checkout Private TrollScript Repository | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: ${{ secrets.TROLLSTORE }} | |
| token: ${{ secrets.GH_TOKEN }} | |
| path: TrollScript-Private | |
| fetch-depth: 0 | |
| - name: Setup Xcode | |
| uses: maxim-lobanov/setup-xcode@v1 | |
| with: | |
| xcode-version: 'latest-stable' | |
| - name: Show Xcode version | |
| run: | | |
| xcodebuild -version | |
| xcode-select -p | |
| - name: Cache Swift Package Manager | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| build/SourcePackages | |
| ~/Library/Caches/org.swift.swiftpm | |
| key: ${{ runner.os }}-spm-${{ hashFiles('**/Package.resolved') }} | |
| restore-keys: | | |
| ${{ runner.os }}-spm- | |
| - name: Install ldid (iOS signing tool) | |
| run: | | |
| brew install ldid | |
| ldid -h || true | |
| echo "ldid installed successfully" | |
| # 4. 从私有仓库 tag 更新版本号 | |
| - name: Update version from tag | |
| working-directory: TrollScript-Private | |
| run: | | |
| INFO_PLIST="TrollScript/Info.plist" | |
| # 获取最新 tag | |
| LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "") | |
| if [ -n "$LATEST_TAG" ]; then | |
| # 从 tag 提取版本号,去掉 v 前缀 | |
| VERSION="${LATEST_TAG#v}" | |
| echo "✅ 从 tag 提取版本号: $VERSION" | |
| else | |
| # 尝试获取当前 commit 的 tag | |
| CURRENT_TAG=$(git describe --tags --exact-match 2>/dev/null || echo "") | |
| if [ -n "$CURRENT_TAG" ]; then | |
| VERSION="${CURRENT_TAG#v}" | |
| echo "✅ 从当前 commit tag 提取版本号: $VERSION" | |
| else | |
| # 没有 tag,保持 Info.plist 中的版本不变 | |
| echo "⚠️ 无 tag,保持原版本号不变" | |
| exit 0 | |
| fi | |
| fi | |
| # 生成构建号(使用时间戳) | |
| BUILD_NUMBER=$(date +'%Y%m%d%H%M') | |
| # 更新 Info.plist | |
| echo "📝 更新 Info.plist:" | |
| echo " CFBundleShortVersionString: $VERSION" | |
| echo " CFBundleVersion: $BUILD_NUMBER" | |
| /usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $VERSION" "$INFO_PLIST" | |
| /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUILD_NUMBER" "$INFO_PLIST" | |
| # 验证更新 | |
| echo "✅ 更新后的版本信息:" | |
| /usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "$INFO_PLIST" | |
| /usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "$INFO_PLIST" | |
| # 5. 进入私有仓库目录,运行 build-tipa.sh | |
| - name: Build TrollScript | |
| working-directory: TrollScript-Private | |
| run: | | |
| chmod +x scripts/build-tipa.sh | |
| ./scripts/build-tipa.sh | |
| # 5. 复制 release 文件夹到本项目 + 提交 latest.tipa 到当前分支 | |
| - name: Copy Build Artifacts and Commit latest.tipa | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} # 参考步骤8的环境变量配置 | |
| run: | | |
| # 第一步:复制构建产物到本项目 | |
| mkdir -p release | |
| cp -r TrollScript-Private/release/* release/ | |
| echo "📦 Build artifacts:" | |
| ls -la release/ | |
| # 第二步:检查 latest.tipa 文件是否存在 | |
| LATEST_TIPA_FILE="./release/TrollScript_latest.tipa" | |
| if [ ! -f "$LATEST_TIPA_FILE" ]; then | |
| echo "❌ Error: $LATEST_TIPA_FILE not found!" | |
| exit 1 | |
| fi | |
| # 第三步:配置git(极简配置,仅为提交需要) | |
| git config --global user.name "github-actions[bot]" | |
| git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
| # 第四步:添加并提交 latest.tipa(仅提交该文件) | |
| git add "$LATEST_TIPA_FILE" | |
| # 检查是否有变更,避免空提交 | |
| if git diff --staged --quiet; then | |
| echo "ℹ️ No changes to latest.tipa, skip commit" | |
| else | |
| git commit -m "Update latest.tipa - $(date +'%Y-%m-%d %H:%M:%S CST')" | |
| # 第五步:推送到当前分支(使用GH_TOKEN认证) | |
| git push origin HEAD:${{ github.ref }} | |
| echo "✅ Successfully committed and pushed latest.tipa to current branch!" | |
| fi | |
| ls -la release/ | |
| # 6. 获取私有仓库最后 tag 的 commit 信息 | |
| - name: Get Version Info from Private Repository | |
| id: version | |
| working-directory: TrollScript-Private | |
| run: | | |
| # 获取最新 tag | |
| LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "") | |
| if [ -z "$LATEST_TAG" ]; then | |
| LATEST_TAG="v0.0.0-$(git rev-parse --short HEAD)" | |
| echo "⚠️ No tag found, using: $LATEST_TAG" | |
| fi | |
| # 获取 tag 对应的 commit 信息 | |
| if git rev-parse "$LATEST_TAG" >/dev/null 2>&1; then | |
| COMMIT_MSG=$(git log -1 --pretty=format:"%B" "$LATEST_TAG" 2>/dev/null || echo "New release") | |
| else | |
| COMMIT_MSG=$(git log -1 --pretty=format:"%B" 2>/dev/null || echo "New release") | |
| fi | |
| COMMIT_SHORT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown") | |
| COMMIT_HASH=$(git rev-parse HEAD 2>/dev/null || echo "") | |
| echo "release_tag=$LATEST_TAG" >> $GITHUB_OUTPUT | |
| echo "commit_short=$COMMIT_SHORT" >> $GITHUB_OUTPUT | |
| echo "commit_hash=$COMMIT_HASH" >> $GITHUB_OUTPUT | |
| # 处理多行 commit 消息 | |
| EOF_MARKER=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) | |
| echo "commit_msg<<$EOF_MARKER" >> $GITHUB_OUTPUT | |
| echo "$COMMIT_MSG" >> $GITHUB_OUTPUT | |
| echo "$EOF_MARKER" >> $GITHUB_OUTPUT | |
| echo "📌 Release Tag: $LATEST_TAG" | |
| echo "📌 Commit: $COMMIT_SHORT" | |
| echo "📌 Commit Message:" | |
| echo "$COMMIT_MSG" | |
| # 7. 创建 Release Notes | |
| - name: Create Release Notes | |
| run: | | |
| RELEASE_TAG="${{ steps.version.outputs.release_tag }}" | |
| COMMIT_MSG="${{ steps.version.outputs.commit_msg }}" | |
| COMMIT_SHORT="${{ steps.version.outputs.commit_short }}" | |
| cat > release-notes.md << EOF | |
| **TrollScript $RELEASE_TAG** | |
| ## 📋 $(TZ='Asia/Shanghai' date '+%Y-%m-%d %H:%M:%S CST') 更新内容 | |
| $COMMIT_MSG | |
| --- | |
| > Commit: \`$COMMIT_SHORT\` | |
| EOF | |
| echo "📝 Release Notes:" | |
| cat release-notes.md | |
| - name: Upload Build Artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: TrollScript-${{ steps.version.outputs.release_tag }} | |
| path: release/*.tipa | |
| retention-days: 30 | |
| # 8. 在本项目创建 Tag 并提交到 Release | |
| - name: Create GitHub Release | |
| if: ${{ steps.version.outputs.release_tag != '' }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| RELEASE_TAG="${{ steps.version.outputs.release_tag }}" | |
| # 查找 TIPA 文件 | |
| TIPA_FILE=$(find ./release -name "*.tipa" ! -name "*_latest.tipa" 2>/dev/null | head -1 || echo "") | |
| LATEST_FILE="./release/TrollScript_latest.tipa" | |
| if [ -z "$TIPA_FILE" ]; then | |
| echo "❌ No TIPA file found" | |
| exit 1 | |
| fi | |
| echo "📦 Found TIPA: $TIPA_FILE" | |
| # 删除已存在的 release(如果有) | |
| if gh release view "$RELEASE_TAG" &>/dev/null; then | |
| echo "⚠️ Release $RELEASE_TAG exists, deleting..." | |
| gh release delete "$RELEASE_TAG" --yes || true | |
| # 删除对应的 tag | |
| git push origin :refs/tags/$RELEASE_TAG 2>/dev/null || true | |
| fi | |
| # 创建新 release(只上传版本号文件) | |
| gh release create "$RELEASE_TAG" \ | |
| "$TIPA_FILE" \ | |
| --title "🧙 TrollScript $RELEASE_TAG" \ | |
| --notes-file release-notes.md \ | |
| --draft=false \ | |
| --prerelease=false | |
| echo "✅ Release $RELEASE_TAG created!" | |
| # 更新 latest release | |
| echo "📌 Updating latest release..." | |
| if gh release view "latest" &>/dev/null; then | |
| gh release delete "latest" --yes || true | |
| fi | |
| # 删除远程和本地的 latest tag | |
| git push origin :refs/tags/latest 2>/dev/null || true | |
| git tag -d latest 2>/dev/null || true | |
| gh release create "latest" \ | |
| "$LATEST_FILE" \ | |
| --title "🧙 TrollScript Latest $RELEASE_TAG" \ | |
| --notes-file release-notes.md \ | |
| --draft=false \ | |
| --prerelease=false | |
| echo "✅ Latest release updated!" |