1- name : Deploy Apps to GitHub Pages
1+ name : Deploy Apps
22
33on :
44 push :
5- branches :
6- - main
5+ branches : [ main ]
6+ paths :
7+ - ' */package.json'
8+ - ' */index.html'
9+ - ' */**'
10+ pull_request :
11+ types : [opened, synchronize, reopened, closed]
712 paths :
813 - ' */package.json'
914 - ' */index.html'
1015 - ' */**'
1116
1217jobs :
1318 build-and-deploy :
19+ if : github.event.action != 'closed'
1420 runs-on : ubuntu-latest
1521 permissions :
16- contents : read
17- pages : write
18- id-token : write
19-
20- environment :
21- name : github-pages
22- url : ${{ steps.deployment.outputs.page_url }}
23-
22+ contents : write
23+ pull-requests : write
24+
2425 steps :
2526 - uses : actions/checkout@v3
2627
@@ -29,22 +30,54 @@ jobs:
2930 with :
3031 node-version : ' 18'
3132
32- - name : Setup Pages
33- uses : actions/configure-pages@v4
34-
3533 - name : Install root dependencies
3634 run : npm install
3735
3836 - name : Build all apps
39- run : npm run build _site
37+ run : |
38+ if [[ "${{ github.event_name }}" == "pull_request" ]]; then
39+ npm run build pr-preview ${{ github.event.pull_request.number }}
40+ else
41+ npm run build _site
42+ fi
4043
41- - name : Upload artifact
42- uses : actions/upload-pages-artifact @v3
44+ - name : Deploy to GitHub Pages
45+ uses : peaceiris/actions-gh-pages @v3
4346 with :
44- path : _site
47+ github_token : ${{ secrets.GITHUB_TOKEN }}
48+ publish_dir : ${{ github.event_name == 'pull_request' && './pr-preview' || './_site' }}
49+ destination_dir : ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number) || '.' }}
50+ keep_files : true
51+ enable_jekyll : false
4552
46- - name : Deploy to GitHub Pages
47- id : deployment
48- uses : actions/deploy-pages@v4
53+ - name : Comment PR
54+ if : github.event_name == 'pull_request'
55+ uses : actions/github-script@v6
56+ with :
57+ script : |
58+ const baseUrl = 'https://pollinations.github.io/hive/pr-${{ github.event.pull_request.number }}/';
59+ github.rest.issues.createComment({
60+ owner: context.repo.owner,
61+ repo: context.repo.repo,
62+ issue_number: context.issue.number,
63+ body: `🚀 PR Preview deployed!\n\nPreview URL: ${baseUrl}`
64+ })
65+
66+ cleanup-preview :
67+ if : github.event_name == 'pull_request' && github.event.action == 'closed'
68+ runs-on : ubuntu-latest
69+ permissions :
70+ contents : write
71+ steps :
72+ - uses : actions/checkout@v3
4973 with :
50- preview : false
74+ ref : gh-pages
75+
76+ - name : Remove PR Preview
77+ run : |
78+ rm -rf pr-${{ github.event.pull_request.number }}
79+ git config user.name github-actions
80+ git config user.email github-actions@github.com
81+ git add .
82+ git commit -m "Remove PR preview for #${{ github.event.pull_request.number }}" || echo "No changes to commit"
83+ git push
0 commit comments