jenkins Webhook Trigger 使用

  1. Jenkins 插件中心安装插件 Generic Webhook Trigger
  2. 设置API Token
    Jenkins -> 系统管理 -> 管理用户 -> 设置 -> API Token -> 添加 Token
  3. 配置 Gitlab webHook

    添加用戶名、token
    然後保存測試
    当点击触发事件的时候,jenkins 就会收到gitlab的请求

jenkins pipeline 脚本

pipeline{
    agent {
       label "master"
     }
    parameters {
         choice(choices: 'dev\nprod',description: '请选择你要构建的环境?', name: 'region')
        //   choice(choices: 'one', description: '请选择你要构建的链 ?', name: 'chain_type')
    //  string(name: 'branch', defaultValue: 'master', description: 'branch')
    //  string(name: 'region', defaultValue: 'dev', description: 'dev/prod')
    }
        environment {
         P_NAME="${JOB_NAME}"
        access_token = "https://oapi.dingtalk.com/robot/send?access_token=94609658fadsfadsfadsf499f59ea054db78d79fe765eb3bccf332886dc5a9e6"
        region=''
        }
    triggers {
        GenericTrigger(
            genericVariables: [
                [key: 'branch', value: '$.ref'],
                [key: 'region', value: 'dev'],
                [key: 'git_http_url', value: '$.repository.git_http_url'],
                [key: 'git_ssh_url', value: '$.repository.git_ssh_url'],
                [key: 'commit_message', value: '$.commits[0].message'],
                [key: 'commit_author_name', value: '$.commits[0].author.name'],
                [key: 'commit_author_email', value: '$.commits[0].author.email']
                
                ],
            causeString: 'Triggered on $ref',
            token: '576ccc294a9372624417a176f426812da46375fcf3289e943cc1de21f53c757c',
            printContributedVariables: true,
            printPostContent: true,
            silentResponse: false
        )
    }
 
        
     
     
stages {
     stage('get code'){
      steps {
          script{
		    branchName = branch.substring("refs/heads/".length())
		    git branch: "${branchName}",credentialsId: 'b9330ecb-5f81-45a3-dfadf-d108a3a05404', url: "${git_http_url}"
      }
}
     }
    
    
stage('sync code'){
  steps{
     script{
        
          if ("${env.region}" == "prod"){
echo  "prod"

                }
                else{
                    sh "echo test"

                }

             }
         
      }
}


}
}
posted @ 2022-08-30 15:35  xiaoganghu  阅读(281)  评论(0编辑  收藏  举报