Harbor(Webhook)+Jenkins+Gitlab(Webhook)

插件

Generic Webhook Trigger Plugin

1.Harbor(Webhook)+Jenkins收到推送镜像自动发布

配置

pipeline {
    agent any
    triggers {
      GenericTrigger(
              genericVariables: [
                [key: 'harbor_type', value: '$.tpye', expressionType: 'JSONPath'],
                [key: 'harbor_image', value: '$.event_data.resources[0].resource_url', expressionType: 'JSONPath'],
                [key: 'harbor_namespace', value: '$.event_data.repository.namespace', expressionType: 'JSONPath'],
                [key: 'repo_name', value: '$.event_data.repository.name', expressionType: 'JSONPath'],
                [key: 'harbor_type', value: '$.tpye', expressionType: 'JSONPath'],
                [key: 'harbor_type', value: '$.tpye', expressionType: 'JSONPath'],
                [key: 'COMMIT', value: '$.after',expressionType: 'JSONPath']
              ],
              token: 'demo-harbor-webhook' ,
              causeString: ' Triggered on $branch' ,
              printContributedVariables: true,
              printPostContent: true,
              //regexpFilterText: '$ref',
              //regexpFilterExpression: 'refs/heads/' + BRANCH_NAME
              regexpFilterText: '$harbor_type#$harbor_namespace#$repo_name',
              regexpFilterExpression: 'pushImage#library#nav'
          )
    }
    stages {
        stage('Hello') {
            steps {
                sh '''
                echo harbor_type=$harbor_type
                echo harbor_image=$harbor_image
                echo repo_name=$repo_name
                echo harbor_namespace=$harbor_namespace
                echo "do something..."
                #kubectl set image deployment.apps/$repo_name $repo_name=$harbor_image
                '''
            }
        }
    }
}

curl测试webhook是否可用 https://github.com/keel-hq/keel/issues/510

# with auth header (user:name as base64)
curl -v -X POST -d '{"type": "PUSH_ARTIFACT", "occur_at": 1592659997, "operator": "robot$ci-user", "event_data": {"resources": [{"digest": "sha256:ff11b0b5aaab2432d17b143ecbe08202ea7f6ca3839253874ed9b0acc345a879", "tag": "latest", "resource_url": "example.com/repo/imagename:latest"}], "repository": {"date_created": 1592200834, "name": "imagename", "namespace": "repo", "repo_full_name": "repo/imagename", "repo_type": "private"}}}' -H "Content-Type: application/json" -H "Authorization: Basic asdasdasdasd" https://keel.example.com/v1/webhooks/registry

# without auth header
curl -v -X POST -d '{"type": "PUSH_ARTIFACT", "occur_at": 1592659997, "operator": "robot$ci-user", "event_data": {"resources": [{"digest": "sha256:ff11b0b5aaab2432d17b143ecbe08202ea7f6ca3839253874ed9b0acc345a879", "tag": "latest", "resource_url": "example.com/repo/imagename:latest"}], "repository": {"date_created": 1592200834, "name": "imagename", "namespace": "repo", "repo_full_name": "repo/imagename", "repo_type": "private"}}}' -H "Content-Type: application/json" https://keel.example.com/v1/webhooks/registry

harbor-webhook 示例

curl --location --request GET 'http://192.168.1.61:8080/generic-webhook-trigger/invoke?token=demo-harbor-webhook' \
--header 'Content-Type: application/json' \
--data-raw '{
  "type": "pushImage",
  "occur_at": 1591172171,
  "event_data": {
    "resources": [{
      "digest": "sha256:8896bb6b310b53ff03c2172db6b2fa7841233d0de516021e9a4912bdef11aed3",
      "tag": "20200603",
      "resource_url": "test-docker.jchl.com/sbpt/icap-gateway-main:20200603"
    }],
    "repository": {
      "date_created": 1588750292,
      "name": "icap-gateway-main",
      "namespace": "sbpt",
      "repo_full_name": "sbpt/icap-gateway-main",
      "repo_type": "public"
    }
  },
  "operator": "sbpt"
}'

2.Gitlab(Webhook)+Jenkins收到代码提交自动发布

pipeline {
    agent any
    environment {
      repoUrl = "https://gitlab.mvmyun.com/demo/devci.git"
    }
    parameters {
      string(name:'repoBranch', defaultValue: 'master', description: 'git分支名称')  
    }
    triggers {
      GenericTrigger(
              genericVariables: [
                [key: 'branch', value: '$.ref'],
                [key: 'COMMIT', value: '$.after']
              ],
              token: 'testpipeline-yxgj-marketing-job_PUSH' ,
              causeString: ' Triggered on $branch' ,
              printContributedVariables: true,
              printPostContent: true
          )
    }
    stages {
      stage('getBranch'){
        steps{
          script{
            try{                    
              if("${branch}" != ""){ 
                println "----------webhook式触发-----------"
                branchName = branch - "refs/heads"
                branchName = sh(returnStdout: true,script: "echo ${branchName}|awk -F '/' '{print \$NF}'").trim()
                println "webhook触发的分支是: " + "${branchName}"
              }
            } catch(exc) { }           
              if("${params.repoBranch}" != ""){
                println "-----------手动方式触发------------"
                branchName = "${params.repoBranch}"
                println "手动触发的分支是: " + "${branchName}"
              }           
          }  
        } 
      }    
      stage('checkOut') {
        steps{
          checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanBeforeCheckout']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '4axxx', url: "${repoUrl}"]]])
        }
      }
    }
}

参考

https://www.cnblogs.com/sxusky/p/13039250.html