Skip to content

Commit

Permalink
Support preview from forked repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Mar 27, 2024
1 parent a0c09ff commit 3df8438
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 72 deletions.
73 changes: 42 additions & 31 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: Surge.sh Preview

on:
pull_request_target:
# when using teardown: 'true', add default event types + closed event type
types: [opened, synchronize, reopened]
workflow_run:
workflows: [ "Publish website" ]
types:
- completed

defaults:
run:
Expand All @@ -15,47 +16,57 @@ concurrency:

jobs:
preview:
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
permissions:
pull-requests: write # allow surge-preview to create/update PR comments
pull-requests: write # Required to update PR status comment
steps:
- uses: actions/checkout@v4
- name: Download PR Artifact
uses: actions/download-artifact@v4
with:
ref: refs/pull/${{ github.event.pull_request.number }}/merge

- name: Cache node modules
id: cache-npm
uses: actions/cache@v3
env:
cache-name: cache-node-modules
with:
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
name: site
path: build/site

- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'

- name: Install packages
run: npm i

- name: Generate Site
run: npm run build

- uses: afc163/surge-preview@v1
id: preview_step
- name: Store PR id as variable
id: pr
run: |
echo "id=$(<pr-id.txt)" >> $GITHUB_OUTPUT
rm -f pr-id.txt
- name: Publishing to surge for preview
id: deploy
run: npx surge ./ --domain https://quarkiverse-docs-pr-${{ steps.pr.outputs.id }}-preview.surge.sh --token ${{ secrets.SURGE_TOKEN }}

- name: Update PR status comment on success
uses: actions-cool/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
body: |
🚀 PR Preview ${{ github.sha }} has been successfully built and deployed to https://quarkiverse-docs-pr-${{ steps.pr.outputs.id }}-preview.surge.sh
<!-- Sticky Pull Request Comment -->
body-include: '<!-- Sticky Pull Request Comment -->'
number: ${{ steps.pr.outputs.id }}

- name: Update PR status comment on failure
if: ${{ failure() }}
uses: actions-cool/[email protected]
with:
surge_token: ${{ secrets.SURGE_TOKEN }}
github_token: ${{ secrets.GITHUB_TOKEN }}
dist: build/site
failOnError: 'true'
build: |
echo Deploying to surge.sh
- name: Get the preview_url
run: echo "url => ${{ steps.preview_step.outputs.preview_url }}"
token: ${{ secrets.GITHUB_TOKEN }}
body: |
😭 Deploy PR Preview failed.
<img width="300" src="https://user-images.githubusercontent.com/507615/90250824-4e066700-de6f-11ea-8230-600ecc3d6a6b.png">
<!-- Sticky Pull Request Comment -->
body-include: '<!-- Sticky Pull Request Comment -->'
number: ${{ steps.pr.outputs.id }}
19 changes: 11 additions & 8 deletions .github/workflows/preview_teardown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ jobs:
preview:
runs-on: ubuntu-20.04
steps:
- uses: afc163/surge-preview@v1
id: preview_step
- name: Teardown surge preview
id: deploy
run: npx surge teardown https://quarkiverse-docs-pr-${{ github.event.number }}-preview.surge.sh --token ${{ secrets.SURGE_TOKEN }} || true
- name: Update PR status comment
uses: actions-cool/[email protected]
with:
surge_token: ${{ secrets.SURGE_TOKEN }}
github_token: ${{ secrets.GITHUB_TOKEN }}
teardown: 'true'
failOnError: 'false'
build: |
echo Undeploying from surge.sh
token: ${{ secrets.GITHUB_TOKEN }}
body: |
🙈 The PR is closed and the preview is expired.
<!-- Sticky Pull Request Comment -->
body-include: '<!-- Sticky Pull Request Comment -->'
number: ${{ github.event.number }}
64 changes: 31 additions & 33 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# This is a basic workflow to help you get started with Actions

name: Publish website

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
schedule:
- cron: '0 */6 * * *'
push:
branches: [ main ]
workflow_dispatch:
pull_request:
types: [ opened, synchronize, reopened ]

defaults:
run:
Expand All @@ -19,30 +17,12 @@ concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3

- name: Cache node modules
id: cache-npm
uses: actions/cache@v3
env:
cache-name: cache-node-modules
with:
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- uses: actions/checkout@v4

- name: Install Node.js
uses: actions/setup-node@v3
Expand All @@ -55,14 +35,32 @@ jobs:
- name: Generate Site
run: npm run build

- name: Publish to GitHub Pages
uses: crazy-max/ghaction-github-pages@v3
- name: Store PR id
run: echo ${{ github.event.number }} > ./public/pr-id.txt

- name: Publishing directory for PR preview
uses: actions/upload-artifact@v4
with:
name: site
path: build/site
retention-days: 1

deploy:
# Only try and deploy on merged code
if: "github.repository == 'quarkiverse/quarkiverse-docs' && github.ref_name == 'main' && (github.event_name == 'push' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch')"
needs: [ build ]
permissions:
contents: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4 # not needed for the code, but needed for the git config
- name: Download Built site
uses: actions/download-artifact@v4
with:
name: site
path: build/site

- name: Deploy
uses: JamesIves/github-pages-deploy-action@v4
with:
# Build directory to deploy
build_dir: build/site
# Do not allow Jekyll to build your site
jekyll: false
# Write the given domain name to the CNAME file
fqdn: docs.quarkiverse.io
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
folder: site # The folder the action should deploy.

0 comments on commit 3df8438

Please sign in to comment.