Compare commits

...

2 Commits

3 changed files with 88 additions and 16 deletions

49
.dockerignore Normal file
View File

@ -0,0 +1,49 @@
# Jenkins相关文件
Jenkinsfile*
jenkins-docker/
*.md
# Git相关
.git/
.gitignore
# IDE相关
.idea/
.vscode/
*.iml
*.ipr
*.iws
# 构建产物除了需要的jar文件
target/classes/
target/test-classes/
target/surefire-reports/
target/site/
target/maven-archiver/
target/maven-status/
# 日志文件
*.log
# 临时文件
*.tmp
*.swp
*~
# 系统文件
.DS_Store
Thumbs.db
# Docker相关
*.tar
docker-compose.yml
# 部署脚本
deploy.sh
server-setup*.sh
# 文档
JENKINS_*.md
SSH_CONFIG_GUIDE.md
ENTERPRISE_JENKINS_GUIDE.md
QUICK_START.md

View File

@ -1,5 +1,5 @@
# 使用多阶段构建优化镜像大小
FROM openjdk:17-jdk-slim as builder
FROM amazoncorretto:17-alpine-jdk as builder
# 设置工作目录
WORKDIR /app
@ -18,15 +18,19 @@ COPY src ./src
# 构建应用
RUN ./mvnw clean package -DskipTests
# 运行时镜像
FROM openjdk:17-jre-slim
# 运行时镜像 - 使用更小的Alpine镜像
FROM amazoncorretto:17-alpine
# 安装curl用于健康检查
RUN apk add --no-cache curl
# 设置时区
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apk add --no-cache tzdata && \
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# 创建非root用户
RUN groupadd -r spring && useradd -r -g spring spring
RUN addgroup -g 1000 spring && adduser -u 1000 -G spring -s /bin/sh -D spring
# 创建应用目录
WORKDIR /app

41
Jenkinsfile vendored
View File

@ -1,9 +1,8 @@
pipeline {
agent any
options {
options {
buildDiscarder(logRotator(numToKeepStr: '10'))
timeout(time: 30, unit: 'MINUTES')
timeout(time: 60, unit: 'MINUTES') // 增加到60分钟
timestamps()
}
@ -145,17 +144,37 @@ pipeline {
}
}
}
stage('构建Docker镜像') {
stage('构建Docker镜像') {
steps {
echo '🐳 构建Docker镜像...'
script {
def image = docker.build("${IMAGE_NAME}:${IMAGE_TAG}")
// 也创建latest标签
sh "docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${IMAGE_NAME}:latest"
echo "✅ Docker镜像构建完成: ${IMAGE_NAME}:${IMAGE_TAG}"
retry(2) {
try {
// 清理旧镜像以节省空间
sh 'docker image prune -f || true'
echo "开始构建Docker镜像: ${IMAGE_NAME}:${IMAGE_TAG}"
// 使用timeout包装Docker构建
timeout(time: 15, unit: 'MINUTES') {
def image = docker.build("${IMAGE_NAME}:${IMAGE_TAG}")
// 也创建latest标签
sh "docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${IMAGE_NAME}:latest"
}
echo "✅ Docker镜像构建完成: ${IMAGE_NAME}:${IMAGE_TAG}"
// 验证镜像是否创建成功
sh "docker images ${IMAGE_NAME}:${IMAGE_TAG}"
} catch (Exception e) {
echo "⚠️ Docker构建失败: ${e.getMessage()}"
// 清理可能的中间状态
sh 'docker system prune -f || true'
throw e
}
}
}
}
}