Skip to content

Commit 2217c3d

Browse files
test: add previews deployments for forks (#299)
1 parent 7861b5b commit 2217c3d

File tree

4 files changed

+130
-80
lines changed

4 files changed

+130
-80
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: PR Preview Build
2+
3+
on:
4+
pull_request:
5+
types: [opened, reopened, synchronize]
6+
branches:
7+
- 'master'
8+
paths-ignore:
9+
- '**.md'
10+
- 'docker*'
11+
- 'Docker*'
12+
13+
jobs:
14+
build:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: 📝 Save PR number
20+
run: echo "${{ github.event.pull_request.number }}" > pr_number.txt
21+
22+
- name: 🏠 Replace homepage URL
23+
run: sed -i "s/https:\/\/valhalla.openstreetmap.de/https:\/\/valhalla-app-tests.gis-ops.com\/${{ github.event.pull_request.number }}/g" package.json
24+
25+
- uses: actions/setup-node@v4
26+
with:
27+
node-version: '20.x'
28+
29+
- name: 🔧 Install and build
30+
run: |
31+
npm ci
32+
npm run build
33+
34+
- name: 📝 Create .htaccess for SPA routing
35+
run: |
36+
cat > ./build/.htaccess << 'EOF'
37+
<IfModule mod_rewrite.c>
38+
RewriteEngine On
39+
RewriteBase /${{ github.event.pull_request.number }}/
40+
41+
RewriteCond %{REQUEST_FILENAME} !-f
42+
RewriteCond %{REQUEST_FILENAME} !-d
43+
RewriteRule ^ index.html [L]
44+
</IfModule>
45+
EOF
46+
47+
- name: 📦 Upload build artifact
48+
uses: actions/upload-artifact@v4
49+
with:
50+
name: preview-build
51+
path: |
52+
./build
53+
pr_number.txt
54+
retention-days: 1
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: PR Preview Cleanup
2+
3+
on:
4+
pull_request_target:
5+
types: [closed]
6+
branches:
7+
- 'master'
8+
9+
jobs:
10+
clean-up:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: 🧹 Remove preview directory
14+
env:
15+
PULL_NUMBER: ${{ github.event.pull_request.number }}
16+
run: |
17+
eval $(ssh-agent -s)
18+
ssh-add - <<< "${{ secrets.PREVIEW_SSH_PRIVATE_KEY }}"
19+
ssh -o StrictHostKeyChecking=no ${{ vars.PREVIEW_HOST_USERNAME }}@${{ vars.PREVIEW_HOST_IP }} "rm -rf ${{ vars.PREVIEW_HOST_TARGET_DIR }}/$PULL_NUMBER"
20+
echo "🧹 Cleaned up preview for PR #$PULL_NUMBER"
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: PR Preview Deploy
2+
3+
on:
4+
workflow_run:
5+
workflows: ['PR Preview Build']
6+
types: [completed]
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
12+
steps:
13+
- name: 📥 Download build artifact
14+
uses: actions/download-artifact@v4
15+
with:
16+
name: preview-build
17+
path: .
18+
run-id: ${{ github.event.workflow_run.id }}
19+
github-token: ${{ secrets.GITHUB_TOKEN }}
20+
21+
- name: 📖 Read PR number
22+
id: pr
23+
run: echo "number=$(cat pr_number.txt)" >> $GITHUB_OUTPUT
24+
25+
- name: 🚀 Deploy preview
26+
env:
27+
PULL_NUMBER: ${{ steps.pr.outputs.number }}
28+
run: |
29+
eval $(ssh-agent -s)
30+
ssh-add - <<< "${{ secrets.PREVIEW_SSH_PRIVATE_KEY }}"
31+
rsync -r --delete --rsync-path="mkdir -p ${{ vars.PREVIEW_HOST_TARGET_DIR }}/$PULL_NUMBER && rsync" -e "ssh -o StrictHostKeyChecking=no" ./build/ ${{ vars.PREVIEW_HOST_USERNAME }}@${{ vars.PREVIEW_HOST_IP }}:${{ vars.PREVIEW_HOST_TARGET_DIR }}/$PULL_NUMBER
32+
33+
- name: 🌐 Comment preview URL
34+
uses: actions/github-script@v7
35+
with:
36+
github-token: ${{ secrets.PAT }}
37+
script: |
38+
const prNumber = parseInt('${{ steps.pr.outputs.number }}');
39+
const { data: comments } = await github.rest.issues.listComments({
40+
owner: context.repo.owner,
41+
repo: context.repo.repo,
42+
issue_number: prNumber
43+
});
44+
const existingComment = comments.find(comment => comment.body.includes('Preview is ready! 🚀'));
45+
if (!existingComment) {
46+
const body = `Preview is ready! 🚀 You can view it here: https://valhalla-app-tests.gis-ops.com/${prNumber}`;
47+
await github.rest.issues.createComment({
48+
owner: context.repo.owner,
49+
repo: context.repo.repo,
50+
issue_number: prNumber,
51+
body: body
52+
});
53+
console.log('Comment created successfully');
54+
} else {
55+
console.log('Comment already exists');
56+
}

.github/workflows/preview.yml

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)