Skip to content

Commit 168266a

Browse files
artczpre-commit-ci[bot]cybit
authored
add previews (#966)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Cyril Bitterich <cebit-github@gunnet.de>
1 parent 223b34c commit 168266a

File tree

3 files changed

+121
-11
lines changed

3 files changed

+121
-11
lines changed

.github/workflows/preview.yml

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
GITHUB_BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
13+
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
18+
- name: Set timestamp for build/deploy
19+
run: echo "TIMESTAMP=$(date +%Y%m%d%H%M%S)" >> $GITHUB_ENV
20+
21+
- name: Set up pnpm
22+
uses: pnpm/action-setup@v4
23+
with:
24+
run_install: false
25+
26+
- name: Set up Node.js
27+
uses: actions/setup-node@v4
28+
with:
29+
node-version: 20
30+
cache: "pnpm"
31+
32+
- name: Install dependencies
33+
run: make install
34+
35+
- name: Build the website
36+
run: make build
37+
38+
- name: Set up SSH key
39+
uses: webfactory/ssh-agent@v0.9.0
40+
with:
41+
ssh-private-key: ${{ secrets.DEPLOY_SSH_KEY }}
42+
43+
- name: Get current branch name
44+
run: |
45+
BRANCH_NAME=$(make safe_branch BRANCH=$GITHUB_BRANCH_NAME)
46+
echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV
47+
48+
- name: ssh keyscan
49+
run: ssh-keyscan "static.europython.eu" > ~/.ssh/known_hosts
50+
51+
- name: Upload preview
52+
run: make preview BRANCH=$GITHUB_BRANCH_NAME
53+
54+
- name: Update PR Comment
55+
uses: actions/github-script@v6
56+
if: github.event_name == 'pull_request'
57+
58+
with:
59+
github-token: ${{ secrets.GITHUB_TOKEN }}
60+
script: |
61+
console.log("Hello world!");
62+
const pr_id = ${{ github.event.number }};
63+
console.log("PR Id %d", pr_id);
64+
65+
comments = await github.paginate(github.rest.issues.listComments, {
66+
owner: context.repo.owner,
67+
repo: context.repo.repo,
68+
issue_number: Number(pr_id)
69+
})
70+
71+
const preview_identifier = "# Preview available"
72+
73+
let comment_id = null;
74+
comments.forEach(comment => {
75+
if(comment.body.indexOf(preview_identifier) >= 0) {
76+
comment_id = comment.id;
77+
}
78+
});
79+
80+
const branch_name = process.env.BRANCH_NAME;
81+
const url = "https://" + branch_name + "." + process.env.PREVIEW_HOSTNAME;
82+
const timestamp = new Date().toISOString();
83+
const header = "\n|Key|Value|\n|---|---|\n"
84+
const body = preview_identifier + header + "|url|" + url + "|\n|last update|" + timestamp + "|";
85+
86+
if(comment_id > 0) {
87+
await github.rest.issues.updateComment({
88+
owner: context.repo.owner,
89+
repo: context.repo.repo,
90+
comment_id: comment_id,
91+
body: body
92+
});
93+
94+
} else {
95+
96+
await github.rest.issues.createComment({
97+
issue_number: Number(pr_id),
98+
owner: context.repo.owner,
99+
repo: context.repo.repo,
100+
body: body
101+
});
102+
}

Makefile

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
VPS_USER ?= static_content_user
55
VPS_HOST ?= static.europython.eu
66
VPS_PROD_PATH ?= /home/static_content_user/content/europython_websites/ep2025
7-
VPS_PREVIEW_PATH ?= /home/static_content_user/content/previews/
7+
VPS_PREVIEW_PATH ?= /home/static_content_user/content/previews
88
REMOTE_CMD=ssh $(VPS_USER)@$(VPS_HOST)
99

1010
# Variables for build/deploy
@@ -15,21 +15,16 @@ export GIT_VERSION ?= $(shell git rev-parse --short HEAD)
1515
# Variables for deploy
1616
# ====================
1717
# Auto-detect and sanitize current git branch
18-
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
18+
BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
1919
# Replace "/" and other non-alphanumeric characters with "-"
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
@@ -52,7 +47,20 @@ build:
5247
# NOTE: also let's find a better way to do this :D
5348
find ./dist/_astro/ -iname '*.jpg' -delete
5449

50+
preview: RELEASES_DIR = $(VPS_PREVIEW_PATH)/$(SAFE_BRANCH)/releases
51+
preview: TARGET = $(RELEASES_DIR)/$(TIMESTAMP)
52+
preview:
53+
echo $(TARGET)
54+
@echo "\n\n**** Deploying preview of a branch '$(BRANCH)' (safe: $(SAFE_BRANCH)) to $(TARGET)...\n\n"
55+
$(REMOTE_CMD) "mkdir -p $(TARGET)"
56+
rsync -avz --delete ./dist/ $(VPS_USER)@$(VPS_HOST):$(TARGET)/
57+
$(REMOTE_CMD) "cd $(RELEASES_DIR) && ln -snf $(TIMESTAMP) current"
58+
@echo "\n\n**** Preview complete.\n\n"
59+
60+
5561
ifeq ($(FORCE_DEPLOY), true)
62+
deploy: RELEASES_DIR = $(VPS_PROD_PATH)/$(SAFE_BRANCH)/releases
63+
deploy: TARGET = $(RELEASES_DIR)/$(TIMESTAMP)
5664
deploy:
5765
@echo "\n\n**** Deploying branch '$(BRANCH)' (safe: $(SAFE_BRANCH)) to $(TARGET)...\n\n"
5866
$(REMOTE_CMD) "mkdir -p $(TARGET)"

src/components/footer.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,6 @@ const gitVersion = import.meta.env.GIT_VERSION;
9393
</div>
9494
</article>
9595

96-
<p>version: {gitVersion} @ {buildTimestamp}</p>
96+
<p class="mb-4" style="color: rgba(255, 255, 255, 0.4)">version: {gitVersion} @ {buildTimestamp}</p>
9797
</footer>
9898
</Fullbleed>

0 commit comments

Comments
 (0)