Skip to content

Commit

Permalink
feat: 新增 构建信息 输出
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Nov 4, 2024
1 parent 0172027 commit 2b26cd4
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 8 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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 }], // 强制函数定义中最大参数个数
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/[email protected]
with:
Expand Down
10 changes: 10 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }}
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
# 后端部分
Expand Down
21 changes: 14 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, './'))

Expand Down Expand Up @@ -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(() => {
Expand Down
27 changes: 27 additions & 0 deletions src/utils/git-info.ts
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit 2b26cd4

Please sign in to comment.