Skip to content

Commit 4e71c27

Browse files
artczegeakmannikoshell
authored
check preview commit hash build (#1151)
Fixes #1143 --------- Co-authored-by: Ege Akman <me@egeakman.dev> Co-authored-by: Nikoś <nikoshell20@protonmail.com>
1 parent 7120471 commit 4e71c27

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
lines changed

.github/workflows/preview.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jobs:
1111
env:
1212
PREVIEW_HOSTNAME: ep-preview.click
1313
GITHUB_BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
14+
GITHUB_COMMIT_HASH: ${{ github.event.pull_request.head.sha }}
1415

1516
steps:
1617
- name: Checkout
@@ -33,15 +34,12 @@ jobs:
3334
- name: Install dependencies
3435
run: make install
3536

36-
- name: Get current branch name
37-
run: |
38-
BRANCH_NAME=$(make safe_branch BRANCH=$GITHUB_BRANCH_NAME)
39-
echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV
40-
4137
- name: Build the website
42-
run:
43-
make build MODE=preview
44-
SITE_URL="https://${BRANCH_NAME}.ep-preview.click"
38+
env:
39+
BRANCH: "${{ env.GITHUB_BRANCH_NAME }}"
40+
GIT_VERSION: "${{ env.GITHUB_COMMIT_HASH }}"
41+
MODE: "preview"
42+
run: make build
4543

4644
- name: Set up SSH key
4745
uses: webfactory/ssh-agent@v0.9.1
@@ -52,7 +50,9 @@ jobs:
5250
run: ssh-keyscan "static.europython.eu" > ~/.ssh/known_hosts
5351

5452
- name: Upload preview
55-
run: make preview BRANCH=$GITHUB_BRANCH_NAME
53+
env:
54+
BRANCH: "${{ env.GITHUB_BRANCH_NAME }}"
55+
run: make preview
5656

5757
- name: Update PR Comment
5858
uses: actions/github-script@v7
@@ -80,7 +80,7 @@ jobs:
8080
}
8181
});
8282
83-
const branch_name = process.env.BRANCH_NAME;
83+
const branch_name = process.env.GITHUB_BRANCH_NAME;
8484
const url = "https://" + branch_name + "." + process.env.PREVIEW_HOSTNAME;
8585
const timestamp = new Date().toISOString();
8686
const header = "\n|Key|Value|\n|---|---|\n"

astro.config.mjs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,31 @@ import deleteUnusedImages from "astro-delete-unused-images";
1313
import preload from "astro-preload";
1414
import { execSync } from "node:child_process";
1515

16-
let gitVersion = "";
17-
try {
18-
gitVersion = execSync("git rev-parse --short HEAD 2>&1 > /dev/null");
19-
} catch (e) {}
16+
let gitVersion = String(process.env.GIT_VERSION ?? "").slice(0, 7);
17+
18+
if (!gitVersion) {
19+
try {
20+
gitVersion = execSync("git rev-parse --short HEAD", {
21+
stdio: ["ignore", "pipe", "ignore"],
22+
})
23+
.toString()
24+
.trim();
25+
} catch {
26+
gitVersion = "unknown";
27+
}
28+
}
2029

2130
// https://astro.build/config
2231
export default defineConfig({
2332
vite: {
2433
define: {
25-
"import.meta.env.TIMESTAMP": new Date()
26-
.toISOString()
27-
.replace(/[-:T.Z]/g, "")
28-
.slice(0, 14),
29-
"import.meta.env.GIT_VERSION": new String(gitVersion),
34+
__TIMESTAMP__: JSON.stringify(
35+
new Date()
36+
.toISOString()
37+
.replace(/[-:T.Z]/g, "")
38+
.slice(0, 14)
39+
),
40+
__GIT_VERSION__: JSON.stringify(gitVersion),
3041
},
3142
resolve: {
3243
alias: {

src/components/BaseHead.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ interface Props {
55
image?: string;
66
}
77
8-
const buildTimestamp = import.meta.env.TIMESTAMP;
9-
const gitVersion = import.meta.env.GIT_VERSION;
8+
const buildTimestamp = __TIMESTAMP__;
9+
const gitVersion = __GIT_VERSION__;
1010
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
1111
1212
const { title, description, image = "/social-card.png" } = Astro.props;

src/components/Footer.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { Fullbleed } from "./layout/fullbleed";
44
import links from "../data/links.json";
55
import { EPSLogo } from "./logo/eps-logo";
66
7-
const buildTimestamp = import.meta.env.TIMESTAMP;
8-
const gitVersion = import.meta.env.GIT_VERSION;
7+
const buildTimestamp = __TIMESTAMP__;
8+
const gitVersion = __GIT_VERSION__;
99
---
1010

1111
<div class="mt-auto">

src/env.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ interface ImportMetaEnv {
1010
interface ImportMeta {
1111
readonly env: ImportMetaEnv;
1212
}
13+
14+
declare const __GIT_VERSION__: string;
15+
declare const __TIMESTAMP__: string;

0 commit comments

Comments
 (0)