首页
About Me
推荐
weibo
github
Search
1
linuxea:gitlab-ci之docker镜像质量品质报告
49,451 阅读
2
linuxea:如何复现查看docker run参数命令
23,044 阅读
3
Graylog收集文件日志实例
18,580 阅读
4
linuxea:jenkins+pipeline+gitlab+ansible快速安装配置(1)
18,275 阅读
5
git+jenkins发布和回滚示例
18,181 阅读
ops
Openvpn
Sys Basics
rsync
Mail
NFS
Other
Network
HeartBeat
server 08
Code
Awk
Shell
Python
Golang
virtualization
KVM
Docker
openstack
Xen
kubernetes
kubernetes-cni
Service Mesh
Data
Mariadb
PostgreSQL
MongoDB
Redis
MQ
Ceph
TimescaleDB
kafka
surveillance system
zabbix
ELK Stack/logs
Open-Falcon
Prometheus
victoriaMetrics
Web
apache
Tomcat
Nginx
自动化
Puppet
Ansible
saltstack
Proxy
HAproxy
Lvs
varnish
更多
互联咨询
最后的净土
软件交付
持续集成
gitops
devops
登录
Search
标签搜索
kubernetes
docker
zabbix
Golang
mariadb
持续集成工具
白话容器
elk
linux基础
nginx
dockerfile
Gitlab-ci/cd
最后的净土
基础命令
gitops
jenkins
docker-compose
Istio
haproxy
saltstack
marksugar
累计撰写
690
篇文章
累计收到
139
条评论
首页
栏目
ops
Openvpn
Sys Basics
rsync
Mail
NFS
Other
Network
HeartBeat
server 08
Code
Awk
Shell
Python
Golang
virtualization
KVM
Docker
openstack
Xen
kubernetes
kubernetes-cni
Service Mesh
Data
Mariadb
PostgreSQL
MongoDB
Redis
MQ
Ceph
TimescaleDB
kafka
surveillance system
zabbix
ELK Stack/logs
Open-Falcon
Prometheus
victoriaMetrics
Web
apache
Tomcat
Nginx
自动化
Puppet
Ansible
saltstack
Proxy
HAproxy
Lvs
varnish
更多
互联咨询
最后的净土
软件交付
持续集成
gitops
devops
页面
About Me
推荐
weibo
github
搜索到
7
篇与
的结果
2021-12-20
linuxea:jenkins清理workspace目录插件
清理workspace插件安装workspace cleanupmanage jenkins -> manage plugins -> 在可选插件框内输入workspace cleanup,并选中 --> install without restart而后在流水线语法里面,选择cleanws--> 高级,选择所有,而后生成流水线脚本生成cleanWs()而后将此语句放在post中post { always{ script { ..... cleanWs() } } failure{ script{ ..... cleanWs() } } }而后在java-maven-20211103_cd项目中运行结果中显示[Pipeline] cleanWs [WS-CLEANUP] Deleting project workspace... [WS-CLEANUP] Deferred wipeout is used... [WS-CLEANUP] done [Pipeline] }对应的目录也会清空[root@linuxea-172_16_100_48 ~]# ls /data/jenkins-latest/jenkins_home/workspace/11月/java-maven-20211103-cd [root@linuxea-172_16_100_48 ~]#
2021年12月20日
2,434 阅读
0 评论
0 点赞
2018-10-19
linuxea:jenkins pipeline阶段终止
在管道中,声明的阶段里面是连续的,其中,可以配置的方式很多。且不管每个阶段运行的状态如何,都可以为配置成想要的一些操作,或者行为。如:发邮件,多条件判断等。如果按照正常的运行,不做阶段处理,默认会依次执行多个阶段。这并不是我们想要的,因此,在每个阶段如果执行失败就终止,而不执行后面的是有必要的。当然,这里是有区分的,脚本管道可以使用try,而post作用范围更大使用try 和currentBuild.result = 'FAILURE'即可假如此阶段不通就停止了。相反的,假如此刻需求是,无论那个阶段执行失败都不应该终止,就默认即可。但是,需求如果变成,当执行失败后的还需要动作,参考external-workspace-manager
2018年10月19日
6,886 阅读
0 评论
0 点赞
2017-10-08
linuxea:jenkins pipeline邮件提醒的两种方式(5)
可以通过Mailer Plugin和Email-ext plugin插件发送邮件在pipeline中可以在执行完成进行,通过直接的结果发送失败或者成功,也可以在执行阶段过程中,如果在那个阶段执行失败发送,想看第一中,只发送失败的详细结果:异常处理参考:https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/ https://jenkins.io/doc/book/pipeline/syntax/Mailer Plugin邮件设置安装Mailer Plugin插件1,jenkins邮箱配置系统管理-->系统设置使用的gmail邮箱,添加置如下注意 这里需要开启允许不够安全的应用如下:2,gmail配置3,post字段加入如下字段post { always { step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "myname@gmail.com", sendToIndividuals: true]) } }4,pipeline文件测试pipeline { agent any environment { def ITEMNAME = "webapp" def DESTPATH = "/data/wwwroot" def SRCPATH = "~/workspace/test" def BUILD_USER = "mark" } stages { stage('代码拉取'){ steps { echo "checkout from ${ITEMNAME}" git url: 'git@git.ds.com:mark/maxtest.git', branch: 'master' //git credentialsId:CRED_ID, url:params.repoUrl, branch:params.repoBranch } } stage('服务检查') { steps { echo "检查nginx进程是否存在" script{ def resultUpdateshell = sh script: 'ansible webapp -m shell -a "ps aux|grep nginx|grep -v grep"' currentBuild.result = 'FAILED' if (resultUpdateshell == 0) { skip = '0' return } } } } } post { always { step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "myname@gmail.com", sendToIndividuals: true]) } } }这个简单的pipeline中我将nginx关闭,执行就会发送邮件这样的 邮件需要观察是否哪里出现问题阶段邮件1,方式一也可以在每个阶段进行添加,像这样 stage('服务检查') { steps { echo "检查nginx进程是否存在" script{ try { sh script: 'ansible webapp -m shell -a "ps aux|grep nginx|grep -v grep"' currentBuild.result = 'SUCCESS' } catch (any) { currentBuild.result = 'FAILURE' throw any } finally { println currentBuild.result step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "myname@gmail.com", sendToIndividuals: true]) } } } }2,方式2或者这样 stage('服务检查') { steps { echo "检查nginx进程是否存在" script{ catchError { sh script: 'ansible webapp -m shell -a "ps aux|grep nginx|grep -v grep"' } step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "myname@gmail.com", sendToIndividuals: true]) } } }3,预览完整的预览就是这样pipeline { agent any environment { def ITEMNAME = "webapp" def DESTPATH = "/data/wwwroot" def SRCPATH = "~/workspace/test" def BUILD_USER = "mark" } stages { stage('代码拉取'){ steps { echo "checkout from ${ITEMNAME}" git url: 'git@git.ds.com:mark/maxtest.git', branch: 'master' //git credentialsId:CRED_ID, url:params.repoUrl, branch:params.repoBranch } } stage('服务检查') { steps { echo "检查nginx进程是否存在" script{ catchError { sh script: 'ansible webapp -m shell -a "ps aux|grep nginx|grep -v grep"' } step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "myname@gmail.com", sendToIndividuals: true]) } } } } }但是这样还是会有问题,如果构建正常,不会发送邮件,只有构建出错才会发送,我们希望每个阶段如果构建错误这发送错误的邮件,如果没有错误则发送构建完成这样的字样,那可以这样Email-ext plugin插件1,插件配置安装完成后打开配置页面,系统管理-->系统设置启用调试模式。Enable Debug Mode,默认的触发器也可以勾选参考:https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin#Email-extplugin-TemplateExamples2,配置参考post返回success/failure也会发送邮件,这样你会收到两份邮件,正常和失败的,如下:post { success { emailext ( subject: "'${env.JOB_NAME} [${env.BUILD_NUMBER}]' 更新正常", body: """ 详情: SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' 状态:${env.JOB_NAME} jenkins 更新运行正常 URL :${env.BUILD_URL} 项目名称 :${env.JOB_NAME} 项目更新进度:${env.BUILD_NUMBER} """, to: "myname@gmail.com", recipientProviders: [[$class: 'DevelopersRecipientProvider']] ) } failure { emailext ( subject: "'${env.JOB_NAME} [${env.BUILD_NUMBER}]' 更新失败", body: """ 详情: FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' 状态:${env.JOB_NAME} jenkins 运行失败 URL :${env.BUILD_URL} 项目名称 :${env.JOB_NAME} 项目更新进度:${env.BUILD_NUMBER} """, to: "myname@gmail.com", recipientProviders: [[$class: 'DevelopersRecipientProvider']] ) } } }3,混合模式(Mailer Email-ext)Mailer 会将pipeline执行详细内容发送到邮件,成功这只发送编辑的信息pipeline { agent any environment { def ITEMNAME = "webapp" def DESTPATH = "/data/wwwroot" def SRCPATH = "~/workspace/test" def BUILD_USER = "mark" } stages { stage('代码拉取'){ steps { echo "checkout from ${ITEMNAME}" git url: 'git@git.ds.com:mark/maxtest.git', branch: 'master' //git credentialsId:CRED_ID, url:params.repoUrl, branch:params.repoBranch } } stage('服务检查') { steps { echo "检查nginx进程是否存在" script{ try { sh script: 'ansible webapp -m shell -a "ps aux|grep nginx|grep -v grep"' } catch (exc) { currentBuild.result = "FAILURE" println currentBuild.result step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "myname@gmail.com", sendToIndividuals: true]) } } } } } post { success { emailext ( subject: "'${env.JOB_NAME} [${env.BUILD_NUMBER}]' 更新正常", body: """ 详情: SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' 状态:${env.JOB_NAME} jenkins 更新运行正常 URL :${env.BUILD_URL} 项目名称 :${env.JOB_NAME} 项目更新进度:${env.BUILD_NUMBER} """, to: "myname@gmail.com", recipientProviders: [[$class: 'DevelopersRecipientProvider']] ) } } }4,阶段邮件发送在前面Mailer 已经可以实现了,但是如果pipeline过长观看不是很理想,可以这样如果希望每个步骤如果失败则发送每一个失败的邮件提醒,如果正常也发送一封邮件提醒。不同的是内容可以自定义pipeline { agent any environment { def ITEMNAME = "webapp" def DESTPATH = "/data/wwwroot" def SRCPATH = "~/workspace/test" def BUILD_USER = "mark" def USERMAIL = "myname@gmail.com" } stages { stage('代码拉取'){ steps { echo "checkout from ${ITEMNAME}" git url: 'git@git.ds.com:mark/maxtest.git', branch: 'master' //git credentialsId:CRED_ID, url:params.repoUrl, branch:params.repoBranch } } stage('服务检查') { steps { echo "检查nginx进程是否存在" script{ try { sh script: 'ansible webapp -m shell -a "ps aux|grep nginx|grep -v grep"' } catch (exc) { currentBuild.result = "FAILURE" emailext ( subject: "'${env.JOB_NAME} [${env.BUILD_NUMBER}]' 更新失败", body: """ 详情: FAILURE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' 状态:${env.JOB_NAME} jenkins 更新失败 URL :${env.BUILD_URL} 项目名称 :${env.JOB_NAME} 项目更新进度:${env.BUILD_NUMBER} 内容:nginx进程不存在 """, to: "${USERMAIL}", recipientProviders: [[$class: 'DevelopersRecipientProvider']] ) } } } } stage('目录检查') { steps { echo "检查${DESTPATH}目录是否存在" script{ try { sh script: 'ansible webapp -m shell -a "ls -d ${DESTPATH}"' } catch (exc) { currentBuild.result = "FAILURE" emailext ( subject: "'${env.JOB_NAME} [${env.BUILD_NUMBER}]' 更新失败", body: """ 详情: FAILURE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' 状态:${env.JOB_NAME} jenkins 更新失败 URL :${env.BUILD_URL} 项目名称 :${env.JOB_NAME} 项目更新进度:${env.BUILD_NUMBER} 内容:${DESTPATH}目录不存在 """, to: "${USERMAIL}", recipientProviders: [[$class: 'DevelopersRecipientProvider']] ) } } } } } post { success { emailext ( subject: "'${env.JOB_NAME} [${env.BUILD_NUMBER}]' 更新正常", body: """ 详情: SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' 状态:${env.JOB_NAME} jenkins 更新运行正常 URL :${env.BUILD_URL} 项目名称 :${env.JOB_NAME} 项目更新进度:${env.BUILD_NUMBER} """, to: "${USERMAIL}", recipientProviders: [[$class: 'DevelopersRecipientProvider']] ) } } }
2017年10月08日
14,260 阅读
0 评论
0 点赞
2017-09-28
linuxea:jenkins集成SonarQube Scanner(4)
jenkins集成SonarQube Scanner,代码在发布之前通过SonarQube,通过后进入下一个环节,他的样子大致是这样的:和之前相比,在pipeline中如果SonarQube检查通过才会进行下一个环节,否则就提前终止参考:jenkins+pipeline+gitlab+ansible快速安装配置(1)https://www.linuxea.com/1733.html插件配置1,jenkins插件打开jenkins登陆,系统管理-->管理插件--->搜索SonarQube Scanner for Jenkins-->安装[SonarQube Scanner for Jenkins]插件2,生成token其实在安装好sonarqube的时候第一次使用就提示过,不过在之前的文章中并未提到,现在重新生成即可sonarqube安装参考:sonarqube-6.5_2代码质量管理系统配置和汉化https://www.linuxea.com/1722.html我的账号-->安全-->输入,如下:复制3,配置jenkins集成插件SonarQube在jenkins的插件管理中选择安装"SonarQube Scanner for Jenkins",该插件可以使项目每次构建都调用sonar进行代码度量。进入配置页面对sonar插件进行配置,如下图,sonar6只需要配置token即可。系统管理-->系统设置点击add sonarqube将生成的token粘贴进去4,sonar-scanner配置下载安装官网配置参考:https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner[root@linuxea.com-Node113 /usr/local]# wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.0.3.778-linux.zip [root@linuxea.com-Node113 /usr/local]# unzip sonar-scanner-cli-3.0.3.778-linux.zip [root@linuxea.com-Node113 /usr/local]# ln -s sonar-scanner-3.0.3.778-linux sonar-scanner编辑配置文件官网配置参考:https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner官网配置参考:https://docs.sonarqube.org/display/SCAN/Advanced+SonarQube+Scanner+Usages[jenkins@linuxea.com-Node113 /usr/local/sonar-scanner/conf]$ cat sonar-scanner.test #Configure here general information about the environment, such as SonarQube DB details for example #No information about specific project should appear here #----- Default SonarQube server sonar.host.url=http://10.10.240.114:888/sonar sonar.projectKey=linuxea-test sonar.projectName=linuxea-test # Key and Name transfer SonarQube server name #这里是php文件放的地方 sonar.sources=/var/lib/jenkins/workspace/test # Language sonar.languages=php,js,css,less sonar.dynamicAnalysis=false # Encoding of the source files sonar.sourceEncoding=UTF-8 [jenkins@linuxea.com-Node113 /usr/local/sonar-scanner/conf]$ 确保SonarQube server防火墙放行了888端口,也就是SonarQube的端口5, 测试测试:由于项目比较多,使用-Dproject.settings指定配置文件/usr/local/sonar-scanner/bin/sonar-scanner -Dproject.settings=/usr/local/sonar-scanner/conf/sonar-scanner.test[jenkins@linuxea.com-Node113 ~]$ /usr/local/sonar-scanner/bin/sonar-scanner -Dproject.settings=/usr/local/sonar-scanner/conf/sonar-scanner.test INFO: Scanner configuration file: /usr/local/sonar-scanner-3.0.3.778-linux/conf/sonar-scanner.properties INFO: Project root configuration file: /usr/local/sonar-scanner/conf/sonar-scanner.test INFO: SonarQube Scanner 3.0.3.778 INFO: Java 1.8.0_121 Oracle Corporation (64-bit) INFO: Linux 4.8.5-1.el7.centos.x86_64 amd64 INFO: User cache: /var/lib/jenkins/.sonar/cache INFO: Load global settings INFO: Load global settings (done) | time=112ms INFO: User cache: /var/lib/jenkins/.sonar/cache INFO: Load plugins index INFO: Load plugins index (done) | time=4ms INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2. INFO: SonarQube server 6.5.0 INFO: Default locale: "zh_CN", source code encoding: "UTF-8" INFO: Process project properties INFO: Load project repositories INFO: Load project repositories (done) | time=49ms INFO: Load quality profiles INFO: Load quality profiles (done) | time=19ms INFO: Load active rules INFO: Load active rules (done) | time=834ms INFO: Load metrics repository INFO: Load metrics repository (done) | time=90ms INFO: Publish mode INFO: Project key: linuxea-test INFO: ------------- Scan linuxea-test INFO: Load server rules INFO: Load server rules (done) | time=342ms INFO: Base dir: /var/lib/jenkins INFO: Working dir: /var/lib/jenkins/.scannerwork INFO: Source paths: workspace/test INFO: Source encoding: UTF-8, default locale: zh_CN INFO: Index files INFO: 11 files indexed INFO: Quality profile for js: Sonar way INFO: Quality profile for php: Sonar way INFO: Sensor SonarJavaXmlFileSensor [java] INFO: Sensor SonarJavaXmlFileSensor [java] (done) | time=1ms INFO: Sensor PHP sensor [php] INFO: 2 source files to be analyzed INFO: No PHPUnit test report provided (see 'sonar.php.tests.reportPath' property) INFO: 2/2 source files have been analyzed INFO: No PHPUnit coverage reports provided (see 'sonar.php.coverage.reportPaths' property) INFO: Sensor PHP sensor [php] (done) | time=569ms INFO: Sensor Analyzer for "php.ini" files [php] INFO: Sensor Analyzer for "php.ini" files [php] (done) | time=2ms INFO: Sensor JavaScript Squid Sensor [javascript] INFO: 1 source files to be analyzed INFO: Unit Test Coverage Sensor is started INFO: Integration Test Coverage Sensor is started INFO: Overall Coverage Sensor is started INFO: Sensor JavaScript Squid Sensor [javascript] (done) | time=1200ms INFO: Sensor Zero Coverage Sensor INFO: 1/1 source files have been analyzed INFO: Sensor Zero Coverage Sensor (done) | time=39ms INFO: Sensor CPD Block Indexer INFO: Sensor CPD Block Indexer (done) | time=2ms INFO: SCM Publisher is disabled INFO: Calculating CPD for 3 files INFO: CPD calculation finished INFO: Analysis report generated in 98ms, dir size=137 KB INFO: Analysis reports compressed in 19ms, zip size=47 KB INFO: Analysis report uploaded in 57ms INFO: ANALYSIS SUCCESSFUL, you can browse http://10.10.240.114:888/sonar/dashboard/index/linuxea-test INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report INFO: More about the report processing at http://10.10.240.114:888/sonar/api/ce/task?id=AV6yWqf79bkYV2Lnt8O3 INFO: Task total time: 5.104 s INFO: ------------------------------------------------------------------------ INFO: EXECUTION SUCCESS INFO: ------------------------------------------------------------------------ INFO: Total time: 6.127s INFO: Final Memory: 46M/259M INFO: ------------------------------------------------------------------------ [jenkins@linuxea.com-Node113 ~]$ 返回SonarQube Server查看6,集成pipeline代码项目linuxea-3-sonarqube1,sonar-scanner配置文件[root@DS-VM-Node113 /usr/local/sonar-scanner/conf]# cat sonar-scanner.linuxea-3 #Configure here general information about the environment, such as SonarQube DB details for example #No information about specific project should appear here #----- Default SonarQube server sonar.host.url=http://10.10.240.114:888/sonar sonar.projectKey=linuxea-3-sonarqube sonar.projectName=linuxea-3-sonarqube #这里是php文件放的地方 sonar.sources=/var/lib/jenkins/workspace/linuxea-3-sonarqube # Language sonar.languages=php,js,css,less sonar.dynamicAnalysis=false # Encoding of the source files sonar.sourceEncoding=UTF-82,pipeline代码这句很重要,指明文件位置sh "/usr/local/sonar-scanner/bin/sonar-scanner -Dproject.settings=/usr/local/sonar-scanner/conf/sonar-scanner.linuxea-3"代码如下:官网配置参考:https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins官网配置参考:https://jenkins.io/doc/pipeline/steps/sonar/#code-waitforqualitygate-code-wait-for-sonarqube-analysis-to-be-completed-and-return-quality-gate-status stage('SonarQube') { steps { echo "正在开始代码检测........." withSonarQubeEnv('SonarQube') { sh "/usr/local/sonar-scanner/bin/sonar-scanner -Dproject.settings=/usr/local/sonar-scanner/conf/sonar-scanner.linuxea-3" } // SonarQube taskId is automatically attached to the pipeline context script { timeout(time: 1, unit: 'HOURS') { // Just in case something goes wrong, pipeline will be killed after a timeout def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv if (qg.status != 'OK') { error "Pipeline aborted due to quality gate failure: ${qg.status}" } } } } }完整的这样pipeline { agent any parameters { string(defaultValue: '', name: 'GIT_TAG', description: '请根据发布类型进行选择发布:\n1,输入-TESTING-发布-最新代码-到灰度\n2,输入-LATEST-发布-最新代码-到生产\n3,输入-版本号-发布-制定版本-到生产 ' ) } environment { def ITEMNAME = "webapp" def ITEMNAME2 = "twebapp" def DESTPATH = "/data/wwwroot" def SRCPATH = "~/workspace/linuxea-3-sonarqube" } stages { stage('代码拉取'){ steps { echo "checkout from ${ITEMNAME}" git url: 'git@git.ds.com:mark/maxtest.git', branch: 'master' //git credentialsId:CRED_ID, url:params.repoUrl, branch:params.repoBranch } } stage('SonarQube') { steps { echo "starting codeAnalyze with SonarQube......" //sonar:sonar.QualityGate should pass withSonarQubeEnv('SonarQube') { //固定使用项目根目录${basedir}下的pom.xml进行代码检查 sh "/usr/local/sonar-scanner/bin/sonar-scanner -Dproject.settings=/usr/local/sonar-scanner/conf/sonar-scanner.linuxea-3" } script { timeout(10) { //利用sonar webhook功能通知pipeline代码检测结果,未通过质量阈,pipeline将会fail def qg = waitForQualityGate() if (qg.status != 'OK') { error "未通过Sonarqube的代码质量阈检查,请及时修改!failure: ${qg.status}" } } } } } stage('目录检查') { steps { echo "检查${DESTPATH}目录是否存在" script{ def resultUpdateshell = sh script: 'ansible webapp -m shell -a "ls -d ${DESTPATH}"' //def resultUpdateshell = sh script: 'ansible twebapp -m shell -a "ls -d ${DESTPATH}"' if (resultUpdateshell == 0) { skip = '0' return } } } } stage('服务检查') { steps { echo "检查nginx进程是否存在" script{ def resultUpdateshell = sh script: 'ansible webapp -m shell -a "ps aux|grep nginx|grep -v grep"' //def resultUpdateshell = sh script: 'ansible twebapp -m shell -a "ps aux|grep nginx|grep -v grep"' if (resultUpdateshell == 0) { skip = '0' return } } } } stage('发布确认') { steps { input "检查完成,是否发布?" } } stage('代码推送') { steps { echo "code sync" script { if (env.GIT_TAG == 'TESTING') { echo 'TESTING' sh "ansible ${ITEMNAME2} -m synchronize -a 'src=${SRCPATH}/ dest=${DESTPATH}/ rsync_opts=-avz,--exclude=.git,--delete'" } else { if (env.GIT_TAG == 'LATEST') { echo 'LATEST' sh "ansible ${ITEMNAME} -m synchronize -a 'src=${SRCPATH}/ dest=${DESTPATH}/ rsync_opts=-avz,--exclude=.git,--delete'" } else { sh """ git checkout ${GIT_TAG} ansible ${ITEMNAME} -m synchronize -a 'src=${SRCPATH}/ dest=${DESTPATH}/ rsync_opts=-avz,--exclude=.git,--delete' """ } } } } } } }在开始之前需要配置sonar-scanner的webhooks参考 : https://docs.sonarqube.org/display/SONAR/Webhooksjenkins状态如下:sonar-scanner如下:
2017年09月28日
6,583 阅读
0 评论
0 点赞
2017-09-25
linuxea:jenkins pipeline参数化构建发布和回滚(3)
jenkins pipeline 发布和回滚jenkins pipeline参数化构建在前面有章简单的介绍了pipeline用法,jenkins pipeline简单使用示例(2)jenkins+pipeline+gitlab+ansible快速安装配置(1)但是不够灵活,在经过资料查阅后,发现可以将条件构建步骤转换成jenkins pipeline,如下:官方参考 : https://jenkins.io/doc/book/pipeline/syntax/#parametersparameters { string(defaultValue: '', name: 'GIT_TAG', description: '请根据发布类型进行选择发布:\n1,输入-TESTING-发布-最新代码-到灰度\n2,输入-LATEST-发布-最新代码-到生产\n3,输入-版本号-发布-制定版本-到生产 ' ) }发布和回滚发布:代码仓库是gitlab,我们知道gitlab发布要么git pull 最新的代码(不需要版本号),或者checkout 指定的版本号代码。我们把代码拉到本地jenkins,是用来发布,且不管拉取那个已经存在的版本回滚:checkout,通常提供版本号即发布和回滚,因为通过版本号直接可以checkout git仓库已有的代码,了解这个后发布和回滚则变的简单起来也可以git reset --hard HEAD^,^^来进行回退,在这里并没有介绍,通过简单的修改即可实现,如:git pull最新代码,git reset --hard HEAD^来进行回退pipeline if判断参数化构建和发布方式确定后就可以编写pipeline文件来进行测试其中内容和上一篇博客https://www.linuxea.com/1738.html中的做修改后使用的,在参数化构建时,提示如下:请根据发布类型进行选择发布:1,输入-TESTING-发布-最新代码-到灰度2,输入-LATEST-发布-最新代码-到生产3,输入-版本号-发布-制定版本-到生产 当GIT_TAG == 'TESTING'执行TESTING的脚本,等于LATEST则执行LATEST脚本,如果都不等于则git checkout,pipeline部分如下: stage('代码推送') { steps { echo "code sync" script { if (env.GIT_TAG == 'TESTING') { echo 'TESTING' sh "ansible ${ITEMNAME2} -m synchronize -a 'src=${SRCPATH}/ dest=${DESTPATH}/ rsync_opts=-avz,--exclude=.git,--delete'" } else { if (env.GIT_TAG == 'LATEST') { echo 'LATEST' sh "ansible ${ITEMNAME} -m synchronize -a 'src=${SRCPATH}/ dest=${DESTPATH}/ rsync_opts=-avz,--exclude=.git,--delete'" } else { sh """ git checkout ${GIT_TAG} } }完整的如下所示pipeline { agent any parameters { string(defaultValue: '', name: 'GIT_TAG', description: '请根据发布类型进行选择发布:\n1,输入-TESTING-发布-最新代码-到灰度\n2,输入-LATEST-发布-最新代码-到生产\n3,输入-版本号-发布-制定版本-到生产 ' ) } environment { def ITEMNAME = "webapp" def ITEMNAME2 = "twebapp" def DESTPATH = "/data/wwwroot" def SRCPATH = "~/workspace/linuxea-2" } stages { stage('代码拉取'){ steps { echo "checkout from ${ITEMNAME}" git url: 'git@git.ds.com:mark/maxtest.git', branch: 'master' //git credentialsId:CRED_ID, url:params.repoUrl, branch:params.repoBranch } } stage('目录检查') { steps { echo "检查${DESTPATH}目录是否存在" script{ def resultUpdateshell = sh script: 'ansible webapp -m shell -a "ls -d ${DESTPATH}"' //def resultUpdateshell = sh script: 'ansible twebapp -m shell -a "ls -d ${DESTPATH}"' if (resultUpdateshell == 0) { skip = '0' return } } } } stage('服务检查') { steps { echo "检查nginx进程是否存在" script{ def resultUpdateshell = sh script: 'ansible webapp -m shell -a "ps aux|grep nginx|grep -v grep"' //def resultUpdateshell = sh script: 'ansible twebapp -m shell -a "ps aux|grep nginx|grep -v grep"' if (resultUpdateshell == 0) { skip = '0' return } } } } stage('发布确认') { steps { input "检查完成,是否发布?" } } stage('代码推送') { steps { echo "code sync" script { if (env.GIT_TAG == 'TESTING') { echo 'TESTING' sh "ansible ${ITEMNAME2} -m synchronize -a 'src=${SRCPATH}/ dest=${DESTPATH}/ rsync_opts=-avz,--exclude=.git,--delete'" } else { if (env.GIT_TAG == 'LATEST') { echo 'LATEST' sh "ansible ${ITEMNAME} -m synchronize -a 'src=${SRCPATH}/ dest=${DESTPATH}/ rsync_opts=-avz,--exclude=.git,--delete'" } else { sh """ git checkout ${GIT_TAG} ansible ${ITEMNAME} -m synchronize -a 'src=${SRCPATH}/ dest=${DESTPATH}/ rsync_opts=-avz,--exclude=.git,--delete' """ } } } } } } }创建项目创建item直接把脚本复制进去即可构建一次后,点击build with parameters
2017年09月25日
14,297 阅读
3 评论
0 点赞
2017-09-24
linuxea:jenkins pipeline简单使用示例(2)
jenkins pipeline简单使用示例pipeline简述Jenkins pipeline 是一套插件,支持将连续输送管道实施和整合到Jenkins。Pipeline提供了一组可扩展的工具,用于通过管道DSL为代码创建简单到复杂的传送流水线。他目前支持jenkins 2.x以上版本。pipeline是由groovy语言编写pipeline使用pipeline分为两种一种为jenkinsfile使用,一种直接在网页界面中输入脚本内容,本章节则是第二种文档参考官方参考:https://jenkins.io/doc/book/pipeline/语法参考:https://jenkins.io/doc/book/pipeline/syntax/示例演示简单的实现了php的代码发布:1,定义了常用变量在全局的变量中2,第一阶段代码拉取,到jenkins的目录中3,第二阶段这里做了即将推送代码的目录的检查,是否存在,主要测试if语句4,第三阶段检查进程是否存在(这没必要)5,第四阶段推送代码前确认,是否发布,否则将暂停6,第五阶段利用ansible推送代码这里面有很多需要提前准备,比如权限问题,ansible配置等jenkins+pipeline+gitlab+ansible快速安装配置(1)参考:点击直达1,pipeline文件pipeline { agent any environment { def ITEMNAME = "webapp" def DESTPATH = "/data/wwwroot" def SRCPATH = "~/workspace/linuxea" } stages { stage('代码拉取'){ steps { echo "checkout from ${ITEMNAME}" git url: 'git@git.ds.com:mark/maxtest.git', branch: 'master' //git credentialsId:CRED_ID, url:params.repoUrl, branch:params.repoBranch } } stage('目录检查') { steps { echo "检查${DESTPATH}目录是否存在" script{ def resultUpdateshell = sh script: 'ansible webapp -m shell -a "ls -d ${DESTPATH}"' if (resultUpdateshell == 0) { skip = '0' return } } } } stage('服务检查') { steps { echo "检查nginx进程是否存在" script{ def resultUpdateshell = sh script: 'ansible webapp -m shell -a "ps aux|grep nginx|grep -v grep"' if (resultUpdateshell == 0) { skip = '0' return } } } } stage('发布确认') { steps { input "检查完成,是否发布?" } } stage('代码推送') { steps { echo "code sync" sh "ansible ${ITEMNAME} -m synchronize -a 'src=${SRCPATH}/ dest=${DESTPATH}/ rsync_opts=-avz,--exclude=.git,--delete'" } } } }2,创建item粘贴脚本到此处保存退出立即构建会提示是否发布,选择Proceed 即可继续不管更新过程中还是更新出错或者更新完成都可以在logs中查看进度和结果,如下图后台console output文件执行结果如下Started by user linuxea [Pipeline] node Running on master in /var/lib/jenkins/workspace/linuxea [Pipeline] { [Pipeline] withEnv [Pipeline] { [Pipeline] stage [Pipeline] { (代码拉取) [Pipeline] echo checkout from webapp [Pipeline] git > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url git@git.ds.com:mark/maxtest.git # timeout=10 Fetching upstream changes from git@git.ds.com:mark/maxtest.git > git --version # timeout=10 > git fetch --tags --progress git@git.ds.com:mark/maxtest.git +refs/heads/*:refs/remotes/origin/* > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision b6c6dc4140aa3a30de442c6d45fe327831cb9ee9 (refs/remotes/origin/master) Commit message: "增加新文件" > git config core.sparsecheckout # timeout=10 > git checkout -f b6c6dc4140aa3a30de442c6d45fe327831cb9ee9 > git branch -a -v --no-abbrev # timeout=10 > git branch -D master # timeout=10 > git checkout -b master b6c6dc4140aa3a30de442c6d45fe327831cb9ee9 > git rev-list b6c6dc4140aa3a30de442c6d45fe327831cb9ee9 # timeout=10 [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (目录检查) [Pipeline] echo 检查/data/wwwroot目录是否存在 [Pipeline] script [Pipeline] { [Pipeline] sh [linuxea] Running shell script + ansible webapp -m shell -a 'ls -d /data/wwwroot' 10.10.0.98 | SUCCESS | rc=0 >> /data/wwwroot [Pipeline] } [Pipeline] // script [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (服务检查) [Pipeline] echo 检查nginx进程是否存在 [Pipeline] script [Pipeline] { [Pipeline] sh [linuxea] Running shell script + ansible webapp -m shell -a 'ps aux|grep nginx|grep -v grep' 10.10.0.98 | SUCCESS | rc=0 >> root 21209 0.0 0.1 13452 2752 ? S 9月20 0:00 nginx: master process /usr/local/nginx/sbin/nginx 400 21210 0.0 1.0 34700 21852 ? S 9月20 0:00 nginx: worker process [Pipeline] } [Pipeline] // script [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (发布确认) [Pipeline] input 检查完成,是否发布? Proceed or Abort Approved by linuxea [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (代码推送) [Pipeline] echo code sync [Pipeline] sh [linuxea] Running shell script + ansible webapp -m synchronize -a 'src=~/workspace/linuxea/ dest=/data/wwwroot/ rsync_opts=-avz,--exclude=.git,--delete' 10.10.0.98 | SUCCESS => { "changed": false, "cmd": "/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -S none -o StrictHostKeyChecking=no -o Port=22' -avz --exclude=.git --delete --out-format='<<CHANGED>>%i %n%L' \"/var/lib/jenkins/workspace/linuxea/\" \"root@10.10.0.98:/data/wwwroot/\"", "msg": "building file list ... done\n\nsent 256 bytes received 12 bytes 536.00 bytes/sec\ntotal size is 47858 speedup is 178.57\n", "rc": 0, "stdout_lines": [ "building file list ... done", "sent 256 bytes received 12 bytes 536.00 bytes/sec", "total size is 47858 speedup is 178.57" ] } [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS
2017年09月24日
7,314 阅读
0 评论
0 点赞
2017-09-23
linuxea:jenkins+pipeline+gitlab+ansible快速安装配置(1)
jenkins+gitlab+ansible配合使用考虑了一下子,还是整理了一些图片和文字,简单的记录安装的过程,主要想玩pipeline。那么在后面的几篇文章中会介绍jenkins+sonarqube的构建,当然,还是用来做pipeline来玩,告别之前的那种模式,本章主要介绍jenkins+gitlab+ansible快速部署sonarqube 安装:https://www.linuxea.com/1722.html1,jenkins在这里负责发布构建,主要插件pipeline,gitlab,sonarqube2,gitlab代码管理3,ansible用来推送代码jenkins安装参考:https://pkg.jenkins.io/redhat-stable/我这里安装的是2.73.1安装jenkins和插件安装jenkins[root@linuxea.com-Node113 ~]# sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo [root@linuxea.com-Node113 ~]# sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key [root@linuxea.com-Node113 ~]# yum install jenkins -y [root@linuxea.com-Node113 ~]# /etc/init.d/jenkins start [root@linuxea.com-Node113 ~]# iptables -I INPUT 5 -p tcp -m tcp -m state --state NEW -m multiport --dports 22,3306,10050,10051,8080 -m comment --comment "ALL" -j ACCEPT打开浏览器进行安装和配置cat /var/lib/jenkins/secrets/initialAdminPassword根据提示将密码输入安装gitlab和pipeline插件我这里有vpn,直接安装安装pipeline等待安装完成后进入界面安装ansible和配置jenkins和gitlab[root@linuxea.com-Node113 ~]# yum install jenkins添加主机[root@linuxea.com-Node113 ~]# cat /etc/ansible/hosts [webapp] 10.10.0.98 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass="mima"配置密钥和主机配置jenkins用户终端修改jenkins用户shell为bashjenkins:x:989:985:Jenkins Automation Server:/var/lib/jenkins:/bin/bash配置ansible和源站测试连通性[root@linuxea.com-Node113 /etc/ansible]# su - jenkins尝试链接一次[jenkins@linuxea.com-Node113 ~]$ ansible webapp -m ping 10.10.0.98 | SUCCESS => { "changed": false, "ping": "pong" }之后会用到sync模块,模块本身不支持密码,所以传递密钥即可[jenkins@DS-VM-Node113 ~]$ ssh-keygen -t rsa传递密钥给源站机器[jenkins@linuxea.com-Node113 ~]$ ssh-copy-id root@10.10.0.98 /bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@10.10.0.98's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@10.10.0.98'" and check to make sure that only the key(s) you wanted were added.测试[jenkins@linuxea.com-Node113 ~]$ ssh root@10.10.0.98 Last login: Wed Sep 20 13:41:41 2017 from 10.10.240.113 [root@linuxea.com-Node98 ~]# 配置gitlab密钥同时也需要将jenkins的id_rsa.pub文件添加到gitlab项目中方便拉代码[jenkins@linuxea.com-Node113 ~]$ cat /var/lib/jenkins/.ssh/id_rsa.pub 复制信息到项目中配置jenkins密钥在jenkins中修改credentials编辑到此位置,安装配置完成
2017年09月23日
18,275 阅读
0 评论
1 点赞