【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: '[email protected]: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: '[email protected]: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
    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: '[email protected]'
            )
        }
    }
}

提交代码:

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: '[email protected]: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 界面中的审查结果。
相关推荐
Wnq100722 小时前
智能巡检机器人在化工企业的应用研究
运维·计算机视觉·机器人·智能硬件·deepseek
tf的测试笔记5 小时前
测试团队UI自动化实施方案
运维·自动化
TDD_06285 小时前
【运维】Centos硬盘满导致开机时处于加载状态无法开机解决办法
linux·运维·经验分享·centos
头孢头孢5 小时前
k8s常用总结
运维·后端·k8s
遇码5 小时前
单机快速部署开源、免费的分布式任务调度系统——DolphinScheduler
大数据·运维·分布式·开源·定时任务·dolphin·scheduler
爱编程的王小美6 小时前
Docker基础详解
运维·docker·容器
白夜易寒6 小时前
Docker学习之容器虚拟化与虚拟机的区别(day11)
学习·docker·容器
学习至死qaq6 小时前
windows字体在linux访问异常
linux·运维·服务器
IEVEl7 小时前
Centos7 安装 TDengine
运维·centos·时序数据库·tdengine
在野靡生.7 小时前
Ansible(4)—— Playbook
linux·运维·ansible