一、管理 Jenkinsfile 脚本文件
将 Pipeline 脚本放入项目
- 目的:将 Pipeline 脚本与项目代码一起进行版本控制,便于维护和备份。
- 步骤:
- 在项目根目录创建
Jenkinsfile
文件。
- 将 Pipeline 脚本内容复制到
Jenkinsfile
中。
- 提交并推送到 GitLab。
示例:
cd /root/web_demo
vim Jenkinsfile
pipeline {
agent any
stages {
stage('拉取代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']],
userRemoteConfigs: [[url: 'git@192.168.80.10:devops_group/web_demo.git',
credentialsId: 'gitlab-auth-ssh']]])
}
}
stage('编译构建') {
steps {
sh 'mvn clean package'
}
}
stage('项目部署') {
steps {
deploy adapters: [tomcat9(credentialsId: 'tomcat-auth', path: '', url: 'http://192.168.80.12:8080')],
war: 'target/*.war'
}
}
}
}
提交代码:
git add .
git commit -m "添加 Jenkinsfile"
git push -u origin master
Jenkins 引用 Jenkinsfile
- 步骤:
- 进入 Jenkins 项目配置页面。
- 在 流水线 部分,选择 Pipeline script from SCM。
- 配置 Git 仓库地址和凭据。
- 指定分支和脚本路径(
Jenkinsfile
)。
- 保存并触发构建。
二、Jenkins 参数化构建
配置参数化构建
- 目的:通过用户输入动态传入参数(如分支名称),实现灵活构建。
- 步骤:
- 进入 Jenkins 项目配置页面。
- 勾选 This project is parameterized。
- 添加 String Parameter,名称设为
branch
,默认值为 master
。
- 修改
Jenkinsfile
,引用参数 ${branch}
。
示例:
pipeline {
agent any
stages {
stage('拉取代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']],
userRemoteConfigs: [[url: 'git@192.168.80.10:devops_group/web_demo.git',
credentialsId: 'gitlab-auth-ssh']]])
}
}
stage('编译构建') {
steps {
sh 'mvn clean package'
}
}
stage('项目部署') {
steps {
deploy adapters: [tomcat9(credentialsId: 'tomcat-auth', path: '', url: 'http://192.168.80.12:8080')],
war: 'target/*.war'
}
}
}
}
提交代码:
git add .
git commit -m "支持参数化构建"
git push -u origin master
2. 测试参数化构建
- 在 Jenkins 中点击 Build with Parameters,输入分支名称(如
v1
),触发构建。
三、配置邮箱服务器发送构建结果
安装插件
- 插件名称:
Email Extension Template
。
- 安装路径:Manage Jenkins → Manage Plugins → 可选插件。
配置邮箱服务器
- 步骤:
- 进入 Manage Jenkins → Configure System。
- 配置 Jenkins Location:
- Jenkins URL:
http://<Jenkins_IP>:8080
- 系统管理员邮件地址:
your-email@example.com
- 配置 Extended E-mail Notification:
- SMTP 服务器:
smtp.qq.com
- SMTP 端口:
465
- 使用 SSL。
- 配置凭据(邮箱账号和授权码)。
- 配置 邮件通知:
添加邮件模板
- 在项目根目录创建
email.html
文件,内容如下:
<html>
<body>
<h2>构建结果通知</h2>
<p>项目名称:${PROJECT_NAME}</p>
<p>构建编号:${BUILD_NUMBER}</p>
<p>构建状态:${BUILD_STATUS}</p>
</body>
</html>
修改 Jenkinsfile
- 在
Jenkinsfile
中添加 post
块,发送邮件:
pipeline {
agent any
stages {
}
post {
always {
emailext(
subject: '构建结果: ${PROJECT_NAME} - 构建 #${BUILD_NUMBER} - ${BUILD_STATUS}!',
body: '${FILE,path="email.html"}',
to: 'your-email@example.com'
)
}
}
}
提交代码:
git add .
git commit -m "添加邮件通知"
git push -u origin master
测试邮件通知
四、Jenkins + SonarQube 代码审查
安装 SonarQube
- 步骤:
- 安装 JDK 1.8 和 MySQL 5.7。
- 创建数据库
sonar
。
- 下载并解压 SonarQube。
- 修改
sonar.properties
配置文件:
- 数据库连接信息。
- Web 端口(默认 9000)。
- 启动 SonarQube:
su sonar ./bin/linux-x86-64/sonar.sh start
Jenkins 配置 SonarQube
- 步骤:
- 安装
SonarQube Scanner
插件。
- 配置 SonarQube 服务器:
- 进入 Manage Jenkins → Configure System → SonarQube servers。
- 添加 SonarQube 服务器地址和 Token。
- 配置 SonarQube Scanner 工具:
- 进入 Manage Jenkins → Global Tool Configuration。
- 添加 SonarQube Scanner,选择自动安装。
配置 SonarQube 项目
- 在项目根目录创建
sonar-project.properties
文件:
sonar.projectKey=web_demo
sonar.projectName=web_demo
sonar.projectVersion=1.0
sonar.sources=.
sonar.exclusions=**/test/**,**/target/**
sonar.java.binaries=.
sonar.java.source=1.8
sonar.java.target=1.8
sonar.sourceEncoding=UTF-8
修改 Jenkinsfile
pipeline {
agent any
stages {
stage('拉取代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']],
userRemoteConfigs: [[url: 'git@192.168.80.10:devops_group/web_demo.git',
credentialsId: 'gitlab-auth-ssh']]])
}
}
stage('SonarQube 代码审查') {
steps {
script {
scannerHome = tool 'sonar-scanner'
}
withSonarQubeEnv('sonarqube') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
stage('编译构建') {
steps {
sh 'mvn clean package'
}
}
stage('项目部署') {
steps {
deploy adapters: [tomcat9(credentialsId: 'tomcat-auth', path: '', url: 'http://192.168.80.12:8080')],
war: 'target/*.war'
}
}
}
}
提交代码:
git add .
git commit -m "添加 SonarQube 代码审查"
git push -u origin master
测试代码审查
- 在 Jenkins 中触发构建,查看 SonarQube 界面中的审查结果。