Skip to content

Commit f8513cb

Browse files
egeakmancybitpre-commit-ci[bot]artcz
authored
Cherry pick new deployment logic to ep2024 (#1017)
Co-authored-by: Cyril Bitterich <cebit-github@gunnet.de> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Artur Czepiel <czepiel.artur@gmail.com>
1 parent 141c08e commit f8513cb

File tree

4 files changed

+223
-0
lines changed

4 files changed

+223
-0
lines changed

.github/workflows/build-deploy.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Deploy to Server
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- ep2024
8+
- ep2025
9+
10+
jobs:
11+
deploy:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout repository
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: ssh keyscan
44+
run: ssh-keyscan "static.europython.eu" > ~/.ssh/known_hosts
45+
46+
- name: Deploy to server
47+
run: make deploy FORCE_DEPLOY=true

.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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#
2+
# Variables for remote host
3+
# =========================
4+
VPS_USER ?= static_content_user
5+
VPS_HOST ?= static.europython.eu
6+
VPS_PROD_PATH ?= /home/static_content_user/content/europython_websites/ep2024
7+
VPS_PREVIEW_PATH ?= /home/static_content_user/content/previews
8+
REMOTE_CMD=ssh $(VPS_USER)@$(VPS_HOST)
9+
10+
# Variables for build/deploy
11+
# ==========================
12+
export TIMESTAMP ?= $(shell date +%Y%m%d%H%M%S)
13+
export GIT_VERSION ?= $(shell git rev-parse --short HEAD)
14+
15+
# Variables for deploy
16+
# ====================
17+
# Auto-detect and sanitize current git branch
18+
BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
19+
# Replace "/" and other non-alphanumeric characters with "-"
20+
SAFE_BRANCH := $(shell echo "$(BRANCH)" | sed 's/[^A-Za-z0-9._-]/-/g')
21+
FORCE_DEPLOY ?= false
22+
23+
.PHONY: build deploy dev clean install
24+
25+
26+
safe_branch:
27+
@echo $(SAFE_BRANCH)
28+
29+
pre:
30+
npm install -g pnpm
31+
32+
install:
33+
pnpm install
34+
35+
dev:
36+
pnpm dev
37+
38+
clean:
39+
git clean -fdX
40+
41+
check:
42+
pnpm run astro check
43+
44+
build:
45+
# TODO: update this to just `pnpm build` after resolving the astro-check warnings
46+
pnpm run astro build
47+
# NOTE: also let's find a better way to do this :D
48+
find ./dist/_astro/ -iname '*.jpg' -delete
49+
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+
61+
ifeq ($(FORCE_DEPLOY), true)
62+
deploy: RELEASES_DIR = $(VPS_PROD_PATH)/$(SAFE_BRANCH)/releases
63+
deploy: TARGET = $(RELEASES_DIR)/$(TIMESTAMP)
64+
deploy:
65+
@echo "\n\n**** Deploying branch '$(BRANCH)' (safe: $(SAFE_BRANCH)) to $(TARGET)...\n\n"
66+
$(REMOTE_CMD) "mkdir -p $(TARGET)"
67+
rsync -avz --delete ./dist/ $(VPS_USER)@$(VPS_HOST):$(TARGET)/
68+
$(REMOTE_CMD) "cd $(RELEASES_DIR) && ln -snf $(TIMESTAMP) current"
69+
@echo "\n\n**** Deployment complete.\n\n"
70+
endif

src/components/footer.astro

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { Fullbleed } from "./layout/fullbleed";
44
import { LogoExtended } from "./logo/logo-extended";
55
import links from "../data/links.json";
66
import { EPSLogo } from "./logo/eps-logo";
7+
8+
const buildTimestamp = import.meta.env.TIMESTAMP;
9+
const gitVersion = import.meta.env.GIT_VERSION;
710
---
811

912
<Fullbleed className="bg-primary text-white">
@@ -84,6 +87,7 @@ import { EPSLogo } from "./logo/eps-logo";
8487
github.com/europython <span> ↗</span>
8588
</a>
8689
</p>
90+
<p class="mb-4" style="color: rgba(255, 255, 255, 0.4)">version: {gitVersion} @ {buildTimestamp}</p>
8791
</div>
8892
</article>
8993
</footer>

0 commit comments

Comments
 (0)