|
| 1 | +name: CD - Build and Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + tags: |
| 7 | + - 'v*' |
| 8 | + workflow_dispatch: |
| 9 | + inputs: |
| 10 | + channel: |
| 11 | + description: 'Build channel' |
| 12 | + required: false |
| 13 | + default: 'beta' |
| 14 | + type: choice |
| 15 | + options: |
| 16 | + - beta |
| 17 | + - stable |
| 18 | + |
| 19 | +# 设置 GITHUB_TOKEN 权限 |
| 20 | +permissions: |
| 21 | + contents: write |
| 22 | + pages: write |
| 23 | + id-token: write |
| 24 | + |
| 25 | +# 只允许一个并发部署 |
| 26 | +concurrency: |
| 27 | + group: 'pages' |
| 28 | + cancel-in-progress: false |
| 29 | + |
| 30 | +env: |
| 31 | + NODE_VERSION: '20' |
| 32 | + PNPM_VERSION: '9' |
| 33 | + |
| 34 | +jobs: |
| 35 | + # ==================== 构建 ==================== |
| 36 | + build: |
| 37 | + runs-on: ubuntu-latest |
| 38 | + outputs: |
| 39 | + version: ${{ steps.version.outputs.version }} |
| 40 | + channel: ${{ steps.channel.outputs.channel }} |
| 41 | + |
| 42 | + steps: |
| 43 | + - name: Checkout |
| 44 | + uses: actions/checkout@v4 |
| 45 | + with: |
| 46 | + fetch-depth: 0 # 需要完整历史来下载 release |
| 47 | + |
| 48 | + - name: Determine channel |
| 49 | + id: channel |
| 50 | + run: | |
| 51 | + if [[ "${{ github.ref }}" == refs/tags/v* ]]; then |
| 52 | + echo "channel=stable" >> $GITHUB_OUTPUT |
| 53 | + echo "Channel: stable (tag trigger)" |
| 54 | + elif [[ "${{ github.event.inputs.channel }}" == "stable" ]]; then |
| 55 | + echo "channel=stable" >> $GITHUB_OUTPUT |
| 56 | + echo "Channel: stable (manual trigger)" |
| 57 | + else |
| 58 | + echo "channel=beta" >> $GITHUB_OUTPUT |
| 59 | + echo "Channel: beta" |
| 60 | + fi |
| 61 | +
|
| 62 | + - name: Setup pnpm |
| 63 | + uses: pnpm/action-setup@v4 |
| 64 | + with: |
| 65 | + version: ${{ env.PNPM_VERSION }} |
| 66 | + |
| 67 | + - name: Setup Node.js |
| 68 | + uses: actions/setup-node@v4 |
| 69 | + with: |
| 70 | + node-version: ${{ env.NODE_VERSION }} |
| 71 | + cache: 'pnpm' |
| 72 | + |
| 73 | + - name: Install dependencies |
| 74 | + run: pnpm install --frozen-lockfile |
| 75 | + |
| 76 | + - name: Get version |
| 77 | + id: version |
| 78 | + run: | |
| 79 | + if [[ "${{ github.ref }}" == refs/tags/v* ]]; then |
| 80 | + VERSION="${{ github.ref_name }}" |
| 81 | + VERSION="${VERSION#v}" |
| 82 | + else |
| 83 | + VERSION=$(node -p "require('./package.json').version") |
| 84 | + fi |
| 85 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 86 | + echo "Version: $VERSION" |
| 87 | +
|
| 88 | + - name: Type check |
| 89 | + run: pnpm typecheck |
| 90 | + |
| 91 | + - name: Run tests |
| 92 | + run: pnpm test |
| 93 | + |
| 94 | + # ===== 构建当前渠道的 Web 版本 ===== |
| 95 | + - name: Build Web version |
| 96 | + run: | |
| 97 | + SERVICE_IMPL=web pnpm build |
| 98 | + mv dist dist-web |
| 99 | +
|
| 100 | + # ===== 构建 DWEB 版本 ===== |
| 101 | + - name: Build DWEB version |
| 102 | + run: | |
| 103 | + SERVICE_IMPL=dweb pnpm build |
| 104 | + mv dist dist-dweb |
| 105 | +
|
| 106 | + - name: Install Plaoc CLI |
| 107 | + run: npm install -g @aspect/plaoc-cli || echo "Plaoc CLI not available" |
| 108 | + |
| 109 | + - name: Bundle DWEB with Plaoc |
| 110 | + continue-on-error: true |
| 111 | + run: | |
| 112 | + if command -v plaoc &> /dev/null; then |
| 113 | + plaoc bundle ./dist-dweb -c ./ -o ./dists |
| 114 | + echo "Plaoc bundle completed" |
| 115 | + else |
| 116 | + echo "Plaoc CLI not installed, using raw dist-dweb" |
| 117 | + mkdir -p dists |
| 118 | + cp -r dist-dweb/* dists/ |
| 119 | + fi |
| 120 | +
|
| 121 | + # ===== 准备 VitePress 的 webapp 目录 ===== |
| 122 | + - name: Prepare webapp for VitePress |
| 123 | + env: |
| 124 | + CHANNEL: ${{ steps.channel.outputs.channel }} |
| 125 | + GH_TOKEN: ${{ github.token }} |
| 126 | + run: | |
| 127 | + echo "Current channel: $CHANNEL" |
| 128 | +
|
| 129 | + # 当前构建的版本放到对应渠道目录 |
| 130 | + if [[ "$CHANNEL" == "stable" ]]; then |
| 131 | + # stable 构建:当前版本放 webapp/,尝试下载 beta |
| 132 | + mkdir -p docs/public/webapp |
| 133 | + cp -r dist-web/* docs/public/webapp/ |
| 134 | + echo "Copied current build to docs/public/webapp/ (stable)" |
| 135 | +
|
| 136 | + # 尝试下载最新的 beta 版本(从 latest prerelease) |
| 137 | + mkdir -p docs/public/webapp-beta |
| 138 | + if gh release download --pattern 'bfmpay-web-beta.zip' --dir /tmp -R ${{ github.repository }} 2>/dev/null; then |
| 139 | + unzip -q /tmp/bfmpay-web-beta.zip -d docs/public/webapp-beta/ |
| 140 | + echo "Downloaded beta version from release" |
| 141 | + else |
| 142 | + # 如果没有 beta release,使用当前构建 |
| 143 | + cp -r dist-web/* docs/public/webapp-beta/ |
| 144 | + echo "No beta release found, using current build for webapp-beta" |
| 145 | + fi |
| 146 | + else |
| 147 | + # beta 构建:当前版本放 webapp-beta/,尝试下载 stable |
| 148 | + mkdir -p docs/public/webapp-beta |
| 149 | + cp -r dist-web/* docs/public/webapp-beta/ |
| 150 | + echo "Copied current build to docs/public/webapp-beta/ (beta)" |
| 151 | +
|
| 152 | + # 尝试下载最新的 stable 版本 |
| 153 | + mkdir -p docs/public/webapp |
| 154 | + if gh release download --pattern 'bfmpay-web.zip' --dir /tmp -R ${{ github.repository }} 2>/dev/null; then |
| 155 | + unzip -q /tmp/bfmpay-web.zip -d docs/public/webapp/ |
| 156 | + echo "Downloaded stable version from release" |
| 157 | + else |
| 158 | + # 如果没有 stable release,使用当前构建 |
| 159 | + cp -r dist-web/* docs/public/webapp/ |
| 160 | + echo "No stable release found, using current build for webapp" |
| 161 | + fi |
| 162 | + fi |
| 163 | +
|
| 164 | + # 显示准备好的目录 |
| 165 | + echo "=== webapp directory ===" |
| 166 | + ls -la docs/public/webapp/ | head -5 |
| 167 | + echo "=== webapp-beta directory ===" |
| 168 | + ls -la docs/public/webapp-beta/ | head -5 |
| 169 | +
|
| 170 | + # ===== 构建 VitePress 站点 ===== |
| 171 | + - name: Build VitePress site |
| 172 | + run: pnpm docs:build |
| 173 | + |
| 174 | + # ===== 准备 GitHub Pages ===== |
| 175 | + - name: Prepare GitHub Pages |
| 176 | + run: | |
| 177 | + # VitePress 输出目录 |
| 178 | + mkdir -p gh-pages |
| 179 | + cp -r docs/.vitepress/dist/* gh-pages/ |
| 180 | +
|
| 181 | + # 禁用 Jekyll |
| 182 | + touch gh-pages/.nojekyll |
| 183 | +
|
| 184 | + echo "=== GitHub Pages structure ===" |
| 185 | + find gh-pages -maxdepth 2 -type d |
| 186 | +
|
| 187 | + # ===== 创建 Release 产物 ===== |
| 188 | + - name: Create release artifacts |
| 189 | + env: |
| 190 | + CHANNEL: ${{ steps.channel.outputs.channel }} |
| 191 | + run: | |
| 192 | + mkdir -p release |
| 193 | + VERSION="${{ steps.version.outputs.version }}" |
| 194 | +
|
| 195 | + if [[ "$CHANNEL" == "stable" ]]; then |
| 196 | + # Stable: 创建不带 beta 后缀的文件 |
| 197 | + cd dist-web && zip -r ../release/bfmpay-web.zip . && cd .. |
| 198 | + cp release/bfmpay-web.zip "release/bfmpay-web-${VERSION}.zip" |
| 199 | +
|
| 200 | + if [ -d "dists" ] && [ "$(ls -A dists)" ]; then |
| 201 | + cd dists && zip -r ../release/bfmpay-dweb.zip . && cd .. |
| 202 | + else |
| 203 | + cd dist-dweb && zip -r ../release/bfmpay-dweb.zip . && cd .. |
| 204 | + fi |
| 205 | + cp release/bfmpay-dweb.zip "release/bfmpay-dweb-${VERSION}.zip" |
| 206 | + else |
| 207 | + # Beta: 创建带 beta 后缀的文件 |
| 208 | + cd dist-web && zip -r ../release/bfmpay-web-beta.zip . && cd .. |
| 209 | + cp release/bfmpay-web-beta.zip "release/bfmpay-web-${VERSION}-beta.zip" |
| 210 | +
|
| 211 | + if [ -d "dists" ] && [ "$(ls -A dists)" ]; then |
| 212 | + cd dists && zip -r ../release/bfmpay-dweb-beta.zip . && cd .. |
| 213 | + else |
| 214 | + cd dist-dweb && zip -r ../release/bfmpay-dweb-beta.zip . && cd .. |
| 215 | + fi |
| 216 | + cp release/bfmpay-dweb-beta.zip "release/bfmpay-dweb-${VERSION}-beta.zip" |
| 217 | + fi |
| 218 | +
|
| 219 | + echo "=== Release artifacts ===" |
| 220 | + ls -la release/ |
| 221 | +
|
| 222 | + # ===== 上传构建产物 ===== |
| 223 | + - name: Upload GitHub Pages artifact |
| 224 | + uses: actions/upload-pages-artifact@v3 |
| 225 | + with: |
| 226 | + path: gh-pages |
| 227 | + |
| 228 | + - name: Upload release artifacts |
| 229 | + uses: actions/upload-artifact@v4 |
| 230 | + with: |
| 231 | + name: release-${{ steps.channel.outputs.channel }}-${{ steps.version.outputs.version }} |
| 232 | + path: release/ |
| 233 | + retention-days: 30 |
| 234 | + |
| 235 | + # ==================== 部署到 GitHub Pages ==================== |
| 236 | + deploy-pages: |
| 237 | + needs: build |
| 238 | + runs-on: ubuntu-latest |
| 239 | + |
| 240 | + environment: |
| 241 | + name: github-pages |
| 242 | + url: ${{ steps.deployment.outputs.page_url }} |
| 243 | + |
| 244 | + steps: |
| 245 | + - name: Deploy to GitHub Pages |
| 246 | + id: deployment |
| 247 | + uses: actions/deploy-pages@v4 |
| 248 | + |
| 249 | + # ==================== 创建 Release ==================== |
| 250 | + create-release: |
| 251 | + needs: build |
| 252 | + runs-on: ubuntu-latest |
| 253 | + |
| 254 | + steps: |
| 255 | + - name: Download release artifacts |
| 256 | + uses: actions/download-artifact@v4 |
| 257 | + with: |
| 258 | + name: release-${{ needs.build.outputs.channel }}-${{ needs.build.outputs.version }} |
| 259 | + path: release/ |
| 260 | + |
| 261 | + - name: Create or Update Release |
| 262 | + uses: softprops/action-gh-release@v2 |
| 263 | + with: |
| 264 | + tag_name: ${{ needs.build.outputs.channel == 'stable' && format('v{0}', needs.build.outputs.version) || 'beta' }} |
| 265 | + name: ${{ needs.build.outputs.channel == 'stable' && format('BFM Pay v{0}', needs.build.outputs.version) || 'BFM Pay Beta' }} |
| 266 | + body: | |
| 267 | + ## BFM Pay ${{ needs.build.outputs.channel == 'stable' && format('v{0}', needs.build.outputs.version) || 'Beta' }} |
| 268 | +
|
| 269 | + ### 下载 |
| 270 | + - **Web 版本**: `bfmpay-web${{ needs.build.outputs.channel == 'beta' && '-beta' || '' }}.zip` |
| 271 | + - **DWEB 版本**: `bfmpay-dweb${{ needs.build.outputs.channel == 'beta' && '-beta' || '' }}.zip` |
| 272 | +
|
| 273 | + ### 在线访问 |
| 274 | + - Web 应用 (stable): https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/webapp/ |
| 275 | + - Web 应用 (beta): https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/webapp-beta/ |
| 276 | + - 文档首页: https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/ |
| 277 | +
|
| 278 | + ### DWEB 安装 |
| 279 | + 在 DWEB 浏览器中打开以下链接安装: |
| 280 | + ``` |
| 281 | + dweb://install?url=https://github.com/${{ github.repository }}/releases/download/${{ needs.build.outputs.channel == 'stable' && format('v{0}', needs.build.outputs.version) || 'beta' }}/bfmpay-dweb${{ needs.build.outputs.channel == 'beta' && '-beta' || '' }}.zip |
| 282 | + ``` |
| 283 | + files: | |
| 284 | + release/* |
| 285 | + draft: false |
| 286 | + prerelease: ${{ needs.build.outputs.channel == 'beta' }} |
| 287 | + generate_release_notes: ${{ needs.build.outputs.channel == 'stable' }} |
0 commit comments