Files
lpt-fe/Jenkinsfile
T
cat-shark 6d05b37399 更新生产环境部署配置
- 修改 VITE_BASE_URL 配置为相对路径 /api
- 移除 Jenkinsfile 中的 HOST_PORT 环境变量
- 添加 Docker 网络创建命令到 Jenkins 构建步骤
- 配置 Traefik 标签实现服务路由和 TLS 证书
- 设置容器网络模式替代端口映射
- 添加健康检查配置确保服务可用性
2026-04-12 15:42:36 +08:00

96 lines
3.0 KiB
Groovy

pipeline {
agent none // 全局不指定,局部单独声明
environment {
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'
}
}
steps {
sh '''
npm config set cache /.npm
npm install
'''
}
}
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
'''
}
}
stage('Build Docker Image') {
agent any // Jenkins默认节点,有Docker命令
steps {
sh '''
docker build -t $IMAGE_NAME .
'''
}
}
stage('Run Docker Container') {
agent any
steps {
sh '''
docker network create traefik-public || true
docker rm -f $CONTAINER_NAME || true
docker run -d --name $CONTAINER_NAME \\
--network traefik-public \\
--label "traefik.enable=true" \\
--label "traefik.http.routers.lpt-fe.rule=Host(`lpt.cat-shark.xyz`)" \\
--label "traefik.http.routers.lpt-fe.entrypoints=websecure" \\
--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') {
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')
}
}
}
}
}
}
}