Skip to content

Updated the numbering that was still not showing up in my last update #457

Updated the numbering that was still not showing up in my last update

Updated the numbering that was still not showing up in my last update #457

name: Deploy Preview for Pull Requests # Name of this workflow, shown in the GitHub Actions UI
on:
pull_request: # Run this workflow whenever a Pull Request (PR) is made...
branches:
- main # ...only if the PR targets the 'main' branch
workflow_dispatch: # Allow manual trigger of workflow from "Actions" tab
inputs:
pr_number:
description: 'PR number (for manual testing)'
required: true
type: string
jobs:
build-and-deploy:
runs-on: ubuntu-latest # Use the latest Ubuntu virtual machine
steps:
- name: Checkout code
uses: actions/checkout@v3 # Step 1: Get (clone) the repository code.
- name: Set up Ruby # Step 2: Set up Ruby for the runner.
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.4 # Use Ruby version 3.4
bundler-cache: true # Cache Ruby gems for quicker builds
- name: Install dependencies # Step 3: Install all Ruby packages required for the site.
run: |
bundle install
- name: Build Jekyll site # Step 4: Build the site with Jekyll, and put it in '_site' directory.
env:
JEKYLL_BASEURL: /ols-site-preview/${{ github.event.pull_request.number || github.event.inputs.pr_number }}
run: |
echo "baseurl: $JEKYLL_BASEURL" > _config_preview.yml
bundle exec jekyll build --config _config.yml,_config_preview.yml --destination _site
# Step 5: Clone existing gh-pages branch. This preserves previously deployed PR previews.
- name: Clone existing gh-pages branch
run: |
git config --global user.name 'github-actions'
git config --global user.email 'github-actions@github.com'
git clone --depth 1 --branch gh-pages https://x-access-token:${{ secrets.PR_PREVIEW_TOKEN }}@github.com/open-life-science/ols-site-preview.git gh-pages
env:
GIT_TERMINAL_PROMPT: 0
# Step 6: Copy the built site into the PR folder, so every PR has its own preview.
- name: Copy built site into PR folder
run: |
pr_number=${{ github.event.pull_request.number || github.event.inputs.pr_number }}
mkdir -p gh-pages/$pr_number
rm -rf gh-pages/$pr_number/*
cp -r _site/* gh-pages/$pr_number/
- name: Push updated preview site
run: |
cd gh-pages
git add .
git commit -m "Deploy preview for PR #${{ github.event.pull_request.number || github.event.inputs.pr_number }}"
git push origin gh-pages
- name: Comment with preview URL
env:
GITHUB_TOKEN: ${{ secrets.PR_PREVIEW_TOKEN }} # Step 7a: Use token for authentication.
run: | # Step 7b: Create a preview URL using the PR number, post a comment with the preview link on this PR. If .md files were changed, also add direct links to those pages.
PREVIEW_URL="https://we-are-ols.org/ols-site-preview/${{ github.event.pull_request.number || github.event.inputs.pr_number }}"
PAGES_LIST=""
while IFS= read -r file; do
[ -z "$file" ] && continue
permalink=$(grep -m1 "^permalink:" "$file" 2>/dev/null | sed 's/permalink:[[:space:]]*//' | tr -d "\"' ")
if [ -n "$permalink" ]; then
page_url="${PREVIEW_URL}${permalink}"
elif [[ "$file" == _posts/* ]]; then
basename=$(basename "$file" .md)
year="${basename:0:4}"
month="${basename:5:2}"
day="${basename:8:2}"
slug="${basename:11}"
page_url="${PREVIEW_URL}/posts/${year}/${month}/${day}/${slug}/"
else
relative=$(echo "$file" | sed 's/index\.md$//' | sed 's/\.md$/.html/')
relative="${relative#/}"
[ -z "$relative" ] && continue # skip root index.md (would duplicate preview URL)
page_url="${PREVIEW_URL}/${relative}"
fi
PAGES_LIST="${PAGES_LIST}\n- [${file}](${page_url})"
done < <(gh pr diff ${{ github.event.pull_request.number || github.event.inputs.pr_number }} --name-only 2>/dev/null | grep '\.md$')
COMMENT_IDENTIFIER="<!-- preview-comment -->"
if [ -n "$PAGES_LIST" ]; then
COMMENT_BODY="$(printf '%s\n🎉 A preview of this PR is available at: [%s/](%s/)\n\n**Changed pages:**\n%b' "$COMMENT_IDENTIFIER" "$PREVIEW_URL" "$PREVIEW_URL" "$PAGES_LIST")"
else
COMMENT_BODY="$(printf '%s\n🎉 A preview of this PR is available at: [%s/](%s/)' "$COMMENT_IDENTIFIER" "$PREVIEW_URL" "$PREVIEW_URL")"
fi
PR_NUMBER="${{ github.event.pull_request.number || github.event.inputs.pr_number }}"
EXISTING_COMMENT_ID=$(gh api "repos/${{ github.repository }}/issues/${PR_NUMBER}/comments" \
--paginate \
--jq '.[] | select(.body | contains("<!-- preview-comment -->")) | .id' 2>/dev/null | head -1)
if [ -n "$EXISTING_COMMENT_ID" ]; then
gh api "repos/${{ github.repository }}/issues/comments/${EXISTING_COMMENT_ID}" \
-X PATCH \
-f body="$COMMENT_BODY"
else
gh pr comment "$PR_NUMBER" --body "$COMMENT_BODY"
fi