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
403 stars 159 forks source link

Pipelines inside a Multibranch Pipeline not being found #314

Closed matheushoeltgebaum closed 13 hours ago

matheushoeltgebaum commented 1 week ago

Jenkins and plugins versions report

I have a Multibranch Pipeline mapped to a Git repository, hosted in our company server. I was looking for a way to trigger the pipelines defined inside this multibranch project, and found this plugin.

So far, those pipelines are not being found, when I do a request to Jenkins. I'm getting the following error:

{"jobs":null,"message":"Did not find any jobs with GenericTrigger configured!\nA token was supplied.\n"}* Connection #0 to host JENKINS-SERVER left intact

I'm using a Jenkins file inside our repository, to configure the pipeline, with the declarative syntax. Here's how the triggers section is defined:

triggers {
    GenericTrigger(
      genericVariables: [
        [key: 'ref', value: '$.ref']
      ],

      token: 'abc123',
      tokenCredentialId: '',

      causeString: 'Triggered on $ref',

      printPostContent: true,
      printContributedVariables: true,

      silentResponse: false,
      shouldNotFlatten: false,

      regexpFilterExpression: 'refs/heads/' + BRANCH_NAME,
      regexpFilterText: '$ref'
    )
  }

And here is how this configuration is applied in Jenkins: image image image image

I'm using the following Curl command: curl -vs "http://JENKINS-SERVER/generic-webhook-trigger/invoke?token=abc123"

However, if I create a simple pipeline, using the same generic webhook trigger configuration as above, then the job is found when I do the same Curl command, getting the response below:

{"jobs":{"DSTeste":{"regexpFilterExpression":"refs/heads/master","triggered":false,"resolvedVariables":{"ref":""},"regexpFilterText":"","id":0,"url":""}},"message":"Triggered jobs."}* Connection #0 to host JENKINS-SERVER left intact

Expected result

Pipelines inside a multibranch project should be found

Actual result

Pipelines inside a multibranch project are not being found

Jenkins and plugins versions report

Jenkins: 2.426.3 OS: Linux - 5.15.0-58-generic Java: 11.0.16 - Eclipse Adoptium (OpenJDK 64-Bit Server VM)

ace-editor:1.1 ansicolor:1.0.4 apache-httpcomponents-client-4-api:4.5.14-208.v438351942757 asm-api:9.7-33.v4d23ef79fcc8 bootstrap5-api:5.3.2-3 bouncycastle-api:2.30.1.77-225.v26ea_c9455fd9 branch-api:2.1144.v1425d1c3d5a_7 caffeine-api:3.1.8-133.v17b_1ff2e0599 checks-api:2.0.2 cloudbees-folder:6.858.v898218f3609d command-launcher:107.v773860566e2e commons-lang3-api:3.13.0-62.v7d18e55f51e2 commons-text-api:1.11.0-95.v22a_d30ee5d36 credentials:1319.v7eb_51b_3ac97b credentials-binding:657.v2b_19db_7d6e6d dependency-track:4.3.1 discord-notifier:241.v448b_ccd0c0d6 display-url-api:2.200.vb_9327d658781 durable-task:547.vd1ea_007d100c echarts-api:5.4.3-2 font-awesome-api:6.5.1-2 generic-webhook-trigger:2.2.1 git:5.2.1 git-client:4.6.0 gson-api:2.10.1-15.v0d99f670e0a_7 instance-identity:185.v303dc7c645f9 ionicons-api:56.v1b_1c8c49374e jackson2-api:2.16.1-373.ve709c6871598 jakarta-activation-api:2.0.1-3 jakarta-mail-api:2.0.1-3 javax-activation-api:1.2.0-6 javax-mail-api:1.6.2-9 jaxb:2.3.9-1 jdk-tool:73.vddf737284550 joda-time-api:2.12.6-21.vca_fd74418fb7 jquery3-api:3.7.1-1 jsch:0.2.16-86.v42e010d9484b json-path-api:2.9.0-58.v62e3e85b_a_655 junit:1256.v002534a_5f33e locale:431.v3435fa8f8445 mailer:463.vedf8358e006b matrix-project:822.824.v14451b_c0fd42 mina-sshd-api-common:2.12.0-90.v9f7fb_9fa3d3b mina-sshd-api-core:2.12.0-90.v9f7fb_9fa3d3b msbuild:1.30 okhttp-api:4.11.0-172.vda_da_1feeb_c6e pipeline-build-step:540.vb_e8849e1a_b_d8 pipeline-graph-analysis:202.va_d268e64deb_3 pipeline-groovy-lib:704.vc58b_8890a_384 pipeline-input-step:477.v339683a_8d55e pipeline-milestone-step:111.v449306f708b_7 pipeline-model-api:2.2175.v76a_fff0a_2618 pipeline-model-definition:2.2175.v76a_fff0a_2618 pipeline-model-extensions:2.2175.v76a_fff0a_2618 pipeline-stage-step:305.ve96d0205c1c6 pipeline-stage-tags-metadata:2.2175.v76a_fff0a_2618 plain-credentials:182.v468b_97b_9dcb_8 plugin-util-api:3.8.0 popper2-api:2.11.6-4 postbuild-task:1.9 scm-api:683.vb_16722fb_b80b script-security:1313.v7a_6067dc7087 snakeyaml-api:2.2-111.vc6598e30cc65 sonar:2.17.1 ssh-credentials:308.ve4497b_ccd8f4 sshd:3.303.vefc7119b_ec23 structs:337.v1b_04ea_4df7c8 trilead-api:2.133.vfb_8a_7b_9c5dd1 variant:60.v7290fc0eb_b_cd workflow-aggregator:596.v8c21c963d92d workflow-api:1291.v51fd2a_625da_7 workflow-basic-steps:1042.ve7b_140c4a_e0c workflow-cps:3853.vb_a_490d892963 workflow-durable-task-step:1317.v5337e0c1fe28 workflow-job:1385.vb_58b_86ea_fff1 workflow-multibranch:773.vc4fe1378f1d5 workflow-scm-step:415.v434365564324 workflow-step-api:657.v03be8115821b workflow-support:865.v43e78cc44e0d

