-
Notifications
You must be signed in to change notification settings - Fork 0
204 lines (173 loc) · 6.01 KB
/
deploy-demo.yml
File metadata and controls
204 lines (173 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
name: Deploy Demo
on:
push:
branches: [main]
pull_request:
types: [opened, synchronize, reopened, closed]
workflow_dispatch:
permissions:
contents: write
pull-requests: write
pages: write
id-token: write
concurrency:
group: 'pages-${{ github.event.pull_request.number || github.ref }}'
cancel-in-progress: true
jobs:
build:
if: github.event.action != 'closed'
runs-on: ubuntu-latest
outputs:
preview_url: ${{ steps.set-url.outputs.url }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
cache: 'yarn'
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Determine base path
id: base-path
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
echo "path=/flagsmith-backstage-plugin/pr-${{ github.event.pull_request.number }}/" >> $GITHUB_OUTPUT
else
echo "path=/flagsmith-backstage-plugin/" >> $GITHUB_OUTPUT
fi
- name: Build demo
run: yarn build:demo
env:
VITE_BASE_PATH: ${{ steps.base-path.outputs.path }}
- name: Set preview URL
id: set-url
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
echo "url=https://${{ github.repository_owner }}.github.io/flagsmith-backstage-plugin/pr-${{ github.event.pull_request.number }}/" >> $GITHUB_OUTPUT
fi
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: demo-build
path: ./dist-demo
deploy-main:
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
needs: build
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: demo-build
path: ./dist-demo
- name: Setup Pages
uses: actions/configure-pages@v4
- name: Upload to Pages
uses: actions/upload-pages-artifact@v3
with:
path: ./dist-demo
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
deploy-pr-preview:
if: github.event_name == 'pull_request' && github.event.action != 'closed'
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: demo-build
path: ./dist-demo
- name: Deploy PR preview
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Move artifact to temp location before switching branches
mv dist-demo /tmp/dist-demo
# Check if gh-pages branch exists
if git ls-remote --heads origin gh-pages | grep -q gh-pages; then
git fetch origin gh-pages
git checkout gh-pages
else
# Create orphan gh-pages branch if it doesn't exist
git checkout --orphan gh-pages
git rm -rf .
echo "# GitHub Pages" > README.md
git add README.md
git commit -m "Initialize gh-pages branch"
fi
# Deploy PR preview
PR_DIR="pr-${{ github.event.pull_request.number }}"
rm -rf "$PR_DIR"
mkdir -p "$PR_DIR"
cp -r /tmp/dist-demo/* "$PR_DIR/"
git add .
git diff --staged --quiet || git commit -m "Deploy PR #${{ github.event.pull_request.number }} preview"
git push origin gh-pages
- name: Comment on PR
uses: actions/github-script@v7
with:
script: |
const url = '${{ needs.build.outputs.preview_url }}';
const body = `## Demo Preview
Preview URL: ${url}
This preview will be automatically cleaned up when the PR is closed.`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(c =>
c.user.type === 'Bot' && c.body.includes('Demo Preview')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}
cleanup-pr-preview:
if: github.event_name == 'pull_request' && github.event.action == 'closed'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Remove PR preview
run: |
# Check if gh-pages branch exists
if ! git ls-remote --heads origin gh-pages | grep -q gh-pages; then
echo "gh-pages branch does not exist, nothing to clean up"
exit 0
fi
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git fetch origin gh-pages
git checkout gh-pages
PR_DIR="pr-${{ github.event.pull_request.number }}"
if [ -d "$PR_DIR" ]; then
rm -rf "$PR_DIR"
git add .
git diff --staged --quiet || git commit -m "Remove PR #${{ github.event.pull_request.number }} preview"
git push origin gh-pages
fi