From ec3f3aedc5861f80946d38532571d765752dad1a Mon Sep 17 00:00:00 2001 From: ygsgdbd Date: Fri, 6 Dec 2024 17:20:44 +0800 Subject: [PATCH] Add GitHub Actions workflow for Homebrew tap updates --- .github/workflows/update-homebrew.yml | 134 ++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 .github/workflows/update-homebrew.yml diff --git a/.github/workflows/update-homebrew.yml b/.github/workflows/update-homebrew.yml new file mode 100644 index 0000000..ddd6426 --- /dev/null +++ b/.github/workflows/update-homebrew.yml @@ -0,0 +1,134 @@ +name: Update Homebrew Tap + +on: + release: + types: [published] + +jobs: + update-tap: + runs-on: macos-latest + steps: + - name: Checkout tap + uses: actions/checkout@v4 + with: + repository: ygsgdbd/homebrew-tap + token: ${{ secrets.TAP_TOKEN }} + path: homebrew-tap + + - name: Get release info + id: release + run: | + # 获取版本号(移除 v 前缀如果存在) + VERSION=${GITHUB_REF#refs/tags/} + VERSION=${VERSION#v} + echo "version=$VERSION" >> $GITHUB_OUTPUT + + # 构建下载 URL 并验证文件是否存在 + DOWNLOAD_URL="https://github.com/ygsgdbd/TypeSwitch/releases/download/${GITHUB_REF#refs/tags/}/TypeSwitch.dmg" + HTTP_STATUS=$(curl -L -s -o TypeSwitch.dmg -w "%{http_code}" "$DOWNLOAD_URL") + + if [ "$HTTP_STATUS" != "200" ]; then + echo "::error::Failed to download DMG file. HTTP status: $HTTP_STATUS" + exit 1 + fi + + # 计算 SHA256 + SHA256=$(shasum -a 256 TypeSwitch.dmg | cut -d ' ' -f 1) + if [ -z "$SHA256" ]; then + echo "::error::Failed to calculate SHA256" + exit 1 + fi + echo "sha256=$SHA256" >> $GITHUB_OUTPUT + + # 输出信息用于调试 + echo "Version: $VERSION" + echo "SHA256: $SHA256" + + - name: Verify tap directory + run: | + cd homebrew-tap + mkdir -p Casks + if [ ! -d "Casks" ]; then + echo "::error::Failed to create Casks directory" + exit 1 + fi + + - name: Update formula + run: | + cd homebrew-tap + cat > Casks/typeswitch.rb << EOL + cask "typeswitch" do + version "${{ steps.release.outputs.version }}" + sha256 "${{ steps.release.outputs.sha256 }}" + + url "https://github.com/ygsgdbd/TypeSwitch/releases/download/v#{version}/TypeSwitch.dmg" + name "TypeSwitch" + desc "Automatic input method switcher for different applications" + homepage "https://github.com/ygsgdbd/TypeSwitch" + + auto_updates false + depends_on macos: ">= :ventura" + + app "TypeSwitch.app" + + caveats <<~EOS + TypeSwitch is currently unsigned. You'll need to: + 1. Right-click the app and select "Open" + 2. Click "Open" in the dialog that appears + 3. Go to System Settings > Privacy & Security and approve the app + EOS + + zap trash: [ + "~/Library/Application Support/TypeSwitch", + "~/Library/Preferences/top.ygsgdbd.TypeSwitch.plist", + "~/Library/Caches/top.ygsgdbd.TypeSwitch" + ] + end + EOL + + - name: Verify formula + run: | + cd homebrew-tap + if [ ! -f "Casks/typeswitch.rb" ]; then + echo "::error::Formula file was not created" + exit 1 + fi + + # 简单的语法检查 + ruby -c Casks/typeswitch.rb || { + echo "::error::Ruby syntax check failed" + exit 1 + } + + - name: Commit and push changes + run: | + cd homebrew-tap + git config user.name "GitHub Action Bot" + git config user.email "github-actions[bot]@users.noreply.github.com" + + # 检查是否有变更 + if git diff --quiet; then + echo "No changes to commit" + exit 0 + fi + + git add Casks/typeswitch.rb + git commit -m "Update typeswitch to ${{ steps.release.outputs.version }}" + + # 添加重试逻辑 + MAX_RETRIES=3 + RETRY_COUNT=0 + while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do + if git push; then + echo "Successfully pushed changes" + exit 0 + fi + RETRY_COUNT=$((RETRY_COUNT+1)) + if [ $RETRY_COUNT -lt $MAX_RETRIES ]; then + echo "Push failed, retrying in 5 seconds..." + sleep 5 + fi + done + + echo "::error::Failed to push changes after $MAX_RETRIES attempts" + exit 1 \ No newline at end of file