From e3e37b9f856cf8919ddc0ba8173af29a6451dfeb Mon Sep 17 00:00:00 2001 From: Artur Czepiel Date: Mon, 20 Jan 2025 13:37:51 +0100 Subject: [PATCH 01/12] add previews --- .github/workflows/preview.yml | 82 +++++++++++++++++++++++++++++++++++ Makefile | 22 ++++++---- 2 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/preview.yml diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml new file mode 100644 index 000000000..2db00ef6e --- /dev/null +++ b/.github/workflows/preview.yml @@ -0,0 +1,82 @@ +name: Preview + +on: + pull_request: + +jobs: + preview: + name: Run preview + runs-on: ubuntu-latest + env: + PREVIEW_HOSTNAME: ep-preview.click + + steps: + + - name: Checkout + uses: actions/checkout@v4 + with: + sparse-checkout: | + 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) + echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV + + - name: Upload preview + run: make preview + + - 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 + }); + } diff --git a/Makefile b/Makefile index aeffeab14..b050828dd 100644 --- a/Makefile +++ b/Makefile @@ -20,16 +20,11 @@ BRANCH := $(shell git rev-parse --abbrev-ref HEAD) 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 @@ -54,9 +49,20 @@ build: ifeq ($(FORCE_DEPLOY), true) deploy: + RELEASES_DIR := $(VPS_PROD_PATH)/releases + TARGET := $(RELEASES_DIR)/$(TIMESTAMP) @echo "\n\n**** Deploying 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**** Deployment complete.\n\n" endif + +preview: + RELEASES_DIR := $(VPS_PREVIEW_PATH)/$(SAFE_BRANCH)/releases + TARGET := $(RELEASES_DIR)/$(TIMESTAMP) + @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" From e75db7dc9173bb1b5678d9e9aaf14271022c6346 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 20 Jan 2025 12:38:34 +0000 Subject: [PATCH 02/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .github/workflows/preview.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index 2db00ef6e..b138c8be1 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -11,7 +11,6 @@ jobs: PREVIEW_HOSTNAME: ep-preview.click steps: - - name: Checkout uses: actions/checkout@v4 with: From 161d3869e4faf63f2ffd155151402bcb34a866ee Mon Sep 17 00:00:00 2001 From: Artur Czepiel Date: Mon, 20 Jan 2025 13:44:19 +0100 Subject: [PATCH 03/12] fix variables --- .github/workflows/preview.yml | 3 +++ Makefile | 26 ++++++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index b138c8be1..051ef54d5 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -27,6 +27,9 @@ jobs: BRANCH_NAME=$(make safe_branch) 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 diff --git a/Makefile b/Makefile index b050828dd..694542848 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -47,22 +47,24 @@ 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: - RELEASES_DIR := $(VPS_PROD_PATH)/releases - TARGET := $(RELEASES_DIR)/$(TIMESTAMP) @echo "\n\n**** Deploying 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**** Deployment complete.\n\n" endif - -preview: - RELEASES_DIR := $(VPS_PREVIEW_PATH)/$(SAFE_BRANCH)/releases - TARGET := $(RELEASES_DIR)/$(TIMESTAMP) - @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" From 511dba1390e589c9148e66253a6acb62d15661a8 Mon Sep 17 00:00:00 2001 From: Artur Czepiel Date: Mon, 20 Jan 2025 13:48:19 +0100 Subject: [PATCH 04/12] update build for previews --- .github/workflows/preview.yml | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index 051ef54d5..886bab07a 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -13,9 +13,21 @@ jobs: 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 Node.js + uses: actions/setup-node@v4 with: - sparse-checkout: | - build + 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 From d0ef3a9e996a44a55a3fe9a42f313a85080ed2a2 Mon Sep 17 00:00:00 2001 From: Artur Czepiel Date: Mon, 20 Jan 2025 13:49:07 +0100 Subject: [PATCH 05/12] add pnpm --- .github/workflows/preview.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index 886bab07a..699973eb8 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -17,6 +17,12 @@ jobs: - 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: From b74598934a214354054b02bf0e7f51656983481b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 20 Jan 2025 12:49:20 +0000 Subject: [PATCH 06/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .github/workflows/preview.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index 699973eb8..d03a0dfa2 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -22,7 +22,6 @@ jobs: with: run_install: false - - name: Set up Node.js uses: actions/setup-node@v4 with: From aafe5719a2961875e2064cd6e6311b2f33338440 Mon Sep 17 00:00:00 2001 From: Artur Czepiel Date: Mon, 20 Jan 2025 14:18:48 +0100 Subject: [PATCH 07/12] trigger build --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index c5fa514f6..7bb7ebafa 100644 --- a/README.md +++ b/README.md @@ -70,3 +70,5 @@ import image from "./image.jpg"; ; ``` + +. From 6f24ebdc41c4210eec33cb0002b2bb25aa547563 Mon Sep 17 00:00:00 2001 From: Artur Czepiel Date: Mon, 20 Jan 2025 14:40:00 +0100 Subject: [PATCH 08/12] add explicit GITHUB_BRANCH_NAME - PR is working on a detached commit --- .github/workflows/preview.yml | 3 ++- Makefile | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index d03a0dfa2..4cc4aa3e3 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -9,6 +9,7 @@ jobs: runs-on: ubuntu-latest env: PREVIEW_HOSTNAME: ep-preview.click + GITHUB_BRANCH_NAME: ${{ github.head_ref || github.ref_name }} steps: - name: Checkout @@ -41,7 +42,7 @@ jobs: - name: Get current branch name run: | - BRANCH_NAME=$(make safe_branch) + BRANCH_NAME=$(make safe_branch BRANCH=$GITHUB_BRANCH_NAME) echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV - name: ssh keyscan diff --git a/Makefile b/Makefile index 694542848..612dfadee 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ 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 From 4cfb7b10a0ac44a1a6b58387bbc06f17a6206fe2 Mon Sep 17 00:00:00 2001 From: Artur Czepiel Date: Mon, 20 Jan 2025 15:09:10 +0100 Subject: [PATCH 09/12] change the color of the version in the footer to check if preview works correctly --- src/components/footer.astro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/footer.astro b/src/components/footer.astro index f7f8623c5..6ed351afa 100644 --- a/src/components/footer.astro +++ b/src/components/footer.astro @@ -93,6 +93,6 @@ const gitVersion = import.meta.env.GIT_VERSION; -

version: {gitVersion} @ {buildTimestamp}

+

version: {gitVersion} @ {buildTimestamp}

From 5e3c141055db8493d62f7feb205d09a59f3f6845 Mon Sep 17 00:00:00 2001 From: Artur Czepiel Date: Mon, 20 Jan 2025 15:14:04 +0100 Subject: [PATCH 10/12] pass correct github branch explicitly --- .github/workflows/preview.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index 4cc4aa3e3..8998fa3b1 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -49,7 +49,7 @@ jobs: run: ssh-keyscan "static.europython.eu" > ~/.ssh/known_hosts - name: Upload preview - run: make preview + run: make preview BRANCH=$GITHUB_BRANCH_NAME - name: Update PR Comment uses: actions/github-script@v6 From 30d6509619538dc3eb6ce784f67824fa096ece54 Mon Sep 17 00:00:00 2001 From: Artur Czepiel Date: Mon, 20 Jan 2025 17:08:45 +0100 Subject: [PATCH 11/12] Update README.md Remove the extra dot that was added just to trigger the build Co-authored-by: Cyril Bitterich --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 7bb7ebafa..c5fa514f6 100644 --- a/README.md +++ b/README.md @@ -70,5 +70,3 @@ import image from "./image.jpg"; ; ``` - -. From c0eca457f884ddcef855d462905715d837e017a6 Mon Sep 17 00:00:00 2001 From: Artur Czepiel Date: Mon, 20 Jan 2025 17:09:23 +0100 Subject: [PATCH 12/12] Update src/components/footer.astro Co-authored-by: Cyril Bitterich --- src/components/footer.astro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/footer.astro b/src/components/footer.astro index 6ed351afa..0d4aad64d 100644 --- a/src/components/footer.astro +++ b/src/components/footer.astro @@ -93,6 +93,6 @@ const gitVersion = import.meta.env.GIT_VERSION; -

version: {gitVersion} @ {buildTimestamp}

+

version: {gitVersion} @ {buildTimestamp}