在Pipeline中实现GitLab触发
需要安装GitLab Plugin
在Pipeline代码中配置GitLab Trigger会用到如下几个step
◼ triggerOnPush:GitLab仓库推送push事件时是否执行构建
◼ triggerOnMergeRequest:GitLab仓库推送mergeRequest事件时,是否执行构建
◼ branchFilterType:只有符合条件的分支才会被触发;必选配置,否则将无法实现触发,支持如下值
◆ NameBaseFilter:基于分支名进行过滤,多个分支名彼此间以逗号分隔
◆ RegexBaseFilter:基于正则表达式模式对分支名过滤
◆ All:所有分支都会被触发
◼ includeBranchSpec:基于branchFilterType值,输入期望包括的分支的规则
◼ excludeBranchSpec:基于branchFilterType值,输入期望排队的分支的规则
◼ secretToken:在回调时使用的token
更详细的配置参数请参考GitLab-Plugin项目的描述
◼ https://github.com/jenkinsci/gitlab-plugin
测试执行(gitlab分支合并触发)
pipeline {
agent any
parameters {
booleanParam(name:'pushImage', defaultValue: 'true', description: 'Push Image to Harbor?')
}
tools {
maven 'maven-3.8.7'
}
triggers {
gitlab(triggerOnPush: true,
triggerOnMergeRequest: true,
branchFilterType: 'All',
addVoteOnMergeRequest: true,
secretToken: '9iH0tq33ROMS07przzLiclNmfqSaoIeTOolHYf1E')
}
environment {
codeRepo="http://gitlab.mengfanchao.com/root/spring-boot-helloWorld.git"
harborServer='harbor.meng.org'
projectName='spring-boot-helloworld'
imageUrl="${harborServer}/ikubernetes/${projectName}"
imageTag='latest'
}
stages {
stage('Source') {
steps {
git branch: 'main', credentialsId: 'gitlab-root-credential', url: "${codeRepo}"
}
}
stage('Build') {
steps {
sh 'mvn -B -DskipTests clean package'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('Build Docker Image') {
steps {
sh 'docker image build . -t "${harborServer}/ikubernetes/${projectName}:${imageTag}"'
}
}
stage('Push Docker Image') {
steps {
withCredentials([usernamePassword(credentialsId: 'harbor-user-credential',
passwordVariable: 'harborUserPassword', usernameVariable: 'harborUserName')]) {
sh "echo ${harborUserPassword} | docker login -u ${env.harborUserName} --password-stdin ${harborServer}"
sh "docker image push ${imageUrl}:${imageTag}"
}
}
}
}
}
在测试执行之后,把配置中构建触发器的url地址复制到gitlab的webhooks的url上
在gitalb上进入管理员模式,允许本地触发(管理员-设置-网络-出站请求)
去gitlab仓库项目上,对相应项目设置webooks,令牌输入测试执行的令牌,并选定推送分支与合并请求,取消ssh验证并确保gitlab主机能够解析jenkins主机名
保存后点击测试,推送事件
回到jenkins pipeline查看
如果合并完成后,需要点赞,
第一须在语法中加入(addVoteOnMergeRequest: true,)
triggers {
gitlab(triggerOnPush: true,
triggerOnMergeRequest: true,
branchFilterType: 'All',
addVoteOnMergeRequest: true,
secretToken: '9iH0tq33ROMS07przzLiclNmfqSaoIeTOolHYf1E')
第二在gitlab之上为当前用户设置令牌,并把令牌保存相应作业上的credentials
个人资料-访问令牌 输入gitlab-root-access-token生成令牌(glpat-Xrvy-kypoWA7-wxDMRv7)
在jenkins上添加credentials
到Jenkins的系统管理处的gitlab
做完之后,就会完成点赞。
Generic Webhook 触发示例
需要安装Generic Webhook Trigger插件
GenericTrigger(
genericVariables: [
[key: 'ref', value: '$.ref']
],
token: 'fClZ0e/kTcqL2ARh7YqxW/3ndOCZA2SqfKnRTLat',
causeString: 'Triggered on $ref',
printContributedVariables: true,
printPostContent: true
)
pipeline {
agent any
parameters {
booleanParam(name:'pushImage', defaultValue: 'true', description: 'Push Image to Harbor?')
}
tools {
maven 'maven-3.8.7'
}
triggers {
GenericTrigger(
genericVariables: [
[key: 'ref', value: '$.ref']
],
token: 'fClZ0e/kTcqL2ARh7YqxW/3ndOCZA2SqfKnRTLat',
causeString: 'Triggered on $ref',
printContributedVariables: true,
printPostContent: true
)
}
environment {
codeRepo="http://gitlab.mengfanchao.com/root/spring-boot-helloWorld.git"
harborServer='harbor.meng.org'
projectName='spring-boot-helloworld'
imageUrl="${harborServer}/ikubernetes/${projectName}"
imageTag='latest'
}
stages {
stage('Source') {
steps {
git branch: 'main', credentialsId: 'gitlab-root-credential', url: "${codeRepo}"
}
}
stage('Build') {
steps {
sh 'mvn -B -DskipTests clean package'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('Build Docker Image') {
steps {
sh 'docker image build . -t "${harborServer}/ikubernetes/${projectName}:${imageTag}"'
}
}
stage('Push Docker Image') {
steps {
withCredentials([usernamePassword(credentialsId: 'harbor-user-credential',
passwordVariable: 'harborUserPassword', usernameVariable: 'harborUserName')]) {
sh "echo ${harborUserPassword} | docker login -u ${env.harborUserName} --password-stdin ${harborServer}"
sh "docker image push ${imageUrl}:${imageTag}"
}
}
}
}
}
立即构建完成后
在命令行中进行触发
curl -X POST -H "Content-Type: application/json" -d '{ "ref": "refs/heads/main" }' -vs http://10.0.0.100:8080/generic-webhook-trigger/invoke?token="fClZ0e/kTcqL2ARh7YqxW/3ndOCZA2SqfKnRTLat"
使用gitlab之上的仓库进行触发(选择推送事件、合并请求事件并取消ssh验证)
流水线被自动触发
服务器租用托管,机房租用托管,主机租用托管,https://www.e1idc.com