Skip to content

Commit 9158f7c

Browse files
committed
chore: move to pr-preview-action
1 parent 269b4b3 commit 9158f7c

7 files changed

Lines changed: 97 additions & 231 deletions

File tree

.github/act/pull_request.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"act": true,
33
"action": "synchronize",
44
"repository": {
5+
"fork": false,
6+
"default_branch": "main",
57
"name": "managing-innersource-projects",
68
"full_name": "InnerSourceCommons/managing-innersource-projects"
79
},

.github/act/push.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
{
2-
"act": true
3-
}
2+
"act": true,
3+
"repository": {
4+
"fork": false,
5+
"default_branch": "main"
6+
}
7+
}

.github/workflows/mdbook-pr-preview.yml

Lines changed: 0 additions & 116 deletions
This file was deleted.

.github/workflows/mdbook.yml

Lines changed: 82 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# Workflow for building and deploying a mdBook site to GitHub Pages.
2-
# Serves the main book at / and PR previews at /pr-<number>/ by merging
3-
# pr-* directories from the gh-pages branch (pushed by mdbook-pr-preview.yml)
4-
# into the deployed artifact. Works with Pages source "GitHub Actions".
2+
# - On push to main: deploys to the root of gh-pages.
3+
# - On pull_request: deploys a preview under pr-<number>/ on gh-pages,
4+
# comments the preview URL, and cleans up when the PR closes.
5+
#
6+
# Repo Settings required:
7+
# Pages -> Source: "Deploy from a branch" -> gh-pages / (root)
8+
# Actions -> General -> Workflow permissions: "Read and write permissions"
59
#
610
# See: https://rust-lang.github.io/mdBook/index.html
711
name: Deploy mdBook site to Pages
812

913
on:
1014
push:
1115
branches: ["main"]
16+
pull_request:
17+
types: [opened, synchronize, reopened, closed]
1218
workflow_dispatch:
1319

1420
permissions:
15-
contents: read
16-
pages: write
17-
id-token: write
18-
19-
concurrency:
20-
group: "pages"
21-
cancel-in-progress: false
21+
contents: write
22+
pull-requests: write
2223

2324
jobs:
24-
build:
25+
deploy:
26+
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
2527
runs-on: ubuntu-latest
28+
concurrency:
29+
group: pages
30+
cancel-in-progress: false
2631
env:
2732
CARGO_HOME: ${{ github.workspace }}/.cargo
2833
RUSTUP_HOME: ${{ github.workspace }}/.rustup
@@ -71,33 +76,73 @@ jobs:
7176
- name: Build with mdBook
7277
run: ${{ env.CARGO_HOME }}/bin/mdbook build
7378

74-
- name: Merge PR previews from gh-pages into artifact
75-
run: |
76-
git fetch origin gh-pages 2>/dev/null || true
77-
if git rev-parse -q origin/gh-pages >/dev/null 2>&1; then
78-
for d in $(git ls-tree -d --name-only origin/gh-pages 2>/dev/null | grep -E '^pr-[0-9]+$'); do
79-
git archive origin/gh-pages "$d" | tar -x -C book
80-
done
81-
fi
82-
83-
- name: Upload artifact
84-
if: ${{ !env.ACT }}
85-
uses: actions/upload-pages-artifact@v3
79+
- name: Deploy to GitHub Pages
80+
uses: peaceiris/actions-gh-pages@v4
8681
with:
87-
path: ./book
82+
github_token: ${{ secrets.GITHUB_TOKEN }}
83+
publish_dir: ./book
84+
keep_files: true
8885

