Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions .github/workflows/pr-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: PR Preview
# Builds and deploys a preview of the blog site for every PR targeting main.

on:
pull_request:
types:
- opened
- synchronize
- reopened
- ready_for_review
branches:
- main

permissions:
contents: write
pull-requests: write

jobs:
build-preview:
name: Build & Deploy PR Preview
runs-on: ubuntu-latest
container:
image: "ghcr.io/pharmaverse/docker_pharmaverse:4.5.1"
steps:
- name: Check out repository
uses: actions/checkout@v4

- name: Configure Git safe directory
run: git config --global --add safe.directory /__w/blog/blog

- name: Set up Quarto
uses: quarto-dev/quarto-actions/setup@v2
with:
version: 1.9.12

- name: Install R Packages
run: |
R -e "install.packages(c(
'admiral',
'diffdf',
'patchwork',
'here',
'filters',
'janitor',
'link',
'sessioninfo',
'renv',
'riskmetric',
'sdtm.oak',
'mirai',
'dverse',
'autoslider.core'))"

- name: Install tinytex
run: quarto install tool tinytex

- name: Sanitize branch name for use as directory/URL slug
id: branch
run: |
SAFE_BRANCH=$(echo "${{ github.head_ref }}" | sed 's|[^a-zA-Z0-9._-]|-|g')
echo "safe=${SAFE_BRANCH}" >> "$GITHUB_OUTPUT"
shell: bash

- name: Render Quarto Project
uses: quarto-dev/quarto-actions/render@v2
with:
to: html

- name: Publish PR preview to gh-pages subdirectory
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./_site
destination_dir: ${{ steps.branch.outputs.safe }}

- name: Post preview URL comment on PR
uses: marocchino/sticky-pull-request-comment@v2
with:
header: "PR Preview"
message: |
:eyes: **Preview available!**

The preview website for this PR is available at:
https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/${{ steps.branch.outputs.safe }}

_The preview is updated every time a new commit is pushed to this PR._
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
80 changes: 80 additions & 0 deletions .github/workflows/remove-pr-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: Remove PR Preview

on:
pull_request:
types:
- closed
branches:
- main

permissions:
contents: write
pull-requests: write

jobs:
delete-preview:
name: Remove PR Preview from gh-pages
runs-on: ubuntu-latest
env:
BRANCH: ${{ github.event.pull_request.head.ref }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
persist-credentials: true

- name: Sanitize branch name for use as directory slug
id: branch
run: |
SAFE_BRANCH=$(echo "${BRANCH}" | sed 's|[^a-zA-Z0-9._-]|-|g')
echo "safe=${SAFE_BRANCH}" >> "$GITHUB_OUTPUT"
shell: bash

- name: Ensure gh-pages exists and check it out
run: |
set -euo pipefail
if git ls-remote --heads origin gh-pages | grep -q 'refs/heads/gh-pages'; then
git fetch origin gh-pages:gh-pages
git checkout gh-pages
else
echo "gh-pages branch not found. Nothing to do."
exit 0
fi
shell: bash

- name: Remove preview subdirectory for closed PR
run: |
set -euo pipefail
SUBDIR="${{ steps.branch.outputs.safe }}"
echo "SUBDIR: ${SUBDIR}"
if [ -z "$SUBDIR" ]; then
echo "Branch name empty. Nothing to do."
exit 0
fi

if [ -d "$SUBDIR" ]; then
git rm -r --ignore-unmatch -- "$SUBDIR"
if [ -n "$(git status --porcelain)" ]; then
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git commit -m "ci: remove gh-pages preview for closed PR branch ${{ steps.branch.outputs.safe }}"
git push origin gh-pages
else
echo "No changes to commit after removal."
fi
else
echo "Subfolder '$SUBDIR' does not exist on gh-pages. Nothing to do."
fi
shell: bash

- name: Update PR comment to reflect removal
uses: marocchino/sticky-pull-request-comment@v2
with:
header: "PR Preview"
message: |
:x: **Preview removed.**

This PR has been closed and the preview website has been removed.
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Loading