@@ -3,20 +3,26 @@ name: Deploy Demo
33on :
44 push :
55 branches : [main]
6+ pull_request :
7+ types : [opened, synchronize, reopened, closed]
68 workflow_dispatch :
79
810permissions :
9- contents : read
11+ contents : write
12+ pull-requests : write
1013 pages : write
1114 id-token : write
1215
1316concurrency :
14- group : " pages"
15- cancel-in-progress : false
17+ group : ' pages-${{ github.event.pull_request.number || github.ref }} '
18+ cancel-in-progress : true
1619
1720jobs :
1821 build :
22+ if : github.event.action != 'closed'
1923 runs-on : ubuntu-latest
24+ outputs :
25+ preview_url : ${{ steps.set-url.outputs.url }}
2026 steps :
2127 - name : Checkout code
2228 uses : actions/checkout@v4
@@ -30,24 +36,144 @@ jobs:
3036 - name : Install dependencies
3137 run : yarn install --frozen-lockfile
3238
39+ - name : Determine base path
40+ id : base-path
41+ run : |
42+ if [ "${{ github.event_name }}" = "pull_request" ]; then
43+ echo "path=/flagsmith-backstage-plugin/pr-${{ github.event.pull_request.number }}/" >> $GITHUB_OUTPUT
44+ else
45+ echo "path=/flagsmith-backstage-plugin/" >> $GITHUB_OUTPUT
46+ fi
47+
3348 - name : Build demo
3449 run : yarn build:demo
50+ env :
51+ VITE_BASE_PATH : ${{ steps.base-path.outputs.path }}
3552
36- - name : Setup Pages
37- uses : actions/configure-pages@v4
53+ - name : Set preview URL
54+ id : set-url
55+ run : |
56+ if [ "${{ github.event_name }}" = "pull_request" ]; then
57+ echo "url=https://${{ github.repository_owner }}.github.io/flagsmith-backstage-plugin/pr-${{ github.event.pull_request.number }}/" >> $GITHUB_OUTPUT
58+ fi
3859
3960 - name : Upload artifact
40- uses : actions/upload-pages- artifact@v3
61+ uses : actions/upload-artifact@v4
4162 with :
63+ name : demo-build
4264 path : ./dist-demo
4365
44- deploy :
66+ deploy-main :
67+ if : github.event_name == 'push' && github.ref == 'refs/heads/main'
4568 needs : build
4669 runs-on : ubuntu-latest
4770 environment :
4871 name : github-pages
4972 url : ${{ steps.deployment.outputs.page_url }}
5073 steps :
74+ - name : Download artifact
75+ uses : actions/download-artifact@v4
76+ with :
77+ name : demo-build
78+ path : ./dist-demo
79+
80+ - name : Setup Pages
81+ uses : actions/configure-pages@v4
82+
83+ - name : Upload to Pages
84+ uses : actions/upload-pages-artifact@v3
85+ with :
86+ path : ./dist-demo
87+
5188 - name : Deploy to GitHub Pages
5289 id : deployment
5390 uses : actions/deploy-pages@v4
91+
92+ deploy-pr-preview :
93+ if : github.event_name == 'pull_request' && github.event.action != 'closed'
94+ needs : build
95+ runs-on : ubuntu-latest
96+ steps :
97+ - name : Checkout gh-pages branch
98+ uses : actions/checkout@v4
99+ with :
100+ ref : gh-pages
101+ path : gh-pages
102+
103+ - name : Download artifact
104+ uses : actions/download-artifact@v4
105+ with :
106+ name : demo-build
107+ path : ./dist-demo
108+
109+ - name : Deploy PR preview
110+ run : |
111+ PR_DIR="gh-pages/pr-${{ github.event.pull_request.number }}"
112+ rm -rf "$PR_DIR"
113+ mkdir -p "$PR_DIR"
114+ cp -r dist-demo/* "$PR_DIR/"
115+
116+ cd gh-pages
117+ git config user.name "github-actions[bot]"
118+ git config user.email "github-actions[bot]@users.noreply.github.com"
119+ git add .
120+ git diff --staged --quiet || git commit -m "Deploy PR #${{ github.event.pull_request.number }} preview"
121+ git push
122+
123+ - name : Comment on PR
124+ uses : actions/github-script@v7
125+ with :
126+ script : |
127+ const url = '${{ needs.build.outputs.preview_url }}';
128+ const body = `## Demo Preview
129+
130+ Preview URL: ${url}
131+
132+ This preview will be automatically cleaned up when the PR is closed.`;
133+
134+ const { data: comments } = await github.rest.issues.listComments({
135+ owner: context.repo.owner,
136+ repo: context.repo.repo,
137+ issue_number: context.issue.number,
138+ });
139+
140+ const botComment = comments.find(c =>
141+ c.user.type === 'Bot' && c.body.includes('Demo Preview')
142+ );
143+
144+ if (botComment) {
145+ await github.rest.issues.updateComment({
146+ owner: context.repo.owner,
147+ repo: context.repo.repo,
148+ comment_id: botComment.id,
149+ body,
150+ });
151+ } else {
152+ await github.rest.issues.createComment({
153+ owner: context.repo.owner,
154+ repo: context.repo.repo,
155+ issue_number: context.issue.number,
156+ body,
157+ });
158+ }
159+
160+ cleanup-pr-preview :
161+ if : github.event_name == 'pull_request' && github.event.action == 'closed'
162+ runs-on : ubuntu-latest
163+ steps :
164+ - name : Checkout gh-pages branch
165+ uses : actions/checkout@v4
166+ with :
167+ ref : gh-pages
168+
169+ - name : Remove PR preview
170+ run : |
171+ PR_DIR="pr-${{ github.event.pull_request.number }}"
172+ if [ -d "$PR_DIR" ]; then
173+ rm -rf "$PR_DIR"
174+ git config user.name "github-actions[bot]"
175+ git config user.email "github-actions[bot]@users.noreply.github.com"
176+ git add .
177+ git diff --staged --quiet || git commit -m "Remove PR #${{ github.event.pull_request.number }} preview"
178+ git push
179+ fi
0 commit comments