使用generic-webhook-trigger插件时,如何在Job DSL脚本中创建多个genericVariables?

时间:2019-05-26 01:04:13

标签: jenkins jenkins-plugins jenkins-groovy jenkins-job-dsl

我找到了一个很好的示例here,并使用此代码添加了其他泛型变量,但我只是不知道如何更改语法。

pipelineJob('Generic Job Example') {
 parameters {
  stringParam('VARIABLE_FROM_POST', '')
 }

 triggers {
  genericTrigger {
   genericVariables {
    genericVariable {
     key("VARIABLE_FROM_POST")
     value("\$.something")
     expressionType("JSONPath") //Optional, defaults to JSONPath
     regexpFilter("") //Optional, defaults to empty string
     defaultValue("") //Optional, defaults to empty string
    }
   }
   genericRequestVariables {
    genericRequestVariable {
     key("requestParameterName")
     regexpFilter("")
    }
   }
   genericHeaderVariables {
    genericHeaderVariable {
     key("requestHeaderName")
     regexpFilter("")
    }
   }
   token('abc123')
   printContributedVariables(true)
   printPostContent(true)
   silentResponse(false)
   regexpFilterText("\$VARIABLE_FROM_POST")
   regexpFilterExpression("aRegExp")
  }
 }

 definition {
  cps {
   // Or just refer to a Jenkinsfile containing the pipeline
   script('''
 node {
 stage('Some Stage') {
 println "VARIABLE_FROM_POST: " + VARIABLE_FROM_POST
 }
 }
 ''')
   sandbox()
  }
 }
}

这部分让我配置第一个变量。

key("VARIABLE_FROM_POST")
value("\$.something")

但是,如果我添加另一个代码块并尝试添加另一个变量,如下所示:

pipelineJob('Generic Job Example') {
 parameters {
  stringParam('VARIABLE_FROM_POST', '')
 }

 triggers {
  genericTrigger {
   genericVariables {
    genericVariable {
     key("VARIABLE_FROM_POST")
     value("\$.something")
     expressionType("JSONPath") //Optional, defaults to JSONPath
     regexpFilter("") //Optional, defaults to empty string
     defaultValue("") //Optional, defaults to empty string
    genericVariable {
     key("project")
     value("\$.repository.name")
     expressionType("JSONPath") //Optional, defaults to JSONPath
     regexpFilter("") //Optional, defaults to empty string
     defaultValue("") //Optional, defaults to empty string
    }
    }
   }
   genericRequestVariables {
    genericRequestVariable {
     key("requestParameterName")
     regexpFilter("")
    }
   }
   genericHeaderVariables {
    genericHeaderVariable {
     key("requestHeaderName")
     regexpFilter("")
    }
   }
   token('abc123')
   printContributedVariables(true)
   printPostContent(true)
   silentResponse(false)
   regexpFilterText("\$VARIABLE_FROM_POST")
   regexpFilterExpression("aRegExp")
  }
 }

 definition {
  cps {
   // Or just refer to a Jenkinsfile containing the pipeline
   script('''
 node {
 stage('Some Stage') {
 println "VARIABLE_FROM_POST: " + VARIABLE_FROM_POST
 }
 }
 ''')
   sandbox()
  }
 }
}

这将导致仅使用一个变量(项目为变量名)来创建作业。

我尝试了其他语法操作,但没有运气。是否有链接显示如何创建这些脚本?我检出了Jenkins Job DSL API here,但找不到答案。

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,发现此链接有用:https://diycode.cc/projects/jenkinsci/generic-webhook-trigger-plugin 我正在使用通用的webhooks插件,但无法访问组织中的gitlab违规,因此我在链接中使用了声明性的jenkinsfile示例,并提出了类似的方法,该方法有效:

pipeline {
    agent any
    parameters {
        string( name: 'action_type', defaultValue: '', description: '')
        string( name: 'action', defaultValue: '', description: '')
        string( name: 'packageid', defaultValue: '', description: '')
        string( name: 'toenvironment', defaultValue: '', description: '')
        string( name: 'totype', defaultValue: '', description: '')
        string( name: 'actionreturncode', defaultValue: '', description: '')
        string( name: 'actionccid', defaultValue: '', description: '')
        string( name: 'elementname', defaultValue: '', description: '')
        string( name: 'tosystem', defaultValue: '', description: '')
        string( name: 'tosubsystem', defaultValue: '', description: '')
        string( name: 'tostageID', defaultValue: '', description: '')
        string( name: 'actionuserid', defaultValue: '', description: '')
    }
    triggers {
        GenericTrigger(
            genericVariables: [
                [key: 'action_type', value:'$.actiontype'],
                [key: 'action', value: '$.action'],
                [key: 'packageid', value: '$.packageid'],
                [key: 'toenvironment', value: '$.toenvironment'],
                [key: 'totype', value: '$.totype'],
                [key: 'actionreturncode', value: '$.actionreturncode'],
                [key: 'actionccid', value: '$.actionccid'],
                [key: 'elementname', value: '$.elementname'],
                [key: 'tosystem', value: '$.tosystem'],
                [key: 'tosubsystem', value: '$.tosubsystem'],
                [key: 'tostageID', value: '$.tostageID'],
                [key: 'actionuserid', value: '$.actionuserid']
            ],
            causeString: 'Tirggered on $action_type',                
            regexpFilterText: '$totype',
            regexpFilterExpression: 'COBOL'
        )
    }
    stages {
        stage ('initialise parms etc...') {

答案 1 :(得分:0)

我认为您更喜欢此链接https://www.geogebra.org/m/HSgSE469

triggers {
  genericTrigger {
   genericVariables {
    genericVariable {
     key("MERGE_REQUEST_TO_URL")
     value("\$.object_attributes.target.git_http_url")
     expressionType("JSONPath")
     regexpFilter("")
    }
    genericVariable {
     key("MERGE_REQUEST_FROM_URL")
     value("\$.object_attributes.source.git_http_url")
     expressionType("JSONPath")
     regexpFilter("")
    }
    genericVariable {
     key("MERGE_REQUEST_TO_BRANCH")
     value("\$.object_attributes.target_branch")
     expressionType("JSONPath")
     regexpFilter("")
    }
    genericVariable {
     key("MERGE_REQUEST_FROM_BRANCH")
     value("\$.object_attributes.source_branch")
     expressionType("JSONPath")
     regexpFilter("")
    }
    genericVariable {
     key("PROJECT_ID")
     value("\$.object_attributes.target_project_id")
     expressionType("JSONPath")
     regexpFilter("")
    }
    genericVariable {
     key("MERGE_REQUST_ID")
     value("\$.object_attributes.id")
     expressionType("JSONPath")
     regexpFilter("")
    }
    genericVariable {
     key("MR_OBJECT_KIND")
     value("\$.object_kind")
     expressionType("JSONPath")
     regexpFilter("")
    }
    genericVariable {
     key("MR_OLD_REV")
     value("\$.object_attributes.oldrev")
     expressionType("JSONPath")
     regexpFilter("")
    }
    genericVariable {
     key("MR_ACTION")
     value("\$.object_attributes.action")
     expressionType("JSONPath")
     regexpFilter("")
    }
   }