【CI/CD】Jenkinsfile管理+参数化构建+邮件通知以及Jenkins + SonarQube 代码审查

文章目录

一、管理 Jenkinsfile 脚本文件

将 Pipeline 脚本放入项目

  • 目的:将 Pipeline 脚本与项目代码一起进行版本控制,便于维护和备份。
  • 步骤
    1. 在项目根目录创建 Jenkinsfile 文件。
    2. 将 Pipeline 脚本内容复制到 Jenkinsfile 中。
    3. 提交并推送到 GitLab。

示例:

bash 复制代码
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'
            }
        }
    }
}

提交代码:

bash 复制代码
git add .
git commit -m "添加 Jenkinsfile"
git push -u origin master

Jenkins 引用 Jenkinsfile

  • 步骤
    1. 进入 Jenkins 项目配置页面。
    2. 流水线 部分,选择 Pipeline script from SCM
    3. 配置 Git 仓库地址和凭据。
    4. 指定分支和脚本路径(Jenkinsfile)。
    5. 保存并触发构建。

二、Jenkins 参数化构建

配置参数化构建

  • 目的:通过用户输入动态传入参数(如分支名称),实现灵活构建。
  • 步骤
    1. 进入 Jenkins 项目配置页面。
    2. 勾选 This project is parameterized
    3. 添加 String Parameter ,名称设为 branch,默认值为 master
    4. 修改 Jenkinsfile,引用参数 ${branch}

示例:

groovy 复制代码
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'
            }
        }
    }
}

提交代码:

bash 复制代码
git add .
git commit -m "支持参数化构建"
git push -u origin master

2. 测试参数化构建

  • 在 Jenkins 中点击 Build with Parameters ,输入分支名称(如 v1),触发构建。

三、配置邮箱服务器发送构建结果

安装插件

  • 插件名称Email Extension Template
  • 安装路径:Manage Jenkins → Manage Plugins → 可选插件。

配置邮箱服务器

  • 步骤
    1. 进入 Manage Jenkins → Configure System
    2. 配置 Jenkins Location
      • Jenkins URL:http://<Jenkins_IP>:8080
      • 系统管理员邮件地址:your-email@example.com
    3. 配置 Extended E-mail Notification
      • SMTP 服务器:smtp.qq.com
      • SMTP 端口:465
      • 使用 SSL。
      • 配置凭据(邮箱账号和授权码)。
    4. 配置 邮件通知
      • 同上,配置 SMTP 服务器和凭据。

添加邮件模板

  • 在项目根目录创建 email.html 文件,内容如下:
html 复制代码
<html>
<body>
<h2>构建结果通知</h2>
<p>项目名称:${PROJECT_NAME}</p>
<p>构建编号:${BUILD_NUMBER}</p>
<p>构建状态:${BUILD_STATUS}</p>
</body>
</html>

修改 Jenkinsfile

  • Jenkinsfile 中添加 post 块,发送邮件:
groovy 复制代码
pipeline {
    agent any
    stages {
        // 原有阶段
    }
    post {
        always {
            emailext(
                subject: '构建结果: ${PROJECT_NAME} - 构建 #${BUILD_NUMBER} - ${BUILD_STATUS}!',
                body: '${FILE,path="email.html"}',
                to: 'your-email@example.com'
            )
        }
    }
}

提交代码:

bash 复制代码
git add .
git commit -m "添加邮件通知"
git push -u origin master

测试邮件通知

  • 触发构建,查看邮箱是否收到构建结果通知。

四、Jenkins + SonarQube 代码审查

安装 SonarQube

  • 步骤
    1. 安装 JDK 1.8 和 MySQL 5.7。

    2. 创建数据库 sonar

    3. 下载并解压 SonarQube。

    4. 修改 sonar.properties 配置文件:

      • 数据库连接信息。
      • Web 端口(默认 9000)。
    5. 启动 SonarQube:

      bash 复制代码
      su sonar ./bin/linux-x86-64/sonar.sh start

Jenkins 配置 SonarQube

  • 步骤
    1. 安装 SonarQube Scanner 插件。
    2. 配置 SonarQube 服务器:
      • 进入 Manage Jenkins → Configure System → SonarQube servers
      • 添加 SonarQube 服务器地址和 Token。
    3. 配置 SonarQube Scanner 工具:
      • 进入 Manage Jenkins → Global Tool Configuration
      • 添加 SonarQube Scanner,选择自动安装。

配置 SonarQube 项目

  • 在项目根目录创建 sonar-project.properties 文件:
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

  • 添加 SonarQube 代码审查阶段:
groovy 复制代码
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'
            }
        }
    }
}

提交代码:

bash 复制代码
git add .
git commit -m "添加 SonarQube 代码审查"
git push -u origin master

测试代码审查

  • 在 Jenkins 中触发构建,查看 SonarQube 界面中的审查结果。
相关推荐
江湖有缘44 分钟前
Jump个人仪表盘Docker化部署教程:从0到 搭建专属导航页
运维·docker·容器
FL16238631291 小时前
win11+WSL+Ubuntu-xrdp+远程桌面闪退+黑屏闪退解决
linux·运维·ubuntu
挖土机_0082 小时前
Kubernetes 1.35 原地扩容(In-Place Pod Resize)完整解析:机制、差异与实战示例
docker·kubernetes
AOwhisky2 小时前
Linux逻辑卷管理:从“固定隔间”到“弹性存储池”的智慧
linux·运维·服务器
05大叔3 小时前
大事件Day02
运维·服务器
五仁火烧3 小时前
Vue3 项目的默认端口行为
服务器·vue.js·nginx·容器·vue
C Yu小白3 小时前
Linux系统调用与文件操作详解
linux·运维·服务器
ZFB00013 小时前
【麒麟桌面系统】V10-SP1 2503 系统知识——常见用户组简介
linux·运维·kylin
acrelgxy3 小时前
告别被动抢修与盲目巡检!安科瑞运维云平台,让电力系统实现预测性守护。
运维·电力监控系统·智能电力仪表
f***28144 小时前
Springboot中使用Elasticsearch(部署+使用+讲解 最完整)
spring boot·elasticsearch·jenkins