|
| 1 | +name: Build & Release on Tag |
| 2 | +permissions: |
| 3 | + contents: write |
| 4 | + |
| 5 | +on: |
| 6 | + push: |
| 7 | + tags: |
| 8 | + - '**/v*' # 匹配格式: project-name/v1.2.3 |
| 9 | + |
| 10 | +jobs: |
| 11 | + parse-and-build: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Checkout repository |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + |
| 20 | + - name: Parse project name and version from tag |
| 21 | + id: parse-tag |
| 22 | + run: | |
| 23 | + TAG_NAME=${GITHUB_REF#refs/tags/} |
| 24 | + PROJECT_NAME=$(echo $TAG_NAME | cut -d'/' -f1) |
| 25 | + VERSION=$(echo $TAG_NAME | cut -d'/' -f2) |
| 26 | + |
| 27 | + echo "PROJECT_NAME=$PROJECT_NAME" >> $GITHUB_OUTPUT |
| 28 | + echo "VERSION=$VERSION" >> $GITHUB_OUTPUT |
| 29 | + echo "TAG_NAME=$TAG_NAME" >> $GITHUB_OUTPUT |
| 30 | + |
| 31 | + echo "📦 解析结果:" |
| 32 | + echo " 项目名称: $PROJECT_NAME" |
| 33 | + echo " 版本号: $VERSION" |
| 34 | + echo " 完整Tag: $TAG_NAME" |
| 35 | +
|
| 36 | + - name: Validate project exists |
| 37 | + run: | |
| 38 | + if [ ! -d "projects/${{ steps.parse-tag.outputs.PROJECT_NAME }}" ]; then |
| 39 | + echo "❌ 错误: 项目 ${{ steps.parse-tag.outputs.PROJECT_NAME }} 不存在" |
| 40 | + echo "💡 可用项目列表:" |
| 41 | + ls -1 projects/ |
| 42 | + exit 1 |
| 43 | + fi |
| 44 | + echo "✅ 项目 ${{ steps.parse-tag.outputs.PROJECT_NAME }} 存在" |
| 45 | +
|
| 46 | + - name: Setup Node.js |
| 47 | + uses: actions/setup-node@v4 |
| 48 | + with: |
| 49 | + node-version: '22' |
| 50 | + |
| 51 | + - name: Install root dependencies |
| 52 | + run: npm install |
| 53 | + |
| 54 | + - name: Install project dependencies |
| 55 | + working-directory: projects/${{ steps.parse-tag.outputs.PROJECT_NAME }} |
| 56 | + run: npm install |
| 57 | + |
| 58 | + - name: Build project |
| 59 | + run: npm run build:${{ steps.parse-tag.outputs.PROJECT_NAME }} |
| 60 | + |
| 61 | + - name: Verify build output |
| 62 | + run: | |
| 63 | + if [ ! -d "projects/${{ steps.parse-tag.outputs.PROJECT_NAME }}/dist" ]; then |
| 64 | + echo "❌ 构建产物不存在" |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | + echo "✅ 构建产物已生成:" |
| 68 | + ls -lh projects/${{ steps.parse-tag.outputs.PROJECT_NAME }}/dist/ |
| 69 | +
|
| 70 | + - name: Get release info |
| 71 | + id: release-info |
| 72 | + uses: actions/github-script@v7 |
| 73 | + with: |
| 74 | + script: | |
| 75 | + const tagName = '${{ steps.parse-tag.outputs.TAG_NAME }}'; |
| 76 | + const version = '${{ steps.parse-tag.outputs.VERSION }}'; |
| 77 | + |
| 78 | + // 尝试获取 release 信息 |
| 79 | + try { |
| 80 | + const { data: release } = await github.rest.repos.getReleaseByTag({ |
| 81 | + owner: context.repo.owner, |
| 82 | + repo: context.repo.repo, |
| 83 | + tag: tagName |
| 84 | + }); |
| 85 | + |
| 86 | + core.setOutput('release_body', release.body || ''); |
| 87 | + core.setOutput('release_name', release.name || version); |
| 88 | + } catch (e) { |
| 89 | + core.setOutput('release_body', ''); |
| 90 | + core.setOutput('release_name', version); |
| 91 | + } |
| 92 | +
|
| 93 | + - name: Create GitHub Release |
| 94 | + uses: softprops/action-gh-release@v2 |
| 95 | + with: |
| 96 | + tag_name: ${{ steps.parse-tag.outputs.TAG_NAME }} |
| 97 | + name: ${{ steps.parse-tag.outputs.PROJECT_NAME }} ${{ steps.parse-tag.outputs.VERSION }} |
| 98 | + body: ${{ steps.release-info.outputs.release_body }} |
| 99 | + files: projects/${{ steps.parse-tag.outputs.PROJECT_NAME }}/dist/** |
| 100 | + draft: false |
| 101 | + prerelease: false |
| 102 | + |
| 103 | + - name: Trigger GreasyFork Webhook |
| 104 | + if: success() |
| 105 | + run: | |
| 106 | + echo "🚀 Triggering GreasyFork sync for ${{ steps.parse-tag.outputs.PROJECT_NAME }}..." |
| 107 | + curl -f -X POST \ |
| 108 | + -H "Content-Type: application/json" \ |
| 109 | + --data '{ |
| 110 | + "action": "published", |
| 111 | + "project": "${{ steps.parse-tag.outputs.PROJECT_NAME }}", |
| 112 | + "version": "${{ steps.parse-tag.outputs.VERSION }}", |
| 113 | + "tag": "${{ steps.parse-tag.outputs.TAG_NAME }}", |
| 114 | + "release_url": "${{ github.server_url }}/${{ github.repository }}/releases/tag/${{ steps.parse-tag.outputs.TAG_NAME }}" |
| 115 | + }' \ |
| 116 | + "${{ secrets.GREASYFORK_WEBHOOK_URL }}" |
| 117 | + echo "✅ GreasyFork webhook triggered successfully" |
0 commit comments