From c1f00378f20b8c9f3653989d6a52a997c5a1c788 Mon Sep 17 00:00:00 2001 From: cat_shark <1716967236@qq.com> Date: Sat, 30 May 2026 10:00:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=A4=9A=E9=98=B6=E6=AE=B5?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=EF=BC=8CDocker=20=E5=B1=82=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E7=AE=A1=E7=90=86=20npm=20install?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 10 +++++++ Dockerfile | 19 +++++++++---- Jenkinsfile | 75 +++++++-------------------------------------------- 3 files changed, 34 insertions(+), 70 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..54544cd --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +node_modules +dist +.git +.gitignore +*.md +.DS_Store +coverage +.nyc_output +playwright-report +test-results diff --git a/Dockerfile b/Dockerfile index d876b6c..6f6bf2f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,19 @@ +# --- 构建阶段 --- +FROM node:20-alpine AS builder +WORKDIR /app + +# 先复制依赖描述文件,利用层缓存 +COPY package.json package-lock.json ./ +RUN npm install + +# 再复制源码,构建 +COPY . . +RUN npm run type-check && npm run build + +# --- 生产阶段 --- FROM nginx:alpine - -# 安装 curl 用于健康检查 RUN apk add --no-cache curl - -COPY dist/ /usr/share/nginx/html/ +COPY --from=builder /app/dist/ /usr/share/nginx/html/ RUN chmod -R 755 /usr/share/nginx/html COPY nginx.conf /etc/nginx/conf.d/default.conf - EXPOSE 80 diff --git a/Jenkinsfile b/Jenkinsfile index 84eee9b..db3ea02 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,54 +1,21 @@ pipeline { - agent none // 全局不指定,局部单独声明 + agent none environment { - IMAGE_NAME = 'lpt-fe:0.0' + IMAGE_NAME = 'lpt-fe:0.0' CONTAINER_NAME = 'LPT_FE' CONTAINER_PORT = '80' } stages { - stage('Install Dependencies') { - agent { - docker { - image 'node:20-alpine' - args '-v ${WORKSPACE}/.npm:/.npm -v ${WORKSPACE}/node_modules:/app/node_modules' - } - } + stage('构建 Docker 镜像') { + agent any steps { - sh ''' - npm config set cache /.npm - npm install - ''' + sh 'docker build -t $IMAGE_NAME .' } } - - stage('Type Check & Build') { - agent { - docker { - image 'node:20-alpine' - args '-v ${WORKSPACE}/.npm:/.npm -v ${WORKSPACE}/node_modules:/app/node_modules' - } - } + stage('部署容器') { + agent any steps { - sh ''' - npm run type-check - npm run build - ''' - } - } - - stage('Build Docker Image') { - agent any // Jenkins默认节点,有Docker命令 - steps { - sh ''' - docker build -t $IMAGE_NAME . - ''' - } - } - - stage('Run Docker Container') { - agent any - steps { - sh ''' + sh ''' docker network create traefik-public || true docker rm -f $CONTAINER_NAME || true docker run -d --name $CONTAINER_NAME \\ @@ -60,36 +27,14 @@ pipeline { --label "traefik.http.routers.lpt-fe.tls.certresolver=le" \\ --label "traefik.http.routers.lpt-fe.service=lpt-fe" \\ --label "traefik.http.services.lpt-fe.loadbalancer.server.port=$CONTAINER_PORT" \\ - --health-cmd="curl -f http://localhost:80 || exit 1" \\ - --health-interval=5s \\ - --health-retries=3 \\ - --restart=always \\ $IMAGE_NAME ''' } } - - stage('Check Docker Status') { + stage('健康检查') { agent any steps { - script { - def lastStatus = '' - timeout(time: 60, unit: 'SECONDS') { - waitUntil { - def status = sh( - script: "docker inspect -f '{{.State.Health.Status}}' $CONTAINER_NAME || echo 'unhealthy'", - returnStdout: true - ).trim() - - if (status != lastStatus) { - echo "Container health: ${status}" - lastStatus = status - } - - return (status == 'healthy') - } - } - } + sh 'sleep 5 && docker exec $CONTAINER_NAME curl -f http://localhost:$CONTAINER_PORT/ || (docker logs $CONTAINER_NAME && exit 1)' } } }