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

How can I trigger one Jenkins job on two events #223

Closed BarelElbaz closed 1 year ago

BarelElbaz commented 2 years ago

Hey, I'm trying to trigger Jenkins multibranch pipeline on two events: 1) push 2) pull request approved

There is any solution for this? SCM: bitbucket the payload of the events are comletly different so I can't use same key for determine branch

What I try to do is to create multiple generic variables that fits the 2 different events and check if one of them equals to null and then determine what event was trigger the job.

The problem is: regexpFilterText and regexpFilterExpression fields can't contain logic of two variables with 'OR' operation between them

Can you help me?

tomasbjerre commented 2 years ago

Create both variables. One resolving it for PR and one resolving it for branch push.

Use both variables in the text. Only one of them will be resolved to the value.the other one, not found in the webhook, will resolve to nothing.

Write the regexp as usual.

BarelElbaz commented 2 years ago
    GenericTrigger(
        genericVariables: [
        [key: 'ref', value: '$.changes[0].refId'],
        [key: 'ref2', value: '$.pullRequest.fromRef.id']
        ],

        causeString: 'Triggered on $ref',

        token: 'test',
        // tokenCredentialId: '',

        printContributedVariables: true,
        printPostContent: true,

        silentResponse: false,

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

    **ref refers to push**
    **ref2 refers to PR**
BarelElbaz commented 2 years ago

This should work?

tomasbjerre commented 2 years ago

Yes.

See also: https://github.com/jenkinsci/generic-webhook-trigger-plugin/blob/master/src/test/resources/org/jenkinsci/plugins/gwt/bdd/bitbucket-server/bitbucket-server-refs_changed.feature

BarelElbaz commented 2 years ago

Thanks but how can I detect which trigger was triggered me? I tried to check with IF statement but I get error: No such property ref2 because one of the refs will not be declared

tomasbjerre commented 2 years ago

You can google on how to check if variable exists in Jenkins.

You can also set some default values.

StefanIGit commented 2 years ago

Hi, I have a multi branch pipeline running with bitbucket cloud with this plugin https://plugins.jenkins.io/cloudbees-bitbucket-branch-source/

BarelElbaz commented 2 years ago

@tomasbjerre Regarding our discussion, when I use: regexpFilterText: '$ref$ref2' I can't use ^ and $ on my filter expression (causes feature/testing trigerring also for feature/testing2 branch) currently expression is:

'refs/heads/' + BRANCH_NAME

but I need something like:

'^refs/heads/' + BRANCH_NAME + '$'

and due to the fact that sometime ref or ref2 are not defined the regex aborting my trigger