Skip to content

Commit 2dbb6c7

Browse files
Copilottyeth
andcommitted
Improve workflow with better error handling and documentation
Co-authored-by: tyeth <6692083+tyeth@users.noreply.github.com>
1 parent 330d687 commit 2dbb6c7

2 files changed

Lines changed: 128 additions & 8 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# GitHub Pages Preview Deployments
2+
3+
This workflow (`deploy-preview.yml`) automatically builds and deploys preview versions of the examples application to GitHub Pages for every branch push and pull request.
4+
5+
## How it works
6+
7+
### Triggering Events
8+
- **Pull Requests**: Triggers on `opened`, `synchronize`, and `reopened` events
9+
- **Branch Pushes**: Triggers on pushes to any branch except `main` (which has its own deployment workflow)
10+
11+
### Deployment Paths
12+
13+
The workflow deploys to the `gh-pages` branch using the following path structure:
14+
15+
- **Pull Requests**: `pr/<pr-number>-<short-sha>/`
16+
- Example: `pr/42-abc1234/`
17+
18+
- **Branches**: `branch/<safe-branch-name>-<short-sha>/`
19+
- Example: `branch/feature_new-ui-a7b8c9d/`
20+
21+
### Base URL Configuration
22+
23+
The workflow automatically:
24+
1. Fetches the GitHub Pages URL using the `gh` CLI
25+
2. Falls back to `https://<owner>.github.io/<repo>` if Pages isn't configured
26+
3. Builds the examples app with the correct base href using Parcel's `--public-url` option
27+
4. All asset paths are absolute URLs pointing to the correct subdirectory
28+
29+
### Features
30+
31+
-**Automatic PR Comments**: Posts a comment on PRs with the preview URL
32+
-**Branch Sanitization**: Safely handles branch names with special characters
33+
-**Incremental Deployments**: Each commit creates a new deployment with a unique SHA
34+
-**Job Summaries**: Provides deployment URL in GitHub Actions summary
35+
-**gh-pages Auto-Init**: Creates the gh-pages branch if it doesn't exist
36+
37+
## Usage
38+
39+
### For Pull Requests
40+
1. Open a pull request
41+
2. Wait for the workflow to complete
42+
3. Click the preview URL in the automated comment
43+
4. Each new commit will update the deployment (with a new SHA in the path)
44+
45+
### For Branch Pushes
46+
1. Push commits to any branch (except `main`)
47+
2. Check the workflow run for the deployment URL in the summary
48+
3. Access your preview at: `https://<owner>.github.io/<repo>/branch/<branch-name>-<sha>/`
49+
50+
## Permissions Required
51+
52+
The workflow needs the following permissions:
53+
- `contents: write` - To push to the gh-pages branch
54+
- `pull-requests: write` - To comment on pull requests
55+
- `pages: read` - To fetch the GitHub Pages URL
56+
57+
## Build Process
58+
59+
1. Install root dependencies and build the library
60+
2. Install example app dependencies
61+
3. Clean previous builds
62+
4. Generate API documentation
63+
5. Build example app with Parcel using custom `--public-url`
64+
6. Deploy to gh-pages branch in the appropriate subdirectory
65+
66+
## Customization
67+
68+
### Changing the Deployment Path Format
69+
70+
Edit the "Determine deployment path" step in `.github/workflows/deploy-preview.yml`:
71+
72+
```yaml
73+
- name: Determine deployment path
74+
id: deployment-path
75+
run: |
76+
# Modify DEPLOY_DIR and BASE_HREF variables here
77+
```
78+
79+
### Changing Build Configuration
80+
81+
The build uses Parcel with the following options:
82+
- `--no-optimize`: Faster builds, easier debugging
83+
- `--public-url`: Dynamic base URL for assets
84+
85+
To modify, edit the "Build example with base href" step.
86+
87+
## Troubleshooting
88+
89+
### Preview URL returns 404
90+
- Ensure GitHub Pages is enabled for the repository
91+
- Check that the gh-pages branch exists
92+
- Verify the deployment path in the workflow logs
93+
94+
### Assets not loading
95+
- Check the browser console for failed requests
96+
- Verify the base href is correct in the deployed HTML
97+
- Ensure all asset paths are absolute URLs
98+
99+
### Workflow fails to push
100+
- Check repository permissions
101+
- Verify the `GITHUB_TOKEN` has write access to contents

.github/workflows/deploy-preview.yml

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
branches:
66
- '**' # All branches
7+
- '!main' # Exclude main branch (handled by pages.yml)
78
pull_request:
89
types: [opened, synchronize, reopened]
910

@@ -100,16 +101,20 @@ jobs:
100101
- name: Setup gh-pages branch
101102
if: steps.checkout-gh-pages.outputs.exists == 'false'
102103
run: |
103-
# Create orphan gh-pages branch
104-
git checkout --orphan gh-pages
105-
git rm -rf .
106-
echo "# GitHub Pages" > README.md
104+
# Create a temporary directory for gh-pages initialization
105+
mkdir -p /tmp/gh-pages-init
106+
cd /tmp/gh-pages-init
107+
git init
108+
git checkout -b gh-pages
109+
echo "# GitHub Pages - Preview Deployments" > README.md
110+
echo "" >> README.md
111+
echo "This branch contains preview deployments for pull requests and branches." >> README.md
107112
git add README.md
108113
git config user.name "github-actions[bot]"
109114
git config user.email "github-actions[bot]@users.noreply.github.com"
110115
git commit -m "Initialize gh-pages branch"
111-
git push origin gh-pages
112-
cd ..
116+
git remote add origin https://x-access-token:${{ github.token }}@github.com/${{ github.repository }}.git
117+
git push -u origin gh-pages
113118
114119
- name: Checkout existing gh-pages
115120
if: steps.checkout-gh-pages.outputs.exists == 'true'
@@ -155,17 +160,31 @@ jobs:
155160
with:
156161
script: |
157162
const deployUrl = '${{ steps.deployment-path.outputs.base_href }}';
163+
const deployDir = '${{ steps.deployment-path.outputs.deploy_dir }}';
158164
const comment = `### 🚀 Preview Deployment Ready!
159165
160166
Your changes have been deployed to GitHub Pages:
161167
162-
**Preview URL:** ${deployUrl}
168+
**Preview URL:** [${deployUrl}](${deployUrl})
163169
164-
This preview will be available until the PR is closed or new commits are pushed.`;
170+
**Deployment Path:** \`${deployDir}\`
171+
172+
This preview will be updated with each new commit to this PR.`;
165173
166174
github.rest.issues.createComment({
167175
owner: context.repo.owner,
168176
repo: context.repo.repo,
169177
issue_number: context.issue.number,
170178
body: comment
171179
});
180+
181+
- name: Output deployment summary
182+
run: |
183+
echo "## Deployment Summary" >> $GITHUB_STEP_SUMMARY
184+
echo "" >> $GITHUB_STEP_SUMMARY
185+
echo "**Deployment URL:** [${{ steps.deployment-path.outputs.base_href }}](${{ steps.deployment-path.outputs.base_href }})" >> $GITHUB_STEP_SUMMARY
186+
echo "" >> $GITHUB_STEP_SUMMARY
187+
echo "**Deployment Directory:** \`${{ steps.deployment-path.outputs.deploy_dir }}\`" >> $GITHUB_STEP_SUMMARY
188+
echo "" >> $GITHUB_STEP_SUMMARY
189+
echo "**Event Type:** ${{ github.event_name }}" >> $GITHUB_STEP_SUMMARY
190+

0 commit comments

Comments
 (0)