tomasbjerre commented 1 week ago

Remove the regexp filter. Or supply a json body in the curl command that matches the regexp.

matheushoeltgebaum commented 1 week ago

Tried both suggestions, but still got the same message that no job was found.

I did the following Curl command, to provide the json body:

curl -v "http://JENKINS-SERVER/generic-webhook-trigger/invoke?token=abc123" -H "Content-Type: application/json" -d "{\"ref\": \"refs/heads/master\"}"

Then, I removed both "regexpFilterExpression" and "regexpFilterText" from my Jenkinsfile, like below:

triggers {
    GenericTrigger(
      genericVariables: [
        [key: 'ref', value: '$.ref']
      ],

      token: 'abc123',
      tokenCredentialId: '',
      causeString: 'Triggered on $ref',
      printPostContent: true,
      printContributedVariables: true,
      silentResponse: false,
      shouldNotFlatten: false,
      regexpFilterExpression: '',
      regexpFilterText: ''
    )
  }

And tried the following Curl command:

curl -vs "http://JENKINS-SERVER/generic-webhook-trigger/invoke?token=abc123"
tomasbjerre commented 1 week ago

You can clone this repo and debug that configuration, I will not have time to do that for a few days.

This might be related to #311 and #312, but it is a long shot. If so it was fixed in latest Jenkins LTS (https://github.com/jenkinsci/jenkins/pull/9304 https://github.com/jenkinsci/jenkins/pull/9303). Maby try with Jenkins 2.452.2.

tomasbjerre commented 3 days ago

I tried this here: https://github.com/tomasbjerre/gwt-issue-314/

I can trigger it with:

curl -v "http://localhost:8080/jenkins/generic-webhook-trigger/invoke?token=abc123" -d '{ "ref": "refs/heads/main"}' -H "Content-Type: application/json"

Using Jenkins 2.452.2.

CarryWll commented 21 hours ago

I also encountered the same problem. How can I resolve it?

matheushoeltgebaum commented 13 hours ago
* Did you add a build-step?

Yes, here's my Jenkinsfile:

pipeline {
  agent { label 'WinSlaveDELIVERERDSV2' }
  options {
    buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '10', daysToKeepStr: '', numToKeepStr: '10')
    disableConcurrentBuilds()
  }
  triggers {
    GenericTrigger(
      genericVariables: [
        [key: 'ref', value: '$.ref']
      ],

      token: 'abc123',
      tokenCredentialId: '',

      causeString: 'Triggered on $ref',

      printPostContent: true,
      printContributedVariables: true,

      silentResponse: false,
      shouldNotFlatten: false,

      regexpFilterExpression: '',
      regexpFilterText: ''
    )
  }
  stages {
    stage('Hello') {
      steps {
        echo 'hello'
        bat 'echo $ref'
      }
    }
  }
}
* Did you supply the `Content-Type: application/json` header?

Yes, I have done that, using the following command:

curl -v "http://JENKINS-SERVER/generic-webhook-trigger/invoke?token=abc123" -H "Content-Type: application/json" -d "{\"ref\": \"refs/heads/master\"}"

I managed a workaround, using the "multibranch-scan-webhook-trigger" plugin, to trigger the pipelines. It's not ideal, since it's going to trigger everything, but it's alright for our current scenario.

I believe the problem it's related to Jenkins itself, using the latest version will probably fix this. I'm planning on updating it, but I won't have time to do it for a few days. So I'm going to close this issue for now. If updating Jenkins won't fix this, I'll reopen it.

Anyway, thank you for your help so far @tomasbjerre!