Skip to content

Commit 199a65b

Browse files
committed
chore: update deploy workflow and scripts
1 parent d5b17cd commit 199a65b

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed

.github/workflows/deploy-gh-pages.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ jobs:
4444
run: npm run build-storybook
4545
env:
4646
NODE_ENV: production
47+
STORYBOOK_BASE_HREF: /pro-react-admin/storybook/
4748

4849
- name: Organize build output
4950
run: |

docs/DEPLOY.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Deployment Guide
2+
3+
This project is configured to deploy automatically to GitHub Pages. The deployment includes both the main application and the Storybook documentation.
4+
5+
## Deployment Structure
6+
7+
- **Main Application**: Deployed to the root (`/`).
8+
- URL: `https://wkylin.github.io/pro-react-admin/`
9+
- **Storybook**: Deployed to a subdirectory (`/storybook/`).
10+
- URL: `https://wkylin.github.io/pro-react-admin/storybook/`
11+
12+
## How to Deploy
13+
14+
We have a unified script to handle the build and deployment process.
15+
16+
### Command
17+
18+
```bash
19+
npm run deploy
20+
```
21+
22+
### What this command does
23+
24+
The `npm run deploy` command executes `scripts/deploy-gh.js`, which performs the following steps:
25+
26+
1. **Build Main App**: Runs `npm run build:production` to generate the production build in the `dist` directory.
27+
2. **Build Storybook**: Runs `npm run build-storybook` with `STORYBOOK_BASE_HREF=/pro-react-admin/storybook/`.
28+
- This ensures that Storybook assets are loaded correctly from the subdirectory.
29+
- The output is generated in `storybook-static`.
30+
3. **Merge Builds**:
31+
- Moves the `storybook-static` directory into `dist/storybook`.
32+
4. **Deploy**:
33+
- Uses `gh-pages` to push the contents of the `dist` directory to the `gh-pages` branch of the repository.
34+
35+
## Configuration Details
36+
37+
### Storybook Base Path
38+
39+
The Storybook build uses a custom script `scripts/add-storybook-base.js` (triggered via `postbuild-storybook`) to inject the `<base>` tag into `index.html`.
40+
41+
- **Environment Variable**: `STORYBOOK_BASE_HREF`
42+
- **Value for GitHub Pages**: `/pro-react-admin/storybook/`
43+
44+
If you are deploying to a different path or domain, you may need to adjust this variable in `scripts/deploy-gh.js`.
45+
46+
### GitHub Pages Branch
47+
48+
The deployment uses the `gh-pages` branch. Ensure your repository settings on GitHub are configured to serve from this branch.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@
9292
"process": "node webpack/process",
9393
"sentry:check": "node check-sentry.js",
9494
"predeploy": "npm run build:production",
95-
"deploy": "gh-pages -d dist",
95+
"deploy": "node scripts/deploy-gh.js",
96+
"deploy:only": "gh-pages -d dist",
9697
"clean:lock": "rimraf package-lock.json yarn.lock pnpm-lock.yaml",
9798
"clean:dist": "rimraf ./dist",
9899
"clean:modules": "rimraf node_modules",

scripts/deploy-gh.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const { execSync } = require('child_process');
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
// Helper to run commands
6+
const run = (command, options = {}) => {
7+
console.log(`Running: ${command}`);
8+
execSync(command, { stdio: 'inherit', ...options });
9+
};
10+
11+
const distDir = path.resolve(__dirname, '../dist');
12+
const storybookStaticDir = path.resolve(__dirname, '../storybook-static');
13+
const storybookTargetDir = path.join(distDir, 'storybook');
14+
15+
try {
16+
// 1. Build Main App
17+
console.log('-------------------------------------------------------');
18+
console.log('Step 1: Building Main App (Production)...');
19+
console.log('-------------------------------------------------------');
20+
run('npm run build:production');
21+
22+
// 2. Build Storybook
23+
console.log('\n-------------------------------------------------------');
24+
console.log('Step 2: Building Storybook...');
25+
console.log('-------------------------------------------------------');
26+
// Set env var for base href.
27+
// Homepage is https://wkylin.github.io/pro-react-admin
28+
// So root is /pro-react-admin/
29+
// Storybook should be at /pro-react-admin/storybook/
30+
const env = { ...process.env, STORYBOOK_BASE_HREF: '/pro-react-admin/storybook/' };
31+
run('npm run build-storybook', { env });
32+
33+
// 3. Move Storybook to dist/storybook
34+
console.log('\n-------------------------------------------------------');
35+
console.log('Step 3: Moving Storybook to dist/storybook...');
36+
console.log('-------------------------------------------------------');
37+
38+
if (!fs.existsSync(distDir)) {
39+
throw new Error('dist directory does not exist! Main app build might have failed.');
40+
}
41+
42+
// Clean previous storybook dir in dist if exists
43+
if (fs.existsSync(storybookTargetDir)) {
44+
console.log(`Removing existing ${storybookTargetDir}...`);
45+
fs.rmSync(storybookTargetDir, { recursive: true, force: true });
46+
}
47+
48+
// Ensure parent dir exists (it should, as dist exists)
49+
if (!fs.existsSync(storybookStaticDir)) {
50+
throw new Error('storybook-static directory does not exist! Storybook build might have failed.');
51+
}
52+
53+
console.log(`Moving ${storybookStaticDir} to ${storybookTargetDir}...`);
54+
55+
// Try rename first (fastest)
56+
try {
57+
fs.renameSync(storybookStaticDir, storybookTargetDir);
58+
} catch (err) {
59+
if (err.code === 'EXDEV') {
60+
// Cross-device link, fall back to copy
61+
console.log('Cross-device link detected, copying files instead...');
62+
fs.cpSync(storybookStaticDir, storybookTargetDir, { recursive: true });
63+
fs.rmSync(storybookStaticDir, { recursive: true, force: true });
64+
} else {
65+
throw err;
66+
}
67+
}
68+
69+
// 4. Deploy
70+
console.log('\n-------------------------------------------------------');
71+
console.log('Step 4: Deploying to GitHub Pages...');
72+
console.log('-------------------------------------------------------');
73+
// We use npx to ensure we use the local gh-pages binary
74+
run('npx gh-pages -d dist');
75+
76+
console.log('\n=======================================================');
77+
console.log('Deployment Complete! 🚀');
78+
console.log('Main App: https://wkylin.github.io/pro-react-admin/');
79+
console.log('Storybook: https://wkylin.github.io/pro-react-admin/storybook/');
80+
console.log('=======================================================');
81+
82+
} catch (error) {
83+
console.error('\n❌ Deployment Failed:', error.message);
84+
process.exit(1);
85+
}

0 commit comments

Comments
 (0)