Skip to content

Commit 3028364

Browse files
authored
Merge pull request open-life-science#1007 from open-life-science/pr_preview
Improve pr-preview action to accomodate previews for multiple open PRs
2 parents 5dd77d0 + 14cb699 commit 3028364

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

.github/workflows/deploy-preview.yml

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,58 @@ on:
44
pull_request: # Run this workflow whenever a Pull Request (PR) is made...
55
branches:
66
- main # ...only if the PR targets the 'main' branch
7+
workflow_dispatch: # Allow manual trigger of workflow from "Actions" tab
78

89
jobs:
910
build-and-deploy:
1011
runs-on: ubuntu-latest # Use the latest Ubuntu virtual machine
1112

1213
steps:
1314
- name: Checkout code
14-
uses: actions/checkout@v3 # Step 1: Get (clone) the repository code
15+
uses: actions/checkout@v3 # Step 1: Get (clone) the repository code.
1516

16-
- name: Set up Ruby # Step 2: Set up Ruby for the runner
17+
- name: Set up Ruby # Step 2: Set up Ruby for the runner.
1718
uses: ruby/setup-ruby@v1
1819
with:
1920
ruby-version: 3.1 # Use Ruby version 3.1
2021
bundler-cache: true # Cache Ruby gems for quicker builds
2122

22-
- name: Install dependencies # Step 3: Install all Ruby packages required for the site
23+
- name: Install dependencies # Step 3: Install all Ruby packages required for the site.
2324
run: |
2425
bundle install
2526
26-
- name: Build Jekyll site # Step 4: Build the site with Jekyll, and put it in '_site' directory
27+
- name: Build Jekyll site # Step 4: Build the site with Jekyll, and put it in '_site' directory.
2728
run: |
2829
bundle exec jekyll build --destination _site
2930
30-
# Step 5: Make a folder called 'pr_preview'; move the built site into a subfolder labelled with the PR number
31-
- name: Copy site to PR-specific folder
31+
# Step 5: Clone existing gh-pages branch. This preserves previously deployed PR previews.
32+
- name: Clone existing gh-pages branch
3233
run: |
33-
mkdir pr_preview
34-
mv _site pr_preview/${{ github.event.pull_request.number }}
34+
git config --global user.name 'github-actions'
35+
git config --global user.email 'github-actions@github.com'
36+
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
37+
env:
38+
GIT_TERMINAL_PROMPT: 0
39+
40+
# Step 6: Copy the built site into the PR folder, so every PR has its own preview.
41+
- name: Copy built site into PR folder
42+
run: |
43+
pr_number=${{ github.event.pull_request.number }}
44+
mkdir -p gh-pages/$pr_number
45+
rm -rf gh-pages/$pr_number/*
46+
cp -r _site/* gh-pages/$pr_number/
3547
36-
- name: Deploy to GitHub Pages for preview
37-
uses: peaceiris/actions-gh-pages@v3 # Step 6: Use a pre-made action to deploy to GitHub Pages
38-
with:
39-
personal_token: ${{ secrets.PR_PREVIEW_TOKEN }} # GitHub token (from secrets) to authenticate this action and publish a preview.
40-
publish_branch: gh-pages # Publish to 'gh-pages' branch
41-
publish_dir: ./pr_preview # Use 'pr_preview' folder as the root for published files
42-
external_repository: open-life-science/ols-site-preview # Publish to external repo
43-
cname: ''
48+
- name: Push updated preview site
49+
run: |
50+
cd gh-pages
51+
git add .
52+
git commit -m "Deploy preview for PR #${{ github.event.pull_request.number }}"
53+
git push origin gh-pages
4454
4555
- name: Comment with preview URL
4656
env:
47-
GITHUB_TOKEN: ${{ secrets.PR_PREVIEW_TOKEN }} # Step 7a: Use token for authentication
48-
run: | # Step 7b: Create a preview URL using the PR number, post a comment with the preview link on this PR
49-
PREVIEW_URL="https://we-are-ols.org/ols-site-preview/${{ github.event.pull_request.number }}"
57+
GITHUB_TOKEN: ${{ secrets.PR_PREVIEW_TOKEN }} # Step 7a: Use token for authentication.
58+
run: | # Step 7b: Create a preview URL using the PR number, post a comment with the preview link on this PR.
59+
PREVIEW_URL="https://we-are-ols.org/ols-site-preview/${{ github.event.pull_request.number }}/"
5060
COMMENT_BODY="🎉 A preview of this PR is available at: [${PREVIEW_URL}](${PREVIEW_URL})"
5161
gh pr comment ${{ github.event.pull_request.number }} --body "$COMMENT_BODY"

0 commit comments

Comments
 (0)