jenkinsci / generic-webhook-trigger-plugin

Can receive any HTTP request, extract any values from JSON or XML and trigger a job with those values available as variables. Works with GitHub, GitLab, Bitbucket, Jira and many more.
https://plugins.jenkins.io/generic-webhook-trigger
410 stars 161 forks source link

How do I extract variables from triggers Generic Plugin in to a Stage ? #179

Closed Thilaknath closed 4 years ago

Thilaknath commented 4 years ago

In the example given below, when I try to use the prNumber variable within a stage, I get the error

groovy.lang.MissingPropertyException: No such property: prNumber for class: groovy.lang.Binding

Can you please advice what I am missing in the below script, Rest of the implementation works fine so far.

pipeline {
    agent any
    triggers {
        GenericTrigger(
                genericVariables: [
                        [key: 'prNumber', value: 'number'],
                        [key: 'prAction', value: 'action'],
                        [key: 'prMergeStatus', value: 'pull_request.merged']
                ],

                causeString: '$prNumber merged',

                printContributedVariables: true,
                printPostContent: true,

                silentResponse: false,

                regexpFilterText: '$prMergeStatus',
                regexpFilterExpression: '\\b(\\w*true\\w*)\\b'
        )
    }
    stages {
        stage('Job to Delete Stale Apps'){
            steps{
                echo "Start excecution of stage: ${prNumber}"
                catchError(buildResult: 'FAILURE', stageResult: 'FAILURE'){
                    dockerExecute(
                            script: this,
                            dockerImage: '************',
                            dockerWorkspace: '/home/piper',
                            dockerEnvVars: [CF_HOME: "/home/piper", CF_PLUGIN_HOME: "/home/piper"]
                    ){
                        withCredentials(
                                [usernamePassword(credentialsId: '************', passwordVariable: 'pwd', usernameVariable: 'user')]
                        ) {
                            script{
                                sh "cf login -u \"${user}\" -p '${pwd}' -a '************' -o '************' -s '************'"
                                sh "cf apps"
                            }
                        }
                    }
                }
            }
            post{
                always{
                    deleteDir()
                }
            }
        }
    }
}
tomasbjerre commented 4 years ago

Value here is not a jsonpath:

[key: 'prNumber', value: 'number'],

Thilaknath commented 4 years ago

@tomasbjerre I am able to fetch the value as I want with the above configuration itself. But even I specify it as [key: 'prNumber', value: '$.number'] and refer prNumber in the stage below it complains of the same error.

tomasbjerre commented 4 years ago

I changed the pipeline like this:

pipeline {
    agent any
    triggers {
        GenericTrigger(
                token: 'abc123',

                genericVariables: [
                        [key: 'prNumber', value: 'number'],
                        [key: 'prAction', value: 'action'],
                        [key: 'prMergeStatus', value: 'pull_request.merged']
                ],

                causeString: '$prNumber merged',

                printContributedVariables: true,
                printPostContent: true,

                silentResponse: false,

        )
    }
    stages {
        stage('Job to Delete Stale Apps'){
            steps{
                echo "Start excecution of stage: ${prNumber}"
            }
            post{
                always{
                    deleteDir()
                }
            }
        }
    }
}

And I trigger it like this:

curl -v -H "Content-Type: application/json" -X POST -d '{ "number": 123}' http://localhost:8080/generic-webhook-trigger/invoke?token=abc123
Note: Unnecessary use of -X or --request, POST is already inferred.
* Expire in 0 ms for 6 (transfer 0x557a8527b5c0)
...
* Expire in 0 ms for 1 (transfer 0x557a8527b5c0)
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Expire in 200 ms for 4 (transfer 0x557a8527b5c0)
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /generic-webhook-trigger/invoke?token=abc123 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.64.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 16
> 
* upload completely sent off: 16 out of 16 bytes
< HTTP/1.1 200 OK
< Date: Sat, 29 Aug 2020 13:36:57 GMT
< X-Content-Type-Options: nosniff
< Content-Type: application/json;charset=utf-8
< Content-Length: 192
< Server: Jetty(9.4.30.v20200611)
< 
* Connection #0 to host localhost left intact
{"jobs":{"pipe generic":{"regexpFilterExpression":"","triggered":true,"resolvedVariables":{"prNumber":"123"},"regexpFilterText":"","id":13,"url":"queue/item/13/"}},"message":"Triggered jobs."}

It builds and log looks like:

123 merged
Running in Durability level: MAX_SURVIVABILITY
Loading library sandbox@master
Attempting to resolve master from remote references...
 > git --version # timeout=10
 > git --version # 'git version 2.11.0'
 > git ls-remote -- https://github.com/tomasbjerre/jenkins-configuration-as-code-sandbox.git # timeout=10
Found match: refs/heads/master revision bf7e3cea6f49da551f90e46343553da874f77cae
GenericWebhookEnvironmentContributor
 Received:

{ "number": 123}

Contributing variables:

    prNumber = 123

The recommended git tool is: NONE
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/tomasbjerre/jenkins-configuration-as-code-sandbox.git # timeout=10
Using shallow fetch with depth 1
Fetching upstream changes from https://github.com/tomasbjerre/jenkins-configuration-as-code-sandbox.git
 > git --version # timeout=10
 > git --version # 'git version 2.11.0'
 > git fetch --no-tags --progress --depth=1 -- https://github.com/tomasbjerre/jenkins-configuration-as-code-sandbox.git +refs/heads/*:refs/remotes/origin/* # timeout=10
Checking out Revision bf7e3cea6f49da551f90e46343553da874f77cae (master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f bf7e3cea6f49da551f90e46343553da874f77cae # timeout=10
Commit message: "Create FUNDING.yml"
 > git rev-list --no-walk bf7e3cea6f49da551f90e46343553da874f77cae # timeout=10
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/pipe generic
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Job to Delete Stale Apps)
[Pipeline] echo
Start excecution of stage: 123
Post stage
[Pipeline] deleteDir
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Thilaknath commented 4 years ago

@tomasbjerre Let me try this out it doesn't look much different from my script. I am struggling to understand the difference here

tomasbjerre commented 4 years ago

I just removed the dockerstuff because i dont want to set that up....

Den mån 31 aug. 2020 14:06Thilaknath notifications@github.com skrev:

@tomasbjerre https://github.com/tomasbjerre Let me try this out it doesn't look much different from my script. I am struggling to understand the difference here

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jenkinsci/generic-webhook-trigger-plugin/issues/179#issuecomment-683738002, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADLKE3XAHRSR73XY6ISFBTSDOG5TANCNFSM4QNJC6CQ .