Skip to content
Merged
102 changes: 102 additions & 0 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
name: Preview

on:
pull_request:

jobs:
preview:
name: Run preview
runs-on: ubuntu-latest
env:
PREVIEW_HOSTNAME: ep-preview.click
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have this as an environment variable please? Makes it reusable for others.

Suggested change
PREVIEW_HOSTNAME: ep-preview.click
PREVIEW_HOSTNAME: ${{ vars.PREVIEW_HOSTNAME }}

Copy link
Contributor Author

@artcz artcz Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! - but I'm going to keep it as-is for now, because there are other things in that GHA that are enviornment-specific, so might be better to just have a separate commit/PR dealing with all those changes at once. – for example there's a separate deployment hostname (static.ep).
Separately it's probably a good idea to structure it as separate build/preview/deploy steps, so could be done as part of that change. Currently deploy/preview share a lot of logic that can be put in a separate "build" step.

GITHUB_BRANCH_NAME: ${{ github.head_ref || github.ref_name }}

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set timestamp for build/deploy
run: echo "TIMESTAMP=$(date +%Y%m%d%H%M%S)" >> $GITHUB_ENV

- name: Set up pnpm
uses: pnpm/action-setup@v4
with:
run_install: false

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"

- name: Install dependencies
run: make install

- name: Build the website
run: make build

- name: Set up SSH key
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.DEPLOY_SSH_KEY }}

- name: Get current branch name
run: |
BRANCH_NAME=$(make safe_branch BRANCH=$GITHUB_BRANCH_NAME)
echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV

- name: ssh keyscan
run: ssh-keyscan "static.europython.eu" > ~/.ssh/known_hosts

- name: Upload preview
run: make preview BRANCH=$GITHUB_BRANCH_NAME

- name: Update PR Comment
uses: actions/github-script@v6
if: github.event_name == 'pull_request'

with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
console.log("Hello world!");
const pr_id = ${{ github.event.number }};
console.log("PR Id %d", pr_id);

comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: Number(pr_id)
})

const preview_identifier = "# Preview available"

let comment_id = null;
comments.forEach(comment => {
if(comment.body.indexOf(preview_identifier) >= 0) {
comment_id = comment.id;
}
});

const branch_name = process.env.BRANCH_NAME;
const url = "https://" + branch_name + "." + process.env.PREVIEW_HOSTNAME;
const timestamp = new Date().toISOString();
const header = "\n|Key|Value|\n|---|---|\n"
const body = preview_identifier + header + "|url|" + url + "|\n|last update|" + timestamp + "|";

if(comment_id > 0) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment_id,
body: body
});

} else {

await github.rest.issues.createComment({
issue_number: Number(pr_id),
owner: context.repo.owner,
repo: context.repo.repo,
body: body
});
}
28 changes: 18 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
VPS_USER ?= static_content_user
VPS_HOST ?= static.europython.eu
VPS_PROD_PATH ?= /home/static_content_user/content/europython_websites/ep2025
VPS_PREVIEW_PATH ?= /home/static_content_user/content/previews/
VPS_PREVIEW_PATH ?= /home/static_content_user/content/previews
REMOTE_CMD=ssh $(VPS_USER)@$(VPS_HOST)

# Variables for build/deploy
Expand All @@ -15,21 +15,16 @@ export GIT_VERSION ?= $(shell git rev-parse --short HEAD)
# Variables for deploy
# ====================
# Auto-detect and sanitize current git branch
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
# Replace "/" and other non-alphanumeric characters with "-"
SAFE_BRANCH := $(shell echo "$(BRANCH)" | sed 's/[^A-Za-z0-9._-]/-/g')
FORCE_DEPLOY ?= false

# TODO: update this to the prod branches
ifeq ($(SAFE_BRANCH), ep2025)
RELEASES_DIR := $(VPS_PROD_PATH)/releases
else
RELEASES_DIR := $(VPS_PREVIEW_PATH)/$(SAFE_BRANCH)/releases
endif
.PHONY: build deploy dev clean install

TARGET := $(RELEASES_DIR)/$(TIMESTAMP)

.PHONY: build deploy dev clean install
safe_branch:
@echo $(SAFE_BRANCH)

pre:
npm install -g pnpm
Expand All @@ -52,7 +47,20 @@ build:
# NOTE: also let's find a better way to do this :D
find ./dist/_astro/ -iname '*.jpg' -delete

preview: RELEASES_DIR = $(VPS_PREVIEW_PATH)/$(SAFE_BRANCH)/releases
preview: TARGET = $(RELEASES_DIR)/$(TIMESTAMP)
preview:
echo $(TARGET)
@echo "\n\n**** Deploying preview of a branch '$(BRANCH)' (safe: $(SAFE_BRANCH)) to $(TARGET)...\n\n"
$(REMOTE_CMD) "mkdir -p $(TARGET)"
rsync -avz --delete ./dist/ $(VPS_USER)@$(VPS_HOST):$(TARGET)/
$(REMOTE_CMD) "cd $(RELEASES_DIR) && ln -snf $(TIMESTAMP) current"
@echo "\n\n**** Preview complete.\n\n"


ifeq ($(FORCE_DEPLOY), true)
deploy: RELEASES_DIR = $(VPS_PROD_PATH)/$(SAFE_BRANCH)/releases
deploy: TARGET = $(RELEASES_DIR)/$(TIMESTAMP)
deploy:
@echo "\n\n**** Deploying branch '$(BRANCH)' (safe: $(SAFE_BRANCH)) to $(TARGET)...\n\n"
$(REMOTE_CMD) "mkdir -p $(TARGET)"
Expand Down
2 changes: 1 addition & 1 deletion src/components/footer.astro
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ const gitVersion = import.meta.env.GIT_VERSION;
</div>
</article>

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