Skip to content

Commit e3e37b9

Browse files
committed
add previews
1 parent 223b34c commit e3e37b9

File tree

2 files changed

+96
-8
lines changed

2 files changed

+96
-8
lines changed

.github/workflows/preview.yml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: Preview
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
preview:
8+
name: Run preview
9+
runs-on: ubuntu-latest
10+
env:
11+
PREVIEW_HOSTNAME: ep-preview.click
12+
13+
steps:
14+
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
with:
18+
sparse-checkout: |
19+
build
20+
21+
- name: Set up SSH key
22+
uses: webfactory/[email protected]
23+
with:
24+
ssh-private-key: ${{ secrets.DEPLOY_SSH_KEY }}
25+
26+
- name: Get current branch name
27+
run: |
28+
BRANCH_NAME=$(make safe_branch)
29+
echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV
30+
31+
- name: Upload preview
32+
run: make preview
33+
34+
- name: Update PR Comment
35+
uses: actions/github-script@v6
36+
if: github.event_name == 'pull_request'
37+
38+
with:
39+
github-token: ${{ secrets.GITHUB_TOKEN }}
40+
script: |
41+
console.log("Hello world!");
42+
const pr_id = ${{ github.event.number }};
43+
console.log("PR Id %d", pr_id);
44+
45+
comments = await github.paginate(github.rest.issues.listComments, {
46+
owner: context.repo.owner,
47+
repo: context.repo.repo,
48+
issue_number: Number(pr_id)
49+
})
50+
51+
const preview_identifier = "# Preview available"
52+
53+
let comment_id = null;
54+
comments.forEach(comment => {
55+
if(comment.body.indexOf(preview_identifier) >= 0) {
56+
comment_id = comment.id;
57+
}
58+
});
59+
60+
const branch_name = process.env.BRANCH_NAME;
61+
const url = "https://" + branch_name + "." + process.env.PREVIEW_HOSTNAME;
62+
const timestamp = new Date().toISOString();
63+
const header = "\n|Key|Value|\n|---|---|\n"
64+
const body = preview_identifier + header + "|url|" + url + "|\n|last update|" + timestamp + "|";
65+
66+
if(comment_id > 0) {
67+
await github.rest.issues.updateComment({
68+
owner: context.repo.owner,
69+
repo: context.repo.repo,
70+
comment_id: comment_id,
71+
body: body
72+
});
73+
74+
} else {
75+
76+
await github.rest.issues.createComment({
77+
issue_number: Number(pr_id),
78+
owner: context.repo.owner,
79+
repo: context.repo.repo,
80+
body: body
81+
});
82+
}

Makefile

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,11 @@ BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
2020
SAFE_BRANCH := $(shell echo "$(BRANCH)" | sed 's/[^A-Za-z0-9._-]/-/g')
2121
FORCE_DEPLOY ?= false
2222

23-
# TODO: update this to the prod branches
24-
ifeq ($(SAFE_BRANCH), ep2025)
25-
RELEASES_DIR := $(VPS_PROD_PATH)/releases
26-
else
27-
RELEASES_DIR := $(VPS_PREVIEW_PATH)/$(SAFE_BRANCH)/releases
28-
endif
23+
.PHONY: build deploy dev clean install
2924

30-
TARGET := $(RELEASES_DIR)/$(TIMESTAMP)
3125

32-
.PHONY: build deploy dev clean install
26+
safe_branch:
27+
@echo $(SAFE_BRANCH)
3328

3429
pre:
3530
npm install -g pnpm
@@ -54,9 +49,20 @@ build:
5449

5550
ifeq ($(FORCE_DEPLOY), true)
5651
deploy:
52+
RELEASES_DIR := $(VPS_PROD_PATH)/releases
53+
TARGET := $(RELEASES_DIR)/$(TIMESTAMP)
5754
@echo "\n\n**** Deploying branch '$(BRANCH)' (safe: $(SAFE_BRANCH)) to $(TARGET)...\n\n"
5855
$(REMOTE_CMD) "mkdir -p $(TARGET)"
5956
rsync -avz --delete ./dist/ $(VPS_USER)@$(VPS_HOST):$(TARGET)/
6057
$(REMOTE_CMD) "cd $(RELEASES_DIR) && ln -snf $(TIMESTAMP) current"
6158
@echo "\n\n**** Deployment complete.\n\n"
6259
endif
60+
61+
preview:
62+
RELEASES_DIR := $(VPS_PREVIEW_PATH)/$(SAFE_BRANCH)/releases
63+
TARGET := $(RELEASES_DIR)/$(TIMESTAMP)
64+
@echo "\n\n**** Deploying preview of a branch '$(BRANCH)' (safe: $(SAFE_BRANCH)) to $(TARGET)...\n\n"
65+
$(REMOTE_CMD) "mkdir -p $(TARGET)"
66+
rsync -avz --delete ./dist/ $(VPS_USER)@$(VPS_HOST):$(TARGET)/
67+
$(REMOTE_CMD) "cd $(RELEASES_DIR) && ln -snf $(TIMESTAMP) current"
68+
@echo "\n\n**** Preview complete.\n\n"

0 commit comments

Comments
 (0)