gitlab提交代码自动触发jenkins pipeline

警告
本文最后更新于 2020-05-30 17:09,文中内容可能已过时。

1.配置jenkins(需要先安装Generic Webhook Trigger插件)

获取gitlab提交的分支,赋给变量branch

 

加一个webhook参数,用于判断触发构建的类型:

 

填写token:

 

打印相关内容和变量,方便调试:

 

2.gitlab配置

URL:http://10.0.0.51:8080/generic-webhook-trigger/invoke?token=demo-maven-service_PUSH&runType=gitlabpush

 

修改pipeline动态获取分支名称

#!groovy
@Library('jenkins-sharelibrary@master')
def tools = new org.devops.tools()

String srcUrl = "${env.srcUrl}"
String branchName = "${env.branchName}"
String buildType = "${buildType}"
String buildShell = "${buildShell}"

try{
    if ( "${runType}" == "gitlabpush" ){
        branchName = "${branch}"
    }
}catch(Exception e){
    println(branchName)
}

currentBuild.description = "构建分支:${branchName}"

pipeline{
    agent { 
        node { label 'master'} 
    }
    stages{
        stage('GetCode'){
            steps{
                script{
                    tools.myprint("正在拉取代码","green")
                    checkout([$class: 'GitSCM', 
                            branches: [[name: "${branchName}"]], 
                            doGenerateSubmoduleConfigurations: false, 
                            extensions: [], 
                            submoduleCfg: [], 
                            userRemoteConfigs: [[credentialsId: '476e5130-258c-4e7e-a80e-4ea8a303a985', 
                                                url: "${srcUrl}"]]]) 
                }
            }
        }
        stage('Build'){
            steps{
                script{
                    tools.build("${buildType}","${buildShell}")
                }
            }
        }
    }
}

 

 

 

 

请我喝杯水
SoulChild 微信号 微信号
SoulChild 微信打赏 微信打赏
0%