lex4089
lex4089

Reputation: 3

Trigger Jenkins pipeline build from Github Tag

I'm trying to trigger a Jenkins Pipeline build (NOT MultiBranch Pipeline) when a specific format of tag is pushed to my GitHub repository. So any branch pushed to the repository will trigger a build if it is tagged with a format of Major.Minor.Patch e.g. 123.123.123

I've setup a webhook which works fine and hits Jenkins (I can see it in the Github Hook Log on Jenkins configuration page). But unfortunately it does not trigger the build.

I've tried setting the refspec to:

+refs/tags/*:refs/remotes/origin/tags/*

And I've accompanied this with a branch identifier:

:origin/tags/[0-9]+\.[0-9]+\.[0-9]+

I have read every article I can find, and scoured StackOverflow but I'm at a loss. I can make it work by setting the branch identifier to **/tags/** but this is too open and triggers a lot of redundant builds.

If anyone can assist in achieving this goal it would be massively appreciated. Also, I'm not certain whether I should be using Pipeline to MultiBranch Pipeline to achieve this?

Starting to lose faith that Jenkins is a good choice so before I jump ship please help!

Thanks!

Upvotes: 0

Views: 1810

Answers (1)

Michael
Michael

Reputation: 181

I prefer using the generic webhook trigger plugin

This allows you to assign a token to a specific Pipeline job so that it will be triggered when your GitHub webhook sends a http request the job's corresponding url:

  • http://JENKINS_URL/generic-webhook-trigger/invoke?token={token-goes-here}

The Github docs describe the http payload content for a push event

You can parse the http payload using a JSONPath expression to obtain the tag string and then filter whether to run the Jenkins job using the Major.Minor.Patch regex

I've not tested it but here's what the pipeline code might look like:

triggers {
        GenericTrigger(
            genericVariables: [
                [key: 'tagString',
                 value: '$.ref',
                 expressionType: 'JSONPath']
            ],
            token: 'example-token',
            printContributedVariables: true,
            printPostContent: true,
            // only trigger if tag follows Major.Minor.Patch regex
            regexpFilterText: '$tagString',
            regexpFilterExpression: '<tag-regex-here>'
        )
}

Upvotes: 1

Related Questions