持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第28天
1.老项目改造实现pipline项目自动上线
1.1.通过jenkins获取流水线语法
1.1.1.获取从gitlab上拉取项目的语法
拉取gitlab上的代码可以通过jenkins获取流水线语法最后粘贴到脚本中
点击配置---高级项目选项---流水线---流水线语法
配置拉取代码的信息
点击生成流水线脚本
1.1.2.获取钉钉报警语法
1.2.使用git parameters参数构建
参数化构建使用blue ocean会报错,会提示不支持,但是不影响构建
2.编写pipeline脚本
pipeline{
agent any
stages {
stage('获取代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '${git_version}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '9847113e-e313-4dc6-9302-dc4dec14804b', url: 'git@gitlab.jiangxl.com:root/monitor.git']]])
}
}
stage('质量扫描'){
steps {
sh '/usr/local/sonar-scanner-4.0.0.1744-linux/bin/sonar-scanner -Dsonar.projectKey=${JOB_NAME} -Dsonar.sources=.'
}
}
stage('编译代码'){
steps {
echo "build code ok"
//sh 'mvn package'
}
}
stage('部署代码'){
steps {
sh 'sh -x /script/monitor_deploy_tag.sh'
}
}
}
}
添加一个Git parameters参数化构建
3.将代码填写到项目中
4.构建选择标签即可
每一步都可以看到日志
5.使用pipeline语法中的parameters语法构建
利用pipeline语法中的parameters语法构建的话,需要将之前添加的git parameters参数删除,并且使用blue ocean去构建
5.1.脚本编写
pipeline{
agent any
parameters {
string(name: 'git_version', defaultValue: 'v1.0', description: '输入tag版本')
}
stages { //一个大的任务合集 (部署代码)
stage('获取代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '${git_version}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '9847113e-e313-4dc6-9302-dc4dec14804b', url: 'git@gitlab.jiangxl.com:root/monitor.git']]])
}
}
stage('质量扫描'){
steps {
sh '/usr/local/sonar-scanner-4.0.0.1744-linux/bin/sonar-scanner -Dsonar.projectKey=${JOB_NAME} -Dsonar.sources=.'
}
}
stage('编译代码'){
steps {
echo "build code ok"
//sh 'mvn package'
}
}
stage('部署代码'){
steps {
sh 'sh -x /script/monitor_deploy_tag.sh'
}
}
}
}
5.2.将代码粘贴到项目中
5.3.利用blue ocean去构建
普通构建也可以的,建议使用blue ocean去构建
打开blue ocean---点击运行---填写版本即可
运行中,每一个阶段都看得清清楚楚,还会有日志输出
部署成功
5.4.项目构建后显示sonarqube图标
我们在构建过程中使用到了sonarqube,但是我们也希望能看到sonarqube的图标并且点击图标即可跳转至sonarqube
在sonarqube质量检测目录前加上withsonarqubeenv一行即可,括号中的值要与系统设置sonarqube服务端的名称
withSonarQubeEnv('SonarQube') {
sh '/usr/local/sonar-scanner-4.0.0.1744-linux/bin/sonar-scanner -Dsonar.projectKey=${JOB_NAME} -Dsonar.sources=.'
}
括号中的值也就是下图中的值,sonarqube服务端的名称
完整脚本
pipeline{
agent any
parameters {
string(name: 'git_version', defaultValue: 'v1.0', description: '输入tag版本')
}
stages { //一个大的任务合集 (部署代码)
stage('获取代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '${git_version}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '9847113e-e313-4dc6-9302-dc4dec14804b', url: 'git@gitlab.jiangxl.com:root/monitor.git']]])
}
}
stage('质量扫描'){
steps {
withSonarQubeEnv('sonarqube') { #主要加了这里
sh '/usr/local/sonar-scanner-4.0.0.1744-linux/bin/sonar-scanner -Dsonar.projectKey=${JOB_NAME} -Dsonar.sources=.'
}
}
}
stage('编译代码'){
steps {
echo "build code ok"
//sh 'mvn package'
}
}
stage('部署代码'){
steps {
sh 'sh -x /script/monitor_deploy_tag.sh'
}
}
}
}
构建成功后查看是有sonarqube图标
5.5.支持钉钉短信报警
首先获取到钉钉token值
https://oapi.dingtalk.com/robot/send?access_token=01a9beb62a838673d7c461a4ecc5e70c11b45c2ed46d67f6cfc08dcceef891e3
本次钉钉报警只增加这些即可,将token值换掉
post { //stages所有任务执行后触发post
failure { //构建失败通知
dingTalk accessToken: '01a9beb62a838673d7c461a4ecc5e70c11b45c2ed46d67f6cfc08dcceef891e3', imageUrl: '', jenkinsUrl: 'http://192.168.81.220:8080', message: '部署失败', notifyPeople: ''
}
success { //构建成功通知
dingTalk accessToken: '01a9beb62a838673d7c461a4ecc5e70c11b45c2ed46d67f6cfc08dcceef891e3', imageUrl: '', jenkinsUrl: 'http://192.168.81.220:8080', message: '部署成功', notifyPeople: ''
}
}
完整脚本
pipeline{
agent any
parameters {
string(name: 'git_version', defaultValue: 'v1.0', description: '输入tag版本')
}
stages { //一个大的任务合集 (部署代码)
stage('获取代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '${git_version}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '9847113e-e313-4dc6-9302-dc4dec14804b', url: 'git@gitlab.jiangxl.com:root/monitor.git']]])
}
}
stage('质量扫描'){
steps {
withSonarQubeEnv('sonarqube') {
sh '/usr/local/sonar-scanner-4.0.0.1744-linux/bin/sonar-scanner -Dsonar.projectKey=${JOB_NAME} -Dsonar.sources=.'
}
}
}
stage('编译代码'){
steps {
echo "build code ok"
//sh 'mvn package'
}
}
stage('部署代码'){
steps {
sh 'sh -x /script/monitor_deploy_tag.sh'
}
}
}
post { //stages所有任务执行后触发post
failure { //构建失败通知
dingTalk accessToken: '01a9beb62a838673d7c461a4ecc5e70c11b45c2ed46d67f6cfc08dcceef891e3', imageUrl: '', jenkinsUrl: 'http://192.168.81.220:8080', message: '部署失败', notifyPeople: ''
}
success { //构建成功通知
dingTalk accessToken: '01a9beb62a838673d7c461a4ecc5e70c11b45c2ed46d67f6cfc08dcceef891e3', imageUrl: '', jenkinsUrl: 'http://192.168.81.220:8080', message: '部署成功', notifyPeople: ''
}
}
}
将pipeline代码写入到项目中
钉钉报警