0

I found a good example here and I used this code to add additional generic variable, but I just don't know how to change the syntax.

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()
  }
 }
}

This part let's me config the first variable.

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

However, if I add another code block and try to add another variable like this:

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()
  }
 }
}

This results in job creation with only one variable, project as the variable name.

I tried other syntax manipulation and no luck. Is there link where it shows how these scripts can be created? I checkout the Jenkins Job DSL API here and I could not find the answer.

1
  • I cannot reproduce the problem. Do you use the latest version of all plugins and the latest Jenkins LTS release? Commented Jun 12, 2019 at 7:53

2 Answers 2

3

I had the same problem and found this link useful: https://diycode.cc/projects/jenkinsci/generic-webhook-trigger-plugin I am using the generic webhooks plugin but don't have access to the violation to gitlab one in my organisation, so I used the declaritive jenkinsfile example in the link and came up with something like this, which worked:

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...') {

Sign up to request clarification or add additional context in comments.

Comments

0

I think you can prefer this link https://wiki.jenkins.io/display/JENKINS/Violation+Comments+to+GitLab+Plugin

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("")
    }
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.