|
4 | 4 | pull_request: # Run this workflow whenever a Pull Request (PR) is made... |
5 | 5 | branches: |
6 | 6 | - main # ...only if the PR targets the 'main' branch |
| 7 | + workflow_dispatch: # Allow manual trigger of workflow from "Actions" tab |
7 | 8 |
|
8 | 9 | jobs: |
9 | 10 | build-and-deploy: |
10 | 11 | runs-on: ubuntu-latest # Use the latest Ubuntu virtual machine |
11 | 12 |
|
12 | 13 | steps: |
13 | 14 | - 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. |
15 | 16 |
|
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. |
17 | 18 | uses: ruby/setup-ruby@v1 |
18 | 19 | with: |
19 | 20 | ruby-version: 3.1 # Use Ruby version 3.1 |
20 | 21 | bundler-cache: true # Cache Ruby gems for quicker builds |
21 | 22 |
|
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. |
23 | 24 | run: | |
24 | 25 | bundle install |
25 | 26 |
|
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. |
27 | 28 | run: | |
28 | 29 | bundle exec jekyll build --destination _site |
29 | 30 |
|
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 |
32 | 33 | 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/ |
35 | 47 |
|
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 |
44 | 54 |
|
45 | 55 | - name: Comment with preview URL |
46 | 56 | 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 }}/" |
50 | 60 | COMMENT_BODY="🎉 A preview of this PR is available at: [${PREVIEW_URL}](${PREVIEW_URL})" |
51 | 61 | gh pr comment ${{ github.event.pull_request.number }} --body "$COMMENT_BODY" |
0 commit comments