89-
- name: Confirm local act build
90-
if: ${{ env.ACT }}
91-
run: echo "Local act run: mdBook build completed; skipping upload."
92-
93-
deploy:
94-
if: ${{ !env.ACT }}
95-
environment:
96-
name: github-pages
97-
url: ${{ steps.deployment.outputs.page_url }}
86+
preview:
87+
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
9888
runs-on: ubuntu-latest
99-
needs: build
89+
concurrency: preview-${{ github.ref }}
90+
env:
91+
CARGO_HOME: ${{ github.workspace }}/.cargo
92+
RUSTUP_HOME: ${{ github.workspace }}/.rustup
10093
steps:
101-
- name: Deploy to GitHub Pages
102-
id: deployment
103-
uses: actions/deploy-pages@v4
94+
- uses: actions/checkout@v4
95+
96+
- name: Cache Cargo registry and git index
97+
if: github.event.action != 'closed'
98+
uses: actions/cache@v4
99+
with:
100+
path: |
101+
${{ env.CARGO_HOME }}/registry
102+
${{ env.CARGO_HOME }}/git
103+
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('scripts/install-mdbook.sh') }}
104+
restore-keys: |
105+
${{ runner.os }}-cargo-registry-
106+
107+
- name: Cache Rust toolchains
108+
if: github.event.action != 'closed'
109+
uses: actions/cache@v4
110+
with:
111+
path: |
112+
${{ env.RUSTUP_HOME }}/toolchains
113+
${{ env.RUSTUP_HOME }}/update-hashes
114+
key: ${{ runner.os }}-rust-toolchain-${{ hashFiles('scripts/install-mdbook.sh') }}
115+
restore-keys: |
116+
${{ runner.os }}-rust-toolchain-
117+
118+
- name: Cache installed mdBook binaries
119+
if: github.event.action != 'closed'
120+
id: mdbook-cache
121+
uses: actions/cache@v4
122+
with:
123+
path: |
124+
${{ env.CARGO_HOME }}/bin
125+
key: ${{ runner.os }}-mdbook-bin-${{ hashFiles('scripts/install-mdbook.sh') }}
126+
restore-keys: |
127+
${{ runner.os }}-mdbook-bin-
128+
129+
- name: Install mdBook
130+
if: github.event.action != 'closed' && steps.mdbook-cache.outputs.cache-hit != 'true'
131+
run: bash scripts/install-mdbook.sh
132+
env:
133+
REPO_ROOT: ${{ github.workspace }}
134+
135+
- name: Add Cargo bin to PATH
136+
if: github.event.action != 'closed'
137+
run: echo "${CARGO_HOME}/bin" >> "$GITHUB_PATH"
138+
139+
- name: Build with mdBook
140+
if: github.event.action != 'closed'
141+
run: ${{ env.CARGO_HOME }}/bin/mdbook build
142+
143+
- name: Deploy PR preview
144+
uses: rossjrw/pr-preview-action@v1
145+
with:
146+
source-dir: ./book
147+
preview-branch: gh-pages
148+
umbrella-dir: pr-preview

.github/workflows/mdbook/cleanup-preview.sh

Lines changed: 0 additions & 20 deletions
This file was deleted.

.github/workflows/mdbook/comment-preview-url.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

scripts/test-mdbook-deploy.sh

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
set -euo pipefail
55

66
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
7-
MAIN_WORKFLOW_PATH="${REPO_ROOT}/.github/workflows/mdbook.yml"
8-
PREVIEW_WORKFLOW_PATH="${REPO_ROOT}/.github/workflows/mdbook-pr-preview.yml"
7+
WORKFLOW_PATH="${REPO_ROOT}/.github/workflows/mdbook.yml"
98
PUSH_EVENT_PATH="${REPO_ROOT}/.github/act/push.json"
109
PULL_REQUEST_EVENT_PATH="${REPO_ROOT}/.github/act/pull_request.json"
1110

@@ -14,13 +13,8 @@ if ! command -v act >/dev/null 2>&1; then
1413
exit 1
1514
fi
1615

17-
if [[ ! -f "${MAIN_WORKFLOW_PATH}" ]]; then
18-
echo "Error: workflow file not found at ${MAIN_WORKFLOW_PATH}" >&2
19-
exit 1
20-
fi
21-
22-
if [[ ! -f "${PREVIEW_WORKFLOW_PATH}" ]]; then
23-
echo "Error: workflow file not found at ${PREVIEW_WORKFLOW_PATH}" >&2
16+
if [[ ! -f "${WORKFLOW_PATH}" ]]; then
17+
echo "Error: workflow file not found at ${WORKFLOW_PATH}" >&2
2418
exit 1
2519
fi
2620

@@ -36,8 +30,8 @@ fi
3630

3731
cd "${REPO_ROOT}"
3832

39-
echo "Running main Pages workflow (push): ${MAIN_WORKFLOW_PATH}"
40-
act push -W "${MAIN_WORKFLOW_PATH}" -e "${PUSH_EVENT_PATH}" "$@"
33+
echo "Running deploy job (push): ${WORKFLOW_PATH}"
34+
act push -W "${WORKFLOW_PATH}" -e "${PUSH_EVENT_PATH}" "$@"
4135

42-
echo "Running PR preview workflow (pull_request): ${PREVIEW_WORKFLOW_PATH}"
43-
act pull_request -W "${PREVIEW_WORKFLOW_PATH}" -e "${PULL_REQUEST_EVENT_PATH}" "$@"
36+
echo "Running deploy-preview job (pull_request): ${WORKFLOW_PATH}"
37+
act pull_request -W "${WORKFLOW_PATH}" -e "${PULL_REQUEST_EVENT_PATH}" "$@"

0 commit comments

Comments
 (0)