Features: - Enable Docker Buildx with advanced caching - Add BuildKit cache mount for Maven dependencies - Use Aliyun Maven mirror for faster dependency downloads - Optimize Docker layer caching strategy - Add automatic cache cleanup to prevent disk overflow - Increase build timeout to 30 minutes - Add buildx builder instance management Performance improvements: - Parallel builds with Buildx - Persistent Maven cache with RUN --mount=type=cache - Layer-optimized Dockerfile structure - Automatic cache rotation This should significantly reduce Docker build time and improve reliability.
Jenkins Demo - Spring Boot 3 CI/CD 实践项目
这是一个用于实践Jenkins CI/CD流程的Spring Boot 3示例项目。项目包含完整的代码拉取、编译构建、单元测试、代码扫描、Docker打包和部署流程。
🚀 项目特性
- Spring Boot 3.2.6 + JDK 17
- RESTful API - 提供用户管理相关接口
- 单元测试 - 完整的JUnit 5测试覆盖
- 代码质量 - 集成SonarQube代码扫描
- Docker支持 - 多阶段构建优化
- Jenkins Pipeline - 完整的CI/CD流程
- 健康检查 - 应用程序监控端点
📋 API接口
健康检查
GET /api/health
- 应用健康状态GET /api/info
- 应用信息GET /api/welcome
- 欢迎信息
用户管理
GET /api/users
- 获取所有用户GET /api/users/{id}
- 获取指定用户POST /api/users
- 创建新用户PUT /api/users/{id}
- 更新用户信息DELETE /api/users/{id}
- 删除用户POST /api/users/{id}/activate
- 激活用户POST /api/users/{id}/deactivate
- 停用用户GET /api/users/stats
- 用户统计信息
🛠️ 技术栈
- 后端框架: Spring Boot 3.2.6
- Java版本: JDK 17
- 构建工具: Maven 3.9+
- 测试框架: JUnit 5 + MockMvc
- 代码覆盖率: JaCoCo
- 容器化: Docker + Docker Compose
- CI/CD: Jenkins Pipeline
🏃♂️ 快速开始
本地开发
-
克隆项目
git clone <repository-url> cd jenkins-demo
-
编译项目
mvn clean compile
-
运行测试
mvn test
-
启动应用
mvn spring-boot:run
-
访问应用
Docker运行
-
构建镜像
docker build -t jenkins-demo:latest .
-
运行容器
docker run -d --name jenkins-demo -p 8080:8080 jenkins-demo:latest
-
使用Docker Compose
docker-compose up -d
🔧 Maven命令
# 编译项目
mvn clean compile
# 运行测试
mvn test
# 生成代码覆盖率报告
mvn test jacoco:report
# 打包应用
mvn clean package
# 跳过测试打包
mvn clean package -DskipTests
# 代码质量扫描(需要配置SonarQube)
mvn sonar:sonar
# 启动应用
mvn spring-boot:run
🔍 代码质量
项目集成了以下代码质量工具:
- JaCoCo - 代码覆盖率分析
- SonarQube - 静态代码分析
- Maven Surefire - 单元测试报告
生成测试报告:
mvn clean test jacoco:report
报告位置:target/site/jacoco/index.html
🚀 Jenkins CI/CD流程
Pipeline阶段
- 代码检出 - 从Git仓库拉取代码
- 环境检查 - 验证Java、Maven等工具版本
- 编译 - 编译Java源代码
- 单元测试 - 运行JUnit测试并生成报告
- 代码扫描 - SonarQube静态代码分析
- 打包 - 生成可执行JAR文件
- Docker构建 - 构建Docker镜像
- 镜像推送 - 推送到Docker Registry
- 自动部署 - 部署到目标环境
- 健康检查 - 验证应用启动状态
环境要求
Jenkins服务器需要安装:
- JDK 17
- Maven 3.9+
- Docker
- Git
- SonarQube Scanner(可选)
部署配置
修改Jenkinsfile
中的以下配置:
environment {
DOCKER_REGISTRY = 'your-registry.com' // Docker仓库地址
SONAR_HOST_URL = 'http://your-sonar-server:9000' // SonarQube服务器
// ... 其他配置
}
📦 项目结构
jenkins-demo/
├── src/
│ ├── main/
│ │ ├── java/com/jenkins/demo/
│ │ │ ├── JenkinsDemoApplication.java # 应用主类
│ │ │ ├── controller/ # REST控制器
│ │ │ ├── service/ # 业务服务
│ │ │ └── model/ # 数据模型
│ │ └── resources/
│ │ └── application.yml # 应用配置
│ └── test/ # 测试代码
├── Dockerfile # Docker镜像构建
├── Jenkinsfile # Jenkins Pipeline
├── docker-compose.yml # Docker Compose配置
├── pom.xml # Maven配置
└── README.md # 项目文档
🔗 API示例
创建用户
curl -X POST http://localhost:8080/api/users \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"email": "test@example.com",
"name": "测试用户"
}'
获取所有用户
curl http://localhost:8080/api/users
健康检查
curl http://localhost:8080/api/health
📊 监控端点
Spring Boot Actuator提供了以下监控端点:
/actuator/health
- 健康状态/actuator/info
- 应用信息/actuator/metrics
- 应用指标/actuator/prometheus
- Prometheus指标(如果启用)
🤝 贡献指南
- Fork项目
- 创建特性分支 (
git checkout -b feature/amazing-feature
) - 提交更改 (
git commit -m 'Add some amazing feature'
) - 推送到分支 (
git push origin feature/amazing-feature
) - 开启Pull Request
📄 许可证
本项目使用MIT许可证。详情请参见LICENSE文件。
📞 联系方式
如有问题或建议,请通过以下方式联系:
- 项目Issues: GitHub Issues
- 邮箱: your-email@example.com
🎉 祝您使用愉快! 如果这个项目对您有帮助,请给个⭐Star支持一下!
Description
Languages
Shell
47.2%
Java
35%
Groovy
9.5%
Dockerfile
5.7%
Batchfile
2.6%