-
Notifications
You must be signed in to change notification settings - Fork 9
203 lines (176 loc) · 6.65 KB
/
pr-preview.yaml
File metadata and controls
203 lines (176 loc) · 6.65 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
name: Deploy PR Preview
on:
pull_request:
types: [opened, synchronize, reopened, closed]
# Cancel previous runs when new commits are pushed to the PR
concurrency:
group: pr-preview-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
deploy-preview:
name: Deploy PR Preview
runs-on: ubuntu-latest
if: github.event.action != 'closed'
permissions:
pull-requests: write
deployments: write
contents: read
steps:
- name: Checkout PR code
uses: actions/checkout@v6
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: '22'
cache: 'npm'
cache-dependency-path: 'package-lock.json'
- name: Install dependencies
run: |
echo "Installing dependencies..."
npm ci
- name: Build Docusaurus site
env:
DOCUSAURUS_BASE_URL: /pr-${{ github.event.pull_request.number }}
run: |
echo "Building site with base URL: $DOCUSAURUS_BASE_URL"
npm run build
- name: Upload build artifact for local testing
uses: actions/upload-artifact@v4
with:
name: pr-${{ github.event.pull_request.number }}-build
path: build/
retention-days: 30
- name: Prepare deployment directory
run: |
PR_DIR="pr-${{ github.event.pull_request.number }}"
echo "Creating directory: $PR_DIR"
mkdir -p "$PR_DIR"
# Create config.yaml
cat > "$PR_DIR/config.yaml" << 'EOF'
static:
files: 'build/**'
urlPath: 'pr-${{ github.event.pull_request.number }}'
extensions: ['html']
index: true
EOF
# Copy build directory
cp -r build "$PR_DIR/"
echo "Contents of $PR_DIR:"
ls -la "$PR_DIR"
- name: Install HarperDB CLI
run: |
npm install harperdb
- name: Deploy to HarperDB
env:
HARPER_PREVIEW_TARGET: ${{ secrets.HARPER_PREVIEW_TARGET }}
HARPER_PREVIEW_USERNAME: ${{ secrets.HARPER_PREVIEW_USERNAME }}
HARPER_PREVIEW_PASSWORD: ${{ secrets.HARPER_PREVIEW_PASSWORD }}
run: |
cd "pr-${{ github.event.pull_request.number }}"
# Add your additional arguments here
npx harper deploy \
target=${HARPER_PREVIEW_TARGET}:9925 \
username=$HARPER_PREVIEW_USERNAME \
password=$HARPER_PREVIEW_PASSWORD \
project=pr-${{ github.event.pull_request.number }} \
restart=true \
replicated=true
- name: Create deployment
env:
HARPER_PREVIEW_TARGET: ${{ secrets.HARPER_PREVIEW_TARGET }}
uses: actions/github-script@v8
with:
script: |
const prNumber = context.payload.pull_request.number;
const target = process.env.HARPER_PREVIEW_TARGET;
const deploymentUrl = `${target}/pr-${prNumber}`;
// Create deployment
const deployment = await github.rest.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.payload.pull_request.head.sha,
environment: `pr-${prNumber}`,
description: `Preview deployment for PR #${prNumber}`,
auto_merge: false,
required_contexts: []
});
// Create deployment status
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: deployment.data.id,
state: 'success',
environment_url: deploymentUrl,
description: 'Preview deployment successful'
});
// Also add a comment to the PR
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: `## 🚀 Preview Deployment\n\nYour preview deployment is ready!\n\n🔗 **Preview URL:** ${deploymentUrl}\n\nThis preview will update automatically when you push new commits.`
});
cleanup-preview:
name: Cleanup PR Preview
runs-on: ubuntu-latest
if: github.event.action == 'closed'
permissions:
pull-requests: write
deployments: write
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: '22'
cache: 'npm'
cache-dependency-path: 'package-lock.json'
- name: Install HarperDB CLI
run: |
npm install harperdb
- name: Remove preview deployment
env:
HARPER_PREVIEW_TARGET: ${{ secrets.HARPER_PREVIEW_TARGET }}
HARPER_PREVIEW_USERNAME: ${{ secrets.HARPER_PREVIEW_USERNAME }}
HARPER_PREVIEW_PASSWORD: ${{ secrets.HARPER_PREVIEW_PASSWORD }}
run: |
# Add your cleanup command here (e.g., harper undeploy or similar)
npx harper drop_component \
target=${HARPER_PREVIEW_TARGET}:9925 \
username=$HARPER_PREVIEW_USERNAME \
password=$HARPER_PREVIEW_PASSWORD \
project=pr-${{ github.event.pull_request.number }} \
replicated=true \
restart=true
echo "Cleaning up preview for PR #${{ github.event.pull_request.number }}"
- name: Update deployment status
uses: actions/github-script@v7
with:
script: |
const prNumber = context.payload.pull_request.number;
// Mark deployment as inactive
const deployments = await github.rest.repos.listDeployments({
owner: context.repo.owner,
repo: context.repo.repo,
environment: `pr-${prNumber}`
});
for (const deployment of deployments.data) {
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: deployment.id,
state: 'inactive',
description: 'Preview deployment removed'
});
}
// Add cleanup comment to PR
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: `## 🧹 Preview Cleanup\n\nThe preview deployment for this PR has been removed.`
});