From 2b26cd47ec14333cc524694d85f24a2c1c970827 Mon Sep 17 00:00:00 2001 From: CaoMeiYouRen <996881204@qq.com> Date: Mon, 4 Nov 2024 17:44:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=20=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslintrc.js | 4 +++- .github/workflows/docker.yml | 8 ++++++++ .github/workflows/release.yml | 10 ++++++++++ Dockerfile | 2 ++ src/main.ts | 21 ++++++++++++++------- src/utils/git-info.ts | 27 +++++++++++++++++++++++++++ 6 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 src/utils/git-info.ts diff --git a/.eslintrc.js b/.eslintrc.js index 9ff61fc9..178bfb24 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,4 +1,6 @@ const IS_PROD = process.env.NODE_ENV === 'production' +const __ERROR__ = process.env.NODE_ENV === 'production' ? 2 : 0 +const __WARN__ = process.env.NODE_ENV === 'production' ? 1 : 0 module.exports = { root: true, globals: { @@ -15,7 +17,7 @@ module.exports = { 'no-console': 0, '@typescript-eslint/indent': 0, 'max-len': [0, { code: 200 }], // 强制行的最大长度 - 'max-lines': [1, { max: 500 }], // 强制文件的最大行数 + 'max-lines': [__WARN__, { max: 500 }], // 强制文件的最大行数 'max-lines-per-function': [0, { max: 120 }], // 强制函数最大行数 'max-nested-callbacks': [1, { max: 5 }], // 强制回调函数最大嵌套深度 'max-params': [1, { max: 5 }], // 强制函数定义中最大参数个数 diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 8c4eff22..c5c892bf 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -60,6 +60,12 @@ jobs: run: echo "REPO=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV} - name: Get short commit hash run: echo "TAG=sha-${GITHUB_SHA::7}" >>${GITHUB_ENV} + - name: Get git info + run: | + GIT_HASH=$(git rev-parse --short HEAD) + echo "GIT_HASH=$GIT_HASH" >> ${GITHUB_ENV} + GIT_DATE=$(git log -1 --format=%cd) + echo "GIT_DATE=$GIT_DATE" >> ${GITHUB_ENV} - name: Set up QEMU uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx @@ -108,6 +114,8 @@ jobs: VITE_BAIDU_STATISTICS=${{ secrets.VITE_BAIDU_STATISTICS }} VITE_DOMAIN=${{ secrets.VITE_DOMAIN }} VITE_SENTRY_DSN=${{ secrets.VITE_SENTRY_DSN }} + GIT_HASH=${{ env.GIT_HASH }} + GIT_DATE=${{ env.GIT_DATE }} - name: Docker Hub README & description sync uses: meeDamian/sync-readme@v1.0.6 with: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4d8f4d40..682ab85f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -62,6 +62,10 @@ jobs: run: | GIT_TAG=$(git describe --tags --exact-match HEAD 2>/dev/null || true) echo "GIT_TAG=$GIT_TAG" >> ${GITHUB_ENV} + GIT_HASH=$(git rev-parse --short HEAD) + echo "GIT_HASH=$GIT_HASH" >> ${GITHUB_ENV} + GIT_DATE=$(git log -1 --format=%cd) + echo "GIT_DATE=$GIT_DATE" >> ${GITHUB_ENV} - name: Set up QEMU uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx @@ -109,3 +113,9 @@ jobs: tags: ${{ steps.metadata.outputs.tags }} labels: ${{ steps.metadata.outputs.labels }} platforms: linux/amd64,linux/arm64 + build-args: | + VITE_BAIDU_STATISTICS=${{ secrets.VITE_BAIDU_STATISTICS }} + VITE_DOMAIN=${{ secrets.VITE_DOMAIN }} + VITE_SENTRY_DSN=${{ secrets.VITE_SENTRY_DSN }} + GIT_HASH=${{ env.GIT_HASH }} + GIT_DATE=${{ env.GIT_DATE }} diff --git a/Dockerfile b/Dockerfile index 5c93f55a..3db1ee31 100644 --- a/Dockerfile +++ b/Dockerfile @@ -55,6 +55,8 @@ RUN export PROJECT_ROOT=/app/ && \ FROM runtime ENV NODE_ENV production +ENV GIT_HASH = ${GIT_HASH} +ENV GIT_DATE = ${GIT_DATE} WORKDIR /app # 后端部分 diff --git a/src/main.ts b/src/main.ts index 9c59119b..a35d1566 100644 --- a/src/main.ts +++ b/src/main.ts @@ -12,14 +12,16 @@ import fs from 'fs-extra' import artTemplate from 'art-template' import ms from 'ms' import { Request } from 'express' -import { setRequestId } from './middlewares/request.middleware' -import { sessionMiddleware } from './middlewares/session.middleware' -import { jsonLogger, logger } from './middlewares/logger.middleware' -import { TimeoutInterceptor } from './interceptors/timeout.interceptor' -import { limiter } from './middlewares/limit.middleware' -import { AllExceptionsFilter } from './filters/all-exceptions.filter' -import { AppModule } from './app.module' import { DATABASE_DIR } from './db/database.module' +import { AppModule } from './app.module' +import { AllExceptionsFilter } from './filters/all-exceptions.filter' +import { limiter } from './middlewares/limit.middleware' +import { TimeoutInterceptor } from './interceptors/timeout.interceptor' +import { jsonLogger, logger } from './middlewares/logger.middleware' +import { sessionMiddleware } from './middlewares/session.middleware' +import { setRequestId } from './middlewares/request.middleware' +import { getGitInfo } from './utils/git-info' +import { timeFormat } from './utils/helper' moduleAlias.addAlias('@', path.join(__dirname, './')) @@ -113,6 +115,11 @@ async function bootstrap() { if (__DEV__) { logger.debug(`Docs http://127.0.0.1:${PORT}/docs`) } + const pkg = await fs.readJson(path.join(__dirname, '../package.json')) + const version = pkg.version + logger.log(`当前 RSS Impact server 版本为:${version}`) + const { gitHash, gitDate } = getGitInfo() + logger.log(`当前 RSS Impact server 构建哈希为:${gitHash},构建时间为:${timeFormat(gitDate)}`) if (CI && __BENCHMARKS_TEST__) { setTimeout(() => { diff --git a/src/utils/git-info.ts b/src/utils/git-info.ts new file mode 100644 index 00000000..75ca4aa8 --- /dev/null +++ b/src/utils/git-info.ts @@ -0,0 +1,27 @@ +import { execSync } from 'child_process' + +interface GitInfo { + gitHash?: string + gitDate?: string +} + +function getGitInfo(): GitInfo { + let gitHash = process.env.GIT_HASH + let gitDate = process.env.GIT_DATE + + if (!gitHash) { + try { + gitHash = execSync('git rev-parse HEAD').toString().trim().slice(0, 8) + const gitDateStr = execSync('git log -1 --format=%cd').toString().trim() + gitDate = new Date(gitDateStr).toISOString() + } catch (error) { + console.error('Failed to get Git commit hash and date:', error) + gitHash = 'unknown' + gitDate = 'unknown' + } + } + + return { gitHash, gitDate } +} + +export { getGitInfo }