Jenkins Generic Webhook Trigger, for a merge-commit

1.8k Views Asked by At

I am trying to generate a webhook from Github to trigger a build whenever a feature branch gets merged to the specific branch (e.g. master). I am using the "Generic Webhook Trigger" plugin in Jenkins to do this since couldn't find any other suitable plugins.

And I found I could refer to two variables in the payload.

  • payload.ref == "refs/head/master"
  • payload.commits[1].message matches Merge pull request*

However, in the plugin, it seems that I could set only one filter. Is there any way to set multiple filters?

1

There are 1 best solutions below

0
Pius Raeder On

I solved it by using the regexpFilterExpression.

First you will need to put the branch name for which the push event came for into a variable named ref by adding this to the GenericTrigger class:

genericVariables: [
    [key: 'ref', value: '$.ref']
]

Now we need all the titles from the commits that were in the trigger payload, so add another variable for it

,[key: 'commit_titles', value: '$.commits[*].title']

Then we need to assemble the string on which the filter will be applied on by defining the regexpFilterText property in GenericTrigger class

...
regexpFilterText: 'BRANCH: $ref COMMIT_TITLES: $commit_titles END',
...

Variables will be substituted.

Then it is time to define the actual filter

...
regexpFilterExpression: "BRANCH: refs/heads/master COMMIT_TITLES: .*?(Merge pull request).*? END"
...

Complete example:

properties ([
    pipelineTriggers([
        [$class: 'GenericTrigger',
            genericVariables: [
                [key: 'ref', value: '$.ref'],
                [key: 'commit_titles', value: '$.commits[*].title']
            ],
            token: 'my secret token',
            causeString: 'Triggered because of $commit_titles in branch $ref',
            printContributedVariables: true,
            printPostContent: true,
            silentResponse: false,
            regexpFilterText: 'BRANCH: $ref COMMIT_TITLES: $commit_titles END',
            regexpFilterExpression: "BRANCH: refs/heads/master COMMIT_TITLES .*?(Merge pull request).*? END"
        ]
    ])
])