test-build-preview #13
Workflow file for this run
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 Transcripts Preview Site | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| issues: write | |
| on: | |
| pull_request_target: | |
| paths: | |
| - '**' | |
| workflow_dispatch: | |
| issue_comment: | |
| types: [created] | |
| jobs: | |
| build-preview: | |
| runs-on: ubuntu-latest | |
| if: | | |
| github.event_name == 'pull_request_target' || | |
| github.event_name == 'workflow_dispatch' || | |
| (github.event_name == 'issue_comment' && contains(github.event.comment.body, '/preview')) | |
| environment: ${{ (github.event_name == 'pull_request_target' && github.event.pull_request.head.repo.full_name != github.repository) && 'external-fork-preview' || '' }} | |
| steps: | |
| - name: Get PR details for comment trigger | |
| id: pr-details | |
| if: github.event_name == 'issue_comment' | |
| uses: actions/github-script@v7 | |
| with: | |
| result-encoding: string | |
| script: | | |
| const pr = await github.rest.pulls.get({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: context.issue.number | |
| }); | |
| return JSON.stringify({ | |
| head_sha: pr.data.head.sha, | |
| head_ref: pr.data.head.ref, | |
| head_repo_fullname: pr.data.head.repo.full_name | |
| }); | |
| - name: Setup Checkout for Bitcoin Transcripts (PR Target) | |
| if: github.event_name == 'pull_request_target' | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: ${{ github.event.pull_request.head.repo.full_name }} | |
| ref: ${{ github.event.pull_request.head.sha }} | |
| path: transcripts | |
| # Fork PR code only runs here after the `external-fork-preview` environment's | |
| # required reviewers approve the job, so the checkout is already gated. | |
| # See https://gh.io/securely-using-pull_request_target | |
| allow-unsafe-pr-checkout: true | |
| - name: Setup Checkout for Bitcoin Transcripts (Comment Trigger) | |
| if: github.event_name == 'issue_comment' | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: ${{ fromJson(steps.pr-details.outputs.result).head_repo_fullname }} | |
| ref: ${{ fromJson(steps.pr-details.outputs.result).head_sha }} | |
| path: transcripts | |
| - name: Setup Checkout for Bitcoin Transcripts (Workflow Dispatch) | |
| if: github.event_name == 'workflow_dispatch' | |
| uses: actions/checkout@v4 | |
| with: | |
| path: transcripts | |
| - name: Setup Node.js 20 | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| - name: Clone Registry Repo | |
| run: git clone https://github.com/bitcointranscripts/registry.git | |
| - name: Replace the public/bitcoin-transcript submodule in Registry with contents of this current branch | |
| working-directory: transcripts | |
| run: | | |
| rm -rf ../registry/public/bitcoin-transcript | |
| mkdir -p ../registry/public/bitcoin-transcript | |
| cp -r * ../registry/public/bitcoin-transcript/ | |
| - name: Install dependencies | |
| working-directory: registry | |
| run: | | |
| npm install | |
| - name: Run Next.js build only | |
| working-directory: registry | |
| env: | |
| API_KEY: ${{ secrets.API_KEY }} | |
| CLOUD_ID: ${{ secrets.CLOUD_ID }} | |
| INDEX: ${{ secrets.INDEX }} | |
| run: | | |
| npm run fetch-topics | |
| npm run fetch-reviewers | |
| npx next build | |
| - name: Install Vercel CLI | |
| run: npm install -g vercel | |
| - name: Add vercel.json with custom build-only command | |
| working-directory: registry | |
| run: | | |
| cat <<EOF > vercel.json | |
| { | |
| "buildCommand": "npm run fetch-topics && npm run fetch-reviewers && npx next build" | |
| } | |
| EOF | |
| - name: Pull Vercel project settings | |
| working-directory: registry | |
| env: | |
| VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} | |
| run: vercel pull --yes --token=$VERCEL_TOKEN | |
| - name: Build with Vercel CLI | |
| working-directory: registry | |
| env: | |
| GITHUB_ACCESS_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }} | |
| VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} | |
| API_KEY: ${{ secrets.API_KEY }} | |
| CLOUD_ID: ${{ secrets.CLOUD_ID }} | |
| INDEX: ${{ secrets.INDEX }} | |
| run: vercel build --token=$VERCEL_TOKEN | |
| - name: Deploy to Vercel | |
| working-directory: registry | |
| env: | |
| VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} | |
| run: | | |
| vercel deploy --prebuilt --token=$VERCEL_TOKEN --archive=tgz --yes > ../vercel-preview-url.txt | |
| - name: Store Top-Level Folders | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const prNumber = context.issue?.number || context.payload.pull_request?.number; | |
| if (!prNumber) { | |
| fs.writeFileSync('top_level_folders.txt', ''); | |
| return; | |
| } | |
| const files = await github.rest.pulls.listFiles({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: prNumber, | |
| per_page: 100 | |
| }); | |
| const names = files.data.map(f => f.filename).join(' '); | |
| fs.writeFileSync('top_level_folders.txt', names); | |
| - name: Comment PR with Preview URL | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const url = fs.readFileSync('vercel-preview-url.txt', 'utf8').trim(); | |
| const top_level_folders = fs.readFileSync('top_level_folders.txt', 'utf8').trim(); | |
| const dirs = top_level_folders.split(' '); | |
| let commentBody = `Your Transcript Preview is ready:\n`; | |
| if (dirs.length >= 1) { | |
| for (const dir of dirs) { | |
| if(dir.includes(".md")) { | |
| let dir_name = dir.replace(".md", ""); | |
| commentBody += `- [${dir_name}](${url}/${dir_name})\n`; | |
| } | |
| } | |
| commentBody += `- [View All Transcript Sources](${url}/sources)\n`; | |
| } | |
| const prNumber = context.issue?.number || context.payload.pull_request?.number; | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| issue_number: prNumber, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| }); | |
| const existing = comments.find(c => c.body.startsWith('Your Transcript Preview is ready')); | |
| if (existing) { | |
| await github.rest.issues.updateComment({ | |
| comment_id: existing.id, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: commentBody | |
| }); | |
| } else { | |
| await github.rest.issues.createComment({ | |
| issue_number: prNumber, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: commentBody | |
| }); | |
| } |