Skip to content

trigger build

trigger build #3

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
environment: ${{ (github.event_name == 'pull_request_target' && github.event.pull_request.head.repo.full_name != github.repository) && 'external-fork-preview' || '' }}
if: |
github.event_name == 'workflow_dispatch' ||
github.event_name == 'pull_request_target' ||
(github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
contains(github.event.comment.body, '/build-preview') &&
(github.event.comment.author_association == 'OWNER' ||
github.event.comment.author_association == 'MEMBER' ||
github.event.comment.author_association == 'COLLABORATOR'))
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
- 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
working-directory: transcripts
run: |
git remote add upstream https://github.com/bitcointranscripts/bitcointranscripts.git
git fetch upstream
top_level_folders=$(git diff --name-only upstream/master)
echo $top_level_folders > ../top_level_folders.txt
- 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;
github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
});