10
votes

Description

I have Jenkins (2.126) set up to build a pipeline when a new tag is pushed. Specifically, I want to tag an existing commit on master as release-3 and have it trigger a build. According to several sources, what I want is possible:

https://mohamicorp.atlassian.net/wiki/spaces/DOC/pages/136740885/Triggering+Jenkins+Based+on+New+Tags

Unfortunately, nothing happens when I tag an existing commit and push it to the Git repository. Other builds (triggered by branches) work as expected, and the tag-triggered build in question completes successfully when started manually.

According to some, tag-triggered pipelines should work since version 2.3. Is there anything I can configure to make it work?

Screenshots of attempts

Attempt with a normal pipeline polling at * * * * *:

enter image description here

Attempt with a multibranch pipeline, scanning every 1 minute:

enter image description here

Related issues

I found the following issues related to the problem, which is supposed to be resolved.

1
What kind of pipeline are you using? Is it a multibranch or a "normal standalone" pipe? Recently the Jenkins blog added a post for this - jenkins.io/blog/2018/05/16/pipelines-with-git-tags. It works fine in multibranch pipelines, however I haven't tried it a normal pipeline job. Also, could you please share your pipeline?tftd
@tftd My setup uses a normal pipeline, set up exactly like the picture on the provided link. Full sources available at github.com/Oduig/effective-ci-with-microservices. I'd think the Jenkinsfile does not matter, since the build trigger occurs before a file is even read.Jodiug
Hmm.. maybe you need to enable the Branch or tag creation event in github's webhook?tftd
@tftd I suppose I could use githooks, but I'd prefer if this worked regardless of the type of repository. We use different servers for different kinds of projects (GitLab at work, BitBucket for private things, GitHub for OS). I realize I'm making it difficult, but the question is intentionally generic: can I configure this type of behavior in Jenkins without resorting to third party tools? To me, it seems unlikely that this is supported for multi-branch pipelines but not for single-branch pipelines.Jodiug
Also, keep in mind that when you are using a normal pipeline (i.e. "New job -> Pipeline"), the settings you enter for the SCM are actually telling Jenkins how and from where to retrieve the Jenkinsfile to build the project. This is entirely different thing from the settings in the multibranch pipelines (or a normal jenkins job) where you'd set Branch sources or Source Code Management. This might be part of the reason why it doesn't work for "normal pipeline".tftd

1 Answers

0
votes

You can use Generic Webhook Trigger Plugin.

In GitHub:

  1. Set a webhook in GitHub to invoke JENKINS_URL/generic-webhook-trigger/invoke?some-token-here

In Jenkins:

  1. Create a job and enable the Generic Trigger under "Triggers"
  2. Configure a variable named ref with JSONPath $.ref
  3. Set the filter text to $ref
  4. Set the filter regexp to ^(refs/tags/.+)$
  5. Set the token to some-token-here

Now when a tag is pushed to the repo this job will trigger.

You may also want to pick other values from the webhook like $.repository.ssh_url to get the SSH clone URL.