|
| 1 | +name: PR Preview (Surge) |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, reopened, synchronize] |
| 6 | + pull_request_target: |
| 7 | + types: [closed] # for optional teardown on close (see job at bottom) |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + |
| 12 | +env: |
| 13 | + # Customize the base if you want (must be a valid, available Surge domain) |
| 14 | + SURGE_DOMAIN_BASE: ${{ github.event.repository.name }} |
| 15 | + |
| 16 | +jobs: |
| 17 | + preview: |
| 18 | + if: github.event_name == 'pull_request' |
| 19 | + name: Build & Deploy PR Preview |
| 20 | + runs-on: ubuntu-latest |
| 21 | + defaults: |
| 22 | + run: |
| 23 | + working-directory: ./website |
| 24 | + steps: |
| 25 | + - name: Checkout |
| 26 | + uses: actions/checkout@v4 |
| 27 | + |
| 28 | + - name: Use Node.js |
| 29 | + uses: actions/setup-node@v4 |
| 30 | + with: |
| 31 | + node-version: 22 |
| 32 | + cache: yarn |
| 33 | + cache-dependency-path: website/yarn.lock |
| 34 | + |
| 35 | + - name: Install deps |
| 36 | + run: yarn install --frozen-lockfile |
| 37 | + |
| 38 | + - name: Build |
| 39 | + run: yarn build |
| 40 | + |
| 41 | + - name: Install Surge CLI |
| 42 | + run: npm i -g surge |
| 43 | + |
| 44 | + - name: Compute preview domain |
| 45 | + id: domain |
| 46 | + run: | |
| 47 | + dom="${{ env.SURGE_DOMAIN_BASE }}-pr-${{ github.event.number }}.surge.sh" |
| 48 | + echo "domain=$dom" >> $GITHUB_OUTPUT |
| 49 | +
|
| 50 | + - name: Deploy to Surge |
| 51 | + env: |
| 52 | + SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }} |
| 53 | + run: | |
| 54 | + surge --project ./website/build \ |
| 55 | + --domain ${{ steps.domain.outputs.domain }} \ |
| 56 | + --token "$SURGE_TOKEN" |
| 57 | +
|
| 58 | + - name: Comment with preview URL |
| 59 | + uses: actions/github-script@v7 |
| 60 | + with: |
| 61 | + script: | |
| 62 | + const domain = '${{ steps.domain.outputs.domain }}' |
| 63 | + const body = `🚀 Preview deployed to **https://${domain}**` |
| 64 | + github.rest.issues.createComment({ |
| 65 | + owner: context.repo.owner, |
| 66 | + repo: context.repo.repo, |
| 67 | + issue_number: context.issue.number, |
| 68 | + body |
| 69 | + }) |
| 70 | +
|
| 71 | + teardown: |
| 72 | + # Optional: remove preview when PR is closed |
| 73 | + if: github.event_name == 'pull_request_target' && github.event.action == 'closed' |
| 74 | + name: Teardown Surge preview |
| 75 | + runs-on: ubuntu-latest |
| 76 | + steps: |
| 77 | + - name: Install Surge CLI |
| 78 | + run: npm i -g surge |
| 79 | + - name: Teardown |
| 80 | + env: |
| 81 | + SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }} |
| 82 | + run: | |
| 83 | + dom="${{ github.event.repository.name }}-pr-${{ github.event.number }}.surge.sh" |
| 84 | + surge teardown "$dom" --token "$SURGE_TOKEN" || true |
0 commit comments