|
| 1 | +name: 'CI: Preview' |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + pr-number: |
| 7 | + description: 'Pull Request number' |
| 8 | + type: number |
| 9 | + required: true |
| 10 | + |
| 11 | +jobs: |
| 12 | + preview-job: |
| 13 | + name: preview |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Checkout |
| 17 | + uses: actions/checkout@v4 |
| 18 | + |
| 19 | + - name: Setup pnpm |
| 20 | + uses: pnpm/action-setup@v4 |
| 21 | + |
| 22 | + - name: Setup Node.js |
| 23 | + uses: actions/setup-node@v4 |
| 24 | + with: |
| 25 | + node-version: 20 |
| 26 | + registry-url: 'https://registry.npmjs.org' |
| 27 | + |
| 28 | + - name: Get pnpm store directory |
| 29 | + id: pnpm-cache |
| 30 | + run: echo "pnpm_cache_dir=$(pnpm store path)" >> $GITHUB_OUTPUT |
| 31 | + |
| 32 | + - name: Setup pnpm cache |
| 33 | + uses: actions/cache@v4 |
| 34 | + with: |
| 35 | + path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }} |
| 36 | + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} |
| 37 | + restore-keys: | |
| 38 | + ${{ runner.os }}-pnpm-store- |
| 39 | +
|
| 40 | + - name: Install dependencies |
| 41 | + run: pnpm i --no-frozen-lockfile |
| 42 | + |
| 43 | + - name: Download build artifacts |
| 44 | + uses: actions/download-artifact@v4 |
| 45 | + with: |
| 46 | + name: build-${{ github.sha }} |
| 47 | + path: packages |
| 48 | + |
| 49 | + - name: Build docs only (components already built) |
| 50 | + run: pnpm -F docs build |
| 51 | + |
| 52 | + - name: Comment build generating |
| 53 | + if: github.event_name == 'pull_request' |
| 54 | + uses: actions-cool/maintain-one-comment@v3.1.1 |
| 55 | + with: |
| 56 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 57 | + body: | |
| 58 | + [<img width="400" src="https://github.com/user-attachments/assets/c25bbbe4-0cf5-4aca-b4b3-db49e7a264d9">](#) |
| 59 | + |
| 60 | + 🔨 Building packages... |
| 61 | + <!-- TINY_ROBOT_BUILD --> |
| 62 | + body-include: '<!-- TINY_ROBOT_BUILD -->' |
| 63 | + number: ${{ inputs.pr-number }} |
| 64 | + |
| 65 | + - name: Publish to pkg.pr.new |
| 66 | + id: pkg-pr-new |
| 67 | + run: | |
| 68 | + npx pkg-pr-new publish \ |
| 69 | + ./packages/components \ |
| 70 | + ./packages/kit \ |
| 71 | + ./packages/svgs \ |
| 72 | + --pnpm \ |
| 73 | + --json output.json \ |
| 74 | + --comment=off |
| 75 | +
|
| 76 | + - name: Post or update pkg.pr.new comment |
| 77 | + if: github.event_name == 'pull_request' |
| 78 | + uses: actions/github-script@v7 |
| 79 | + with: |
| 80 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 81 | + script: | |
| 82 | + const fs = require('fs'); |
| 83 | + const output = JSON.parse(fs.readFileSync('output.json', 'utf8')); |
| 84 | + |
| 85 | + const sha = context.payload.pull_request.head.sha.substring(0, 7); |
| 86 | + |
| 87 | + const packages = output.packages.map((p) => { |
| 88 | + const shortUrl = p.url.replace(/@[a-f0-9]{40}/, '@' + sha); |
| 89 | + return 'pnpm add ' + shortUrl; |
| 90 | + }).join('\n\n'); |
| 91 | + |
| 92 | + const commitUrl = 'https://github.com/' + context.repo.owner + '/' + context.repo.repo + '/commit/' + sha; |
| 93 | + |
| 94 | + const body = '## 📦 Package Preview\n\n' + packages + '\n\n**commit:** [' + sha + '](' + commitUrl + ')\n\n<!-- PKG_PR_NEW_COMMENT -->'; |
| 95 | + const botCommentIdentifier = '<!-- PKG_PR_NEW_COMMENT -->'; |
| 96 | + |
| 97 | + const comments = await github.rest.issues.listComments({ |
| 98 | + owner: context.repo.owner, |
| 99 | + repo: context.repo.repo, |
| 100 | + issue_number: context.issue.number, |
| 101 | + }); |
| 102 | + |
| 103 | + const existingComment = comments.data.find((comment) => |
| 104 | + comment.body.includes(botCommentIdentifier) |
| 105 | + ); |
| 106 | + |
| 107 | + if (existingComment) { |
| 108 | + await github.rest.issues.updateComment({ |
| 109 | + owner: context.repo.owner, |
| 110 | + repo: context.repo.repo, |
| 111 | + comment_id: existingComment.id, |
| 112 | + body: body, |
| 113 | + }); |
| 114 | + } else { |
| 115 | + await github.rest.issues.createComment({ |
| 116 | + issue_number: context.issue.number, |
| 117 | + owner: context.repo.owner, |
| 118 | + repo: context.repo.repo, |
| 119 | + body: body, |
| 120 | + }); |
| 121 | + } |
| 122 | +
|
| 123 | + - name: Deploy Site to Surge |
| 124 | + id: deploy |
| 125 | + run: | |
| 126 | + DEPLOY_DOMAIN=preview-${{ inputs.pr-number }}-tiny-robot.surge.sh |
| 127 | + echo "Deploying to: https://$DEPLOY_DOMAIN" |
| 128 | + npx surge --project ./docs/dist --domain $DEPLOY_DOMAIN --token $SURGE_TOKEN |
| 129 | + echo "url=https://$DEPLOY_DOMAIN" >> $GITHUB_OUTPUT |
| 130 | + env: |
| 131 | + SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }} |
| 132 | + |
| 133 | + - name: Comment build success |
| 134 | + if: ${{ success() && github.event_name == 'pull_request' }} |
| 135 | + uses: actions-cool/maintain-one-comment@v3.1.1 |
| 136 | + with: |
| 137 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 138 | + body: | |
| 139 | + [<img width="400" src="https://github.com/user-attachments/assets/2a0bb639-dfaf-4384-8d73-cec616ea4b97">](https://preview-${{ inputs.pr-number }}-tiny-robot.surge.sh) |
| 140 | + |
| 141 | + ✅ Preview build completed successfully! |
| 142 | + |
| 143 | + > Click the image above to preview. |
| 144 | + > Preview will be automatically removed when this PR is closed. |
| 145 | + <!-- TINY_ROBOT_BUILD --> |
| 146 | + body-include: '<!-- TINY_ROBOT_BUILD -->' |
| 147 | + number: ${{ inputs.pr-number }} |
| 148 | + |
| 149 | + - name: Comment build failed |
| 150 | + if: ${{ failure() && github.event_name == 'pull_request' }} |
| 151 | + uses: actions-cool/maintain-one-comment@v3.1.1 |
| 152 | + with: |
| 153 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 154 | + body: | |
| 155 | + [<img width="400" src="https://github.com/user-attachments/assets/acfe799d-40be-4ffb-8190-da33f84056dc">](#) |
| 156 | + |
| 157 | + ❌ Build failed. Please check the logs for details. |
| 158 | + <!-- TINY_ROBOT_BUILD --> |
| 159 | + body-include: '<!-- TINY_ROBOT_BUILD -->' |
| 160 | + number: ${{ inputs.pr-number }} |
0 commit comments