|
| 1 | +name: Deploy Preview to GitHub Pages |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - '**' # All branches |
| 7 | + - '!main' # Exclude main branch (handled by pages.yml) |
| 8 | + pull_request: |
| 9 | + types: [opened, synchronize, reopened] |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + pull-requests: write |
| 14 | + pages: read |
| 15 | + |
| 16 | +jobs: |
| 17 | + deploy-preview: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Checkout |
| 21 | + uses: actions/checkout@v4 |
| 22 | + |
| 23 | + - name: Set up Node |
| 24 | + uses: actions/setup-node@v4 |
| 25 | + with: |
| 26 | + node-version: 20 |
| 27 | + |
| 28 | + - name: Determine deployment path |
| 29 | + id: deployment-path |
| 30 | + run: | |
| 31 | + # Get the GitHub Pages base URL |
| 32 | + GH_PAGES_URL=$(gh api repos/${{ github.repository }}/pages --jq '.html_url' 2>/dev/null || echo "") |
| 33 | + |
| 34 | + if [ -z "$GH_PAGES_URL" ]; then |
| 35 | + echo "GitHub Pages not configured, using repository name as base" |
| 36 | + REPO_NAME=$(echo "${{ github.repository }}" | cut -d'/' -f2) |
| 37 | + GH_PAGES_URL="https://${{ github.repository_owner }}.github.io/${REPO_NAME}" |
| 38 | + fi |
| 39 | + |
| 40 | + # Remove trailing slash from base URL |
| 41 | + GH_PAGES_URL=${GH_PAGES_URL%/} |
| 42 | + |
| 43 | + # Determine if this is a PR or branch push |
| 44 | + if [ "${{ github.event_name }}" = "pull_request" ]; then |
| 45 | + PR_NUMBER="${{ github.event.pull_request.number }}" |
| 46 | + REF="${{ github.event.pull_request.head.sha }}" |
| 47 | + SHORT_REF=${REF:0:7} |
| 48 | + DEPLOY_DIR="pr/${PR_NUMBER}-${SHORT_REF}" |
| 49 | + BASE_HREF="${GH_PAGES_URL}/pr/${PR_NUMBER}-${SHORT_REF}/" |
| 50 | + else |
| 51 | + BRANCH_NAME="${{ github.ref_name }}" |
| 52 | + # Sanitize branch name for use in paths |
| 53 | + SAFE_BRANCH=$(echo "$BRANCH_NAME" | sed 's/[^a-zA-Z0-9-]/_/g') |
| 54 | + REF="${{ github.sha }}" |
| 55 | + SHORT_REF=${REF:0:7} |
| 56 | + DEPLOY_DIR="branch/${SAFE_BRANCH}-${SHORT_REF}" |
| 57 | + BASE_HREF="${GH_PAGES_URL}/branch/${SAFE_BRANCH}-${SHORT_REF}/" |
| 58 | + fi |
| 59 | + |
| 60 | + echo "deploy_dir=${DEPLOY_DIR}" >> $GITHUB_OUTPUT |
| 61 | + echo "base_href=${BASE_HREF}" >> $GITHUB_OUTPUT |
| 62 | + echo "gh_pages_url=${GH_PAGES_URL}" >> $GITHUB_OUTPUT |
| 63 | + |
| 64 | + echo "Deployment directory: ${DEPLOY_DIR}" |
| 65 | + echo "Base HREF: ${BASE_HREF}" |
| 66 | + env: |
| 67 | + GH_TOKEN: ${{ github.token }} |
| 68 | + |
| 69 | + - name: Install root dependencies |
| 70 | + run: npm install |
| 71 | + |
| 72 | + - name: Build library |
| 73 | + run: npm run build |
| 74 | + |
| 75 | + - name: Install example dependencies |
| 76 | + run: | |
| 77 | + cd examples/typescript |
| 78 | + npm install |
| 79 | +
|
| 80 | + - name: Build example with base href |
| 81 | + run: | |
| 82 | + cd examples/typescript |
| 83 | + # Clean and generate docs first |
| 84 | + npm run clean |
| 85 | + npm run genDocs |
| 86 | + # Build with the dynamic base href using custom script |
| 87 | + npm run parcel:build:custom -- --public-url "${{ steps.deployment-path.outputs.base_href }}" |
| 88 | +
|
| 89 | + - name: Checkout gh-pages branch |
| 90 | + id: checkout-gh-pages |
| 91 | + run: | |
| 92 | + # Try to checkout gh-pages branch |
| 93 | + if git ls-remote --heads origin gh-pages | grep -q gh-pages; then |
| 94 | + echo "gh-pages branch exists" |
| 95 | + echo "exists=true" >> $GITHUB_OUTPUT |
| 96 | + else |
| 97 | + echo "gh-pages branch does not exist" |
| 98 | + echo "exists=false" >> $GITHUB_OUTPUT |
| 99 | + fi |
| 100 | +
|
| 101 | + - name: Setup gh-pages branch |
| 102 | + if: steps.checkout-gh-pages.outputs.exists == 'false' |
| 103 | + run: | |
| 104 | + # Create a temporary directory for gh-pages initialization |
| 105 | + TEMP_DIR="${{ runner.temp }}/gh-pages-init" |
| 106 | + mkdir -p "$TEMP_DIR" |
| 107 | + cd "$TEMP_DIR" |
| 108 | + git init |
| 109 | + git checkout -b gh-pages |
| 110 | + echo "# GitHub Pages - Preview Deployments" > README.md |
| 111 | + echo "" >> README.md |
| 112 | + echo "This branch contains preview deployments for pull requests and branches." >> README.md |
| 113 | + git add README.md |
| 114 | + git config user.name "github-actions[bot]" |
| 115 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 116 | + git commit -m "Initialize gh-pages branch" |
| 117 | + git remote add origin https://x-access-token:${{ github.token }}@github.com/${{ github.repository }}.git |
| 118 | + git push -u origin gh-pages |
| 119 | +
|
| 120 | + - name: Checkout existing gh-pages |
| 121 | + if: steps.checkout-gh-pages.outputs.exists == 'true' |
| 122 | + uses: actions/checkout@v4 |
| 123 | + with: |
| 124 | + ref: gh-pages |
| 125 | + path: gh-pages-repo |
| 126 | + |
| 127 | + - name: Setup deployment directory |
| 128 | + run: | |
| 129 | + if [ "${{ steps.checkout-gh-pages.outputs.exists }}" = "false" ]; then |
| 130 | + # Clone the newly created gh-pages branch |
| 131 | + git clone --single-branch --branch gh-pages https://x-access-token:${{ github.token }}@github.com/${{ github.repository }}.git gh-pages-repo |
| 132 | + fi |
| 133 | +
|
| 134 | + - name: Deploy to gh-pages |
| 135 | + run: | |
| 136 | + DEPLOY_DIR="${{ steps.deployment-path.outputs.deploy_dir }}" |
| 137 | + |
| 138 | + # Create deployment directory structure |
| 139 | + mkdir -p "gh-pages-repo/${DEPLOY_DIR}" |
| 140 | + |
| 141 | + # Copy built files to deployment directory |
| 142 | + cp -r examples/typescript/dist/* "gh-pages-repo/${DEPLOY_DIR}/" |
| 143 | + |
| 144 | + # Configure git |
| 145 | + cd gh-pages-repo |
| 146 | + git config user.name "github-actions[bot]" |
| 147 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 148 | + |
| 149 | + # Commit and push |
| 150 | + git add . |
| 151 | + if git diff --staged --quiet; then |
| 152 | + echo "No changes to deploy" |
| 153 | + else |
| 154 | + git commit -m "Deploy preview: ${{ steps.deployment-path.outputs.deploy_dir }}" |
| 155 | + git push origin gh-pages |
| 156 | + fi |
| 157 | +
|
| 158 | + - name: Comment on PR with preview URL |
| 159 | + if: github.event_name == 'pull_request' |
| 160 | + uses: actions/github-script@v7 |
| 161 | + with: |
| 162 | + script: | |
| 163 | + const deployUrl = '${{ steps.deployment-path.outputs.base_href }}'; |
| 164 | + const deployDir = '${{ steps.deployment-path.outputs.deploy_dir }}'; |
| 165 | + const comment = `### 🚀 Preview Deployment Ready! |
| 166 | + |
| 167 | + Your changes have been deployed to GitHub Pages: |
| 168 | + |
| 169 | + **Preview URL:** [${deployUrl}](${deployUrl}) |
| 170 | + |
| 171 | + **Deployment Path:** \`${deployDir}\` |
| 172 | + |
| 173 | + This preview will be updated with each new commit to this PR.`; |
| 174 | + |
| 175 | + github.rest.issues.createComment({ |
| 176 | + owner: context.repo.owner, |
| 177 | + repo: context.repo.repo, |
| 178 | + issue_number: context.issue.number, |
| 179 | + body: comment |
| 180 | + }); |
| 181 | +
|
| 182 | + - name: Output deployment summary |
| 183 | + run: | |
| 184 | + echo "## Deployment Summary" >> $GITHUB_STEP_SUMMARY |
| 185 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 186 | + echo "**Deployment URL:** [${{ steps.deployment-path.outputs.base_href }}](${{ steps.deployment-path.outputs.base_href }})" >> $GITHUB_STEP_SUMMARY |
| 187 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 188 | + echo "**Deployment Directory:** \`${{ steps.deployment-path.outputs.deploy_dir }}\`" >> $GITHUB_STEP_SUMMARY |
| 189 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 190 | + echo "**Event Type:** ${{ github.event_name }}" >> $GITHUB_STEP_SUMMARY |
| 191 | +
|
0 commit comments