add preview staging sites to each opened PR using surge #2
This file contains 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
# Deploy pull requests to surge, so multiple previews can exist at once | |
# The deployments API is used to make the deployments visible in the pull request | |
# https://surge.sh/ | |
# to install surge: | |
# $ npm install --global surge | |
# to log in to surge or create an account, run | |
# $ surge | |
# to publish a folder as a static site, run | |
# $ surge --project <folder> --domain <example28928.surge.sh> | |
# or | |
# $ surge <folder> | |
# to remove a site, run | |
# $ surge teardown <example28928.surge.sh> | |
# to list active sites, run | |
# $ surge list | |
# this workflow requires two secrets: SURGE_LOGIN (the email you use above) and SURGE_TOKEN, generated with | |
# $ surge token | |
# note: the PDFs are not deployed, as they turn a ~60mb site into a ~600mb site. | |
# workflow modified from https://sanderknape.com/2020/05/deploy-pull-requests-github-actions-deployments/ | |
name: Deploy pull request preview to surge | |
on: | |
pull_request: | |
types: [opened, reopened, synchronize] | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: get pull request ref | |
id: get_pull_request_ref | |
uses: octokit/[email protected] | |
with: | |
route: GET /repos/:repository/pulls/:issue_id | |
repository: ${{ github.repository }} | |
issue_id: ${{ github.event.number }} | |
env: | |
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" | |
- name: create deployment | |
id: create_deployment | |
uses: octokit/[email protected] | |
with: | |
route: POST /repos/:repository/deployments | |
repository: ${{ github.repository }} | |
ref: ${{ fromJson(steps.get_pull_request_ref.outputs.data).head.ref }} | |
environment: dev | |
auto_merge: false | |
required_contexts: "[]" | |
env: | |
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" | |
- name: set deployment status to in progress | |
id: start_deployment | |
uses: octokit/[email protected] | |
with: | |
route: POST /repos/:repository/deployments/:deployment/statuses | |
repository: ${{ github.repository }} | |
deployment: ${{ fromJson(steps.create_deployment.outputs.data).id }} | |
environment: dev | |
log_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
state: in_progress | |
env: | |
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" | |
# deployment goes here | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- uses: actions/setup-node@v3 | |
with: | |
node-version: 20 | |
- name: Install dependencies | |
run: npm ci | |
- name: Build with Eleventy | |
run: npm run build | |
- name: Remove pdfs (filesize restraints) | |
run: rm _site/pdfs/*.pdf | |
- name: deploy to surge | |
run: npm run surge -- liputenpotest-${{ github.event.number }}.surge.sh | |
env: | |
SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }} # get with `surge token` | |
SURGE_LOGIN: ${{ secrets.SURGE_LOGIN }} # email used | |
- name: set deployment status to success | |
id: successful_deployment | |
uses: octokit/[email protected] | |
with: | |
route: POST /repos/:repository/deployments/:deployment/statuses | |
repository: ${{ github.repository }} | |
deployment: ${{ fromJson(steps.create_deployment.outputs.data).id }} | |
environment: dev | |
environment_url: https://liputenpotest-${{ github.event.number }}.surge.sh | |
log_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
state: success | |
env: | |
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" | |
- name: set deployment status to failure | |
id: failed_deployment | |
uses: octokit/[email protected] | |
if: failure() | |
with: | |
route: POST /repos/:repository/deployments/:deployment/statuses | |
repository: ${{ github.repository }} | |
deployment: ${{ fromJson(steps.create_deployment.outputs.data).id }} | |
environment: dev | |
log_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
state: failure | |
env: | |
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" |