Jenkinsfile-dev 也改为多阶段构建,Dockerfile 支持 BUILD_MODE 参数
This commit is contained in:
+3
-1
@@ -2,13 +2,15 @@
|
||||
FROM node:20-alpine AS builder
|
||||
WORKDIR /app
|
||||
|
||||
ARG BUILD_MODE=production
|
||||
|
||||
# 先复制依赖描述文件,利用层缓存
|
||||
COPY package.json package-lock.json ./
|
||||
RUN npm install
|
||||
|
||||
# 再复制源码,构建
|
||||
COPY . .
|
||||
RUN npm run type-check && npm run build
|
||||
RUN npm run type-check && npm run build:${BUILD_MODE}
|
||||
|
||||
# --- 生产阶段 ---
|
||||
FROM nginx:alpine
|
||||
|
||||
Vendored
+1
-1
@@ -9,7 +9,7 @@ pipeline {
|
||||
stage('构建 Docker 镜像') {
|
||||
agent any
|
||||
steps {
|
||||
sh 'docker build -t $IMAGE_NAME .'
|
||||
sh 'docker build --build-arg BUILD_MODE=production -t $IMAGE_NAME .'
|
||||
}
|
||||
}
|
||||
stage('部署容器') {
|
||||
|
||||
+7
-62
@@ -1,51 +1,18 @@
|
||||
pipeline {
|
||||
agent none // 全局不指定,局部单独声明
|
||||
agent none
|
||||
environment {
|
||||
IMAGE_NAME = 'lpt-fe-dev:0.0'
|
||||
CONTAINER_NAME = 'LPT_FE-dev'
|
||||
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 --build-arg BUILD_MODE=dev -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'
|
||||
}
|
||||
}
|
||||
steps {
|
||||
sh '''
|
||||
npm run type-check
|
||||
npm run build:dev
|
||||
'''
|
||||
}
|
||||
}
|
||||
|
||||
stage('Build Docker Image') {
|
||||
agent any // Jenkins默认节点,有Docker命令
|
||||
steps {
|
||||
sh '''
|
||||
docker build -t $IMAGE_NAME .
|
||||
'''
|
||||
}
|
||||
}
|
||||
|
||||
stage('Run Docker Container') {
|
||||
stage('部署容器') {
|
||||
agent any
|
||||
steps {
|
||||
sh '''
|
||||
@@ -60,36 +27,14 @@ pipeline {
|
||||
--label "traefik.http.routers.lpt-fe-dev.tls.certresolver=le" \\
|
||||
--label "traefik.http.routers.lpt-fe-dev.service=lpt-fe-dev" \\
|
||||
--label "traefik.http.services.lpt-fe-dev.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)'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user