Jenkins(七)
2019-09-14 本文已影响0人
测试游记
欢迎关注我公众号呀~「测试游记」「zx94_11」
触发流水线执行可以分为:
- 时间触发
- 事件触发
时间触发
定义一个时间,时间到了就触发pipeline。
在Jenkins pipeline中使用trigger
指令来定义时间触发
定时执行:cron
Jenkins trigger cron语法采用UNIX cron语法。一条cron包含五个字段,使用空格分隔。
格式:MINUTE HOUR DOM MONTH DOW
- MINUTE:分钟 0~59
- HOUR:小时 0~23
- DOM:一个月中的某天 1~31
- MONTH:月份 1~12
- DOW:星期几 0~7,0和7代表星期天
使用特殊字符,指定多个值
-
*
:匹配所有值 -
M-N
:匹配M-N之间的值 -
M-N|X
or*|x
:指定M到N以X为步长的值 -
A,B,C……Z
:多个值
为了解决在同一时刻执行定时任务可能出现的负载不均衡问题。在Jenkins trigger cron语法中使用
H
字符来解决这个问题。H
代表hash
H 0 * * *
代表在0点0分至0点59分任意一个时间点执行。
几个例子:
-
H/5 * * * *
:每隔5分钟构建一次 -
H H/2 * * *
:每两小时构建一次 -
0 12 * * *
:每天12点定时构建一次 -
H(0-29)/10 * * * *
:前半小时的每隔10分钟 -
45 9-16/2 * * 1-5
:周一到周五的9点45到16点45的每隔两个小时构建一次
pipeline {
agent any
triggers {
cron ('0 0 * * *')
}
stages {
stage ('Nightly build') {
steps {
echo '明天凌晨执行'
}
}
}
}
轮询代码仓库:pollSCM
定期到代码仓库询问代码是否有变化,如果代码仓库有变化,就执行
pipeline {
agent any
triggers {
pollSCM ('H/1 * * * *')
}
stages {
stage ('build') {
steps {
echo '轮询执行'
}
}
}
}
事件触发
- 手动在界面上触发
- 其他job触发
- HTTP API Webhook触发
由上游任务出发:upstream
当B任务的执行依赖A任务的执行结果,A就是B的上游任务。
triggers {
upstream(upstreamProjects: 'job1,job2',threshold:hudson.model.Result.SUCCESS)
}
- job1和job2是任务名
- threshold判断上游任务是什么状态的时候触发
- hudson.model.Result包含了
ABORTED
,FAILURE
,SUCCESS
,UNSTSBLE
,NOT_BUILT
其中它们分别表示:
- ABORTED:任务被手动中止
- FAILURE:构建失败
- SUCCESS:构建成功
- UNSTSBLE:存在一些错误,但不至于构建失败
- NOT_BUILT:在多阶段构建时,前面阶段的问题导致后面阶段无法执行
Gitlab通知触发
- 安装Gitlab插件
- 安装git插件(应该已经安装了)

- 在Gitlab上创建一个项目

- 将代码上库
$ git clone http://123.56.13.233:9000/zhongxin/hello-world-pipeline.git
正克隆到 'hello-world-pipeline'...
warning: 您似乎克隆了一个空仓库。
$ cd hello-world-pipeline
$ touch 1.py
$ git add 1.py
$ git commit -m "add 1.py"
[master(根提交) e815882] add 1.py
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 1.py
$ git push -u origin master
remote: You are not allowed to push code to this project.
fatal: unable to access 'http://123.56.13.233:9000/zhongxin/hello-world-pipeline.git/': The requested URL returned error: 403
$ git push -u origin master
分支 'master' 设置为跟踪来自 'origin' 的远程分支 'master'。
Everything up-to-date
PS:如果遇到403问题请将.git/config
中的url修改为:https://用户名:密码@123.56.13.233:9000/zhongxin/hello-world-pipeline.git/


再同之前GitHub一样,添加一个凭证


为了保障安全,需要生成一个Secret toekn
c342352fc4cf45e01c0f783a7fdf38b7
就是一个Secret toekn


额,到这里遇到一个比较尴尬的问题。我的gitlab服务器在公网,Jenkins服务器在局域网内
如果,如果成功了的话就可以在下方Project services
处看到新增的Webhooks
Jenkinsfile中编写Gitlab trigger
pipeline {
agent any
triggers {
gitlab(triggerOnPush: true,
triggerOnMergeRequest: true,
branchFilterType: 'All',
secretToken: 'c342352fc4cf45e01c0f783a7fdf38b7')
}
stages {
stage('build') {
steps {
echo "Hello wolrd"
}
}
}
}
- triggerOnPush:当Gitlab触发push事件,是否执行构建
- triggerOnMergeRequest:当Gitlab触发mergeRequest事件时,是否执行构建
- branchFilterType:只有符合条件的分支才会触发,必选
将构建信息推送到GitLab



pipeline {
agent any
triggers {
gitlab(triggerOnPush: true,
triggerOnMergeRequest: true,
branchFilterType: 'All',
secretToken: 'c342352fc4cf45e01c0f783a7fdf38b7')
}
stages {
stage('build') {
steps {
echo "Hello wolrd"
}
}
}
post {
failure {
updateGitlabCommitStatus name:'build',state:'failed'
}
success {
updateGitlabCommitStatus name:'build',state:'success'
}
}
options {
gitLabConnecton('gitlab')
}
}
其他系统触发Webhook
使用Generic Webhook Trigger
插件,具体内容不展开