|
1 | | -# This GitHub Action deploys documentation previews for pull requests to the gh-pages branch. |
| 1 | +# This GitHub Action prepares documentation for deployment to GitHub Pages. |
2 | 2 | # |
3 | | -# When triggered during a PR build, it uploads the built documentation from the specified directory |
4 | | -# to a unique subdirectory (pr-preview/pr-<PR_NUMBER>) under the gh-pages branch. |
5 | | -# This allows contributors and reviewers to view preview documentation for the PR without merging it. |
6 | | -# The deployment is performed using the provided GitHub token for authentication, and is safe to run |
7 | | -# concurrently for multiple PRs. If the gh-pages branch does not exist, it is initialized automatically. |
8 | 3 |
|
9 | | -name: 'Deploy Docs Preview' |
10 | | -description: 'Deploy documentation preview to gh-pages branch' |
| 4 | +name: 'Prepare Docs for Pages' |
| 5 | +description: 'Prepare documentation for GitHub Pages deployment' |
11 | 6 |
|
12 | 7 | inputs: |
13 | | - github_token: |
14 | | - description: 'GitHub token for authentication' |
| 8 | + docs_dir: |
| 9 | + description: 'Path to the directory containing built documentation' |
15 | 10 | required: true |
16 | | - repository: |
17 | | - description: 'Repository in owner/repo format' |
| 11 | + target_path: |
| 12 | + description: 'Target path within the site (e.g., "pr-preview/pr-123" or "latest")' |
18 | 13 | required: true |
19 | | - pr_number: |
20 | | - description: 'Pull request number' |
21 | | - required: true |
22 | | - commit_sha: |
23 | | - description: 'Commit SHA for the PR head' |
24 | | - required: true |
25 | | - docs_build_dir: |
26 | | - description: 'Path to the built documentation' |
| 14 | + existing_pages_dir: |
| 15 | + description: 'Path to existing site content' |
| 16 | + required: false |
| 17 | + default: '_existing_pages' |
| 18 | + output_dir: |
| 19 | + description: 'Output directory for the prepared site' |
27 | 20 | required: false |
28 | | - default: 'docs/_build_sphinx/html' |
| 21 | + default: '_site' |
| 22 | + |
| 23 | +outputs: |
| 24 | + site_dir: |
| 25 | + description: 'Path to the prepared site directory' |
| 26 | + value: ${{ steps.prepare.outputs.site_dir }} |
29 | 27 |
|
30 | 28 | runs: |
31 | 29 | using: 'composite' |
32 | 30 | steps: |
33 | | - - name: Deploy to gh-pages branch |
| 31 | + - name: Prepare site directory |
| 32 | + id: prepare |
34 | 33 | shell: bash |
35 | 34 | env: |
36 | | - GITHUB_TOKEN: ${{ inputs.github_token }} |
37 | | - REPOSITORY: ${{ inputs.repository }} |
38 | | - PR_NUMBER: ${{ inputs.pr_number }} |
39 | | - COMMIT_SHA: ${{ inputs.commit_sha }} |
40 | | - DOCS_BUILD_DIR: ${{ inputs.docs_build_dir }} |
| 35 | + DOCS_DIR: ${{ inputs.docs_dir }} |
| 36 | + TARGET_PATH: ${{ inputs.target_path }} |
| 37 | + EXISTING_DIR: ${{ inputs.existing_pages_dir }} |
| 38 | + OUTPUT_DIR: ${{ inputs.output_dir }} |
41 | 39 | run: | |
42 | | - # Configure git |
43 | | - git config --global user.name "github-actions[bot]" |
44 | | - git config --global user.email "github-actions[bot]@users.noreply.github.com" |
45 | | -
|
46 | | - # Create a temporary directory for gh-pages content |
47 | | - TEMP_DIR=$(mktemp -d) |
48 | | -
|
49 | | - # Clone just the gh-pages branch (or create it if it doesn't exist) |
50 | | - if git ls-remote --exit-code --heads "https://x-access-token:${GITHUB_TOKEN}@github.com/${REPOSITORY}.git" gh-pages > /dev/null 2>&1; then |
51 | | - git clone --single-branch --branch gh-pages --depth 1 "https://x-access-token:${GITHUB_TOKEN}@github.com/${REPOSITORY}.git" "$TEMP_DIR" |
52 | | - else |
53 | | - mkdir -p "$TEMP_DIR" |
54 | | - cd "$TEMP_DIR" |
55 | | - git init |
56 | | - git remote add origin "https://x-access-token:${GITHUB_TOKEN}@github.com/${REPOSITORY}.git" |
57 | | - git checkout -b gh-pages |
58 | | - touch .nojekyll |
59 | | - git add .nojekyll |
60 | | - git commit -m "Initialize gh-pages branch" |
| 40 | + # Create output directory |
| 41 | + mkdir -p "${OUTPUT_DIR}" |
| 42 | +
|
| 43 | + # Copy existing pages content if available (preserves other PR previews) |
| 44 | + if [ -d "${EXISTING_DIR}" ] && [ "$(ls -A ${EXISTING_DIR} 2>/dev/null)" ]; then |
| 45 | + echo "Copying existing pages content..." |
| 46 | + cp -r "${EXISTING_DIR}"/* "${OUTPUT_DIR}/" 2>/dev/null || true |
| 47 | + rm -rf "${OUTPUT_DIR}/.git" # Remove git directory if present |
61 | 48 | fi |
62 | 49 |
|
63 | | - # Create the preview directory |
64 | | - PR_PREVIEW_DIR="$TEMP_DIR/pr-preview/pr-${PR_NUMBER}" |
65 | | - rm -rf "$PR_PREVIEW_DIR" |
66 | | - mkdir -p "$PR_PREVIEW_DIR" |
| 50 | + # Ensure .nojekyll exists (prevents Jekyll processing) |
| 51 | + touch "${OUTPUT_DIR}/.nojekyll" |
67 | 52 |
|
68 | | - # Copy built docs to preview directory |
69 | | - cp -r "${GITHUB_WORKSPACE}/${DOCS_BUILD_DIR}"/* "$PR_PREVIEW_DIR/" |
| 53 | + # Prepare target directory |
| 54 | + FULL_TARGET="${OUTPUT_DIR}/${TARGET_PATH}" |
| 55 | + rm -rf "$FULL_TARGET" |
| 56 | + mkdir -p "$FULL_TARGET" |
70 | 57 |
|
71 | | - # Commit and push |
72 | | - cd "$TEMP_DIR" |
73 | | - git add . |
74 | | - git commit -m "Deploy docs preview for PR #${PR_NUMBER} (commit ${COMMIT_SHA})" || echo "No changes to commit" |
75 | | - git push origin gh-pages |
| 58 | + # Copy built docs to target directory |
| 59 | + cp -r "${DOCS_DIR}"/* "$FULL_TARGET/" |
76 | 60 |
|
77 | | - echo "Documentation preview deployed to gh-pages branch" |
| 61 | + echo "site_dir=${OUTPUT_DIR}" >> $GITHUB_OUTPUT |
0 commit comments