From 5ab36a08b2dcbc19363f4b867ff8bdc2ee32cbc6 Mon Sep 17 00:00:00 2001 From: diced Date: Tue, 6 May 2025 01:14:13 -0700 Subject: [PATCH] feat: expose git sha in versioning api --- .github/workflows/docker-release.yml | 2 ++ .github/workflows/docker.yml | 2 ++ Dockerfile | 4 ++++ package.json | 3 ++- src/lib/version.ts | 29 ++++++++++++++++++++++++++++ src/server/routes/api/version.ts | 11 +++++------ 6 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 src/lib/version.ts diff --git a/.github/workflows/docker-release.yml b/.github/workflows/docker-release.yml index 9adb3d65..e1dcae63 100644 --- a/.github/workflows/docker-release.yml +++ b/.github/workflows/docker-release.yml @@ -46,6 +46,8 @@ jobs: cache-from: type=gha cache-to: type=gha,mode=max provenance: false + build-args: | + ZIPLINE_GIT_SHA=${{ steps.sha.outputs.short_sha }} tags: | ghcr.io/diced/zipline:${{ steps.version.outputs.zipline_version }}-${{ matrix.arch }} ghcr.io/diced/zipline:${{ steps.version.outputs.zipline_version }}-${{ steps.sha.outputs.short_sha }}-${{ matrix.arch }} diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index c7879932..c77574d1 100755 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -40,6 +40,8 @@ jobs: cache-from: type=gha cache-to: type=gha,mode=max provenance: false + build-args: | + ZIPLINE_GIT_SHA=${{ steps.sha.outputs.short_sha }} tags: | ghcr.io/diced/zipline:trunk-${{ matrix.arch }} ghcr.io/diced/zipline:trunk-${{ steps.sha.outputs.short_sha }}-${{ matrix.arch }} diff --git a/Dockerfile b/Dockerfile index 109a2ae6..9ef427b7 100755 --- a/Dockerfile +++ b/Dockerfile @@ -42,6 +42,7 @@ COPY --from=builder /zipline/mimes.json ./mimes.json COPY --from=builder /zipline/code.json ./code.json COPY --from=builder /zipline/generated ./generated + RUN pnpm build:prisma # clean @@ -49,4 +50,7 @@ RUN rm -rf /tmp/* /root/* ENV NODE_ENV=production +ARG ZIPLINE_GIT_SHA +ENV ZIPLINE_GIT_SHA=${ZIPLINE_GIT_SHA:-"unknown"} + CMD ["node", "--enable-source-maps", "build/server"] diff --git a/package.json b/package.json index 721936a0..5dac4ca1 100755 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "db:prototype": "prisma db push --skip-generate && prisma generate --no-hints", "db:migrate": "prisma migrate dev --create-only", "docker:engine": "colima start --mount $PWD/themes:w --mount $PWD/uploads:w --mount $PWD/public:w", - "docker:compose:dev:up": "docker-compose --file docker-compose.dev.yml up --build -d", + "docker:compose:dev:build": "docker-compose --file docker-compose.dev.yml build --build-arg ZIPLINE_GIT_SHA=$(git rev-parse HEAD)", + "docker:compose:dev:up": "docker-compose --file docker-compose.dev.yml up -d", "docker:compose:dev:down": "docker-compose --file docker-compose.dev.yml down", "docker:compose:dev:logs": "docker-compose --file docker-compose.dev.yml logs -f" }, diff --git a/src/lib/version.ts b/src/lib/version.ts new file mode 100644 index 00000000..07c2e148 --- /dev/null +++ b/src/lib/version.ts @@ -0,0 +1,29 @@ +// TODO: implement version checking against the API at https://zipline.diced.sh + +import { version } from '../../package.json'; + +export function gitSha() { + const envValue = process.env.ZIPLINE_GIT_SHA; + if (envValue && envValue !== 'unknown') return envValue; + + try { + const { execSync } = require('child_process'); + const commitHash = execSync('git rev-parse --short HEAD').toString().trim(); + return commitHash; + } catch (error) { + console.error('Error getting git commit hash:', error); + return null; + } +} + +export function getVersion(): { + version: string; + sha: string | null; +} { + const sha = gitSha(); + + return { + version, + sha, + }; +} diff --git a/src/server/routes/api/version.ts b/src/server/routes/api/version.ts index fe919a3e..9650d8b5 100755 --- a/src/server/routes/api/version.ts +++ b/src/server/routes/api/version.ts @@ -1,7 +1,6 @@ -import { administratorMiddleware } from '@/server/middleware/administrator'; import { userMiddleware } from '@/server/middleware/user'; import fastifyPlugin from 'fastify-plugin'; -import { version } from '../../../../package.json'; +import { getVersion } from '@/lib/version'; export type ApiVersionResponse = { version: string; @@ -10,10 +9,10 @@ export type ApiVersionResponse = { export const PATH = '/api/version'; export default fastifyPlugin( (server, _, done) => { - server.get(PATH, { preHandler: [userMiddleware, administratorMiddleware] }, async (_, res) => { - return res.send({ - version, - }); + server.get(PATH, { preHandler: [userMiddleware] }, async (_, res) => { + const details = getVersion(); + + return res.send(details); }); done();