【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 界面中的审查结果。
相关推荐
EMTime2 小时前
Docker运行OpenWRT
运维·docker·容器
lolo大魔王3 小时前
Linux 文件系统超全面详解(原理、结构、挂载、分区、inode、日志、管理命令)
linux·运维·服务器
zyl837215 小时前
Docker 使用手册
运维·docker·容器
古月方枘Fry5 小时前
MGRE实验
运维·服务器
stolentime6 小时前
FreeDomain 本地开发环境快速搭建指南
运维·服务器·网络
bush47 小时前
嵌入式linux学习记录四
linux·运维·学习
maomao大哥闯天下8 小时前
K8s如何实现滚动更新、健康检查与探测机制
docker·容器·kubernetes
lihao lihao8 小时前
软硬链接
linux·运维·服务器
TOWE technology8 小时前
智能安防监控系统如何做好防雷?——视频信号SPD综合应用方案解析
运维·服务器·防雷产品·信号保护·信号防雷·spd
楼田莉子8 小时前
Docker学习:Docker介绍及其架构介绍
运维·后端·学习·docker·容器·架构