12

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.

Jodiug
  • 5,425
  • 6
  • 32
  • 48
  • 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 - https://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 Jun 12 '18 at 11:41
  • @tftd My setup uses a normal pipeline, set up exactly like the picture on the provided link. Full sources available at https://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 Jun 12 '18 at 13:33
  • Hmm.. maybe you need to enable the `Branch or tag creation` event in github's webhook? – tftd Jun 12 '18 at 14:01
  • @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 Jun 12 '18 at 14:29
  • It doesn't matter what type of repository you're using - what matters is what job type you are using. With "normal pipelines" you don't have all of the features you normally have with "multibranch pipelines". With multibranch pipelines (based on your job configuration) Jenkins will automatically discover your repository branches, PRs and tags and build them upon push events. It doesn't matter if your repository is in github/gitlab or bitbucket. I'm using it with bitbucket and github repos without problems. You only need to setup your repository webhook properly and you're pretty much done. – tftd Jun 12 '18 at 14:59
  • 1
    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 Jun 12 '18 at 15:01
  • Can you post an image of your source code management configuration? Have you tried using `*/tags/*` in the branch specifier? You are using annotated tags, not lightweight tags, correct? – Mike Frank Jun 19 '18 at 03:51
  • @tftd I tried setting up a multibranch pipeline. Unfortunately, there are three problems: 1) the polling mechanism behind multibranch pipelines does not seem to work (https://issues.jenkins-ci.org/browse/JENKINS-47077), 2) Bitbucket webhooks are not supported (https://issues.jenkins-ci.org/browse/JENKINS-47891) and 3) even if I trigger a build manually, pushing a tag does not cause a build to start. I attached a screenshot of my attempt. – Jodiug Jul 04 '18 at 20:28
  • @Mike Frank I'll add a screenshot. I have tried more allowing branch specifiers, but it did not work. I'm using annotated tags (pushed from Sourcetree) – Jodiug Jul 04 '18 at 20:29
  • I would do it via a git hook - see http://kohsuke.org/2011/12/01/polling-must-die-triggering-jenkins-builds-from-a-git-hook/ – PowerStat Oct 05 '18 at 11:47
  • Is this a duplicate of https://stackoverflow.com/questions/29742847/jenkins-trigger-build-if-new-tag-is-released or would that solution work for you? – Eva Brigid Nov 09 '18 at 20:55
  • Is this a duplicate of https://stackoverflow.com/questions/29742847/jenkins-trigger-build-if-new-tag-is-released or would that solution work for you? – Eva Brigid Nov 09 '18 at 20:56
  • @EvaBrigid It is not a duplicate, because the accepted answer does not work for existing commits. To quote one of the comments in that thread: "[...] works half. It works if I tag a version to the latest revision not if I tag an older revision. I want that jenkins starts building if a new tag is tagged on any revision." – Jodiug Nov 12 '18 at 09:25
  • Same problem with you. @Jodiug – TangHongWan Apr 13 '19 at 10:52
  • @Jodiug have you successfully configured to trigger job when creating tag on remote master branch? How do you do that in normal pipeline job? Thanks. – TangHongWan Apr 13 '19 at 10:57
  • @PageNotFound I have not found a solution yet. – Jodiug Apr 15 '19 at 05:15

1 Answers1

1

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.

Tomas Bjerre
  • 3,270
  • 22
  • 27
  • Thanks for the suggestion, this would work. That said, it doesn't translate easily across multiple kinds of repositories. In my case, Bitbucket webhooks are not supported yet. (See https://issues.jenkins-ci.org/browse/JENKINS-47891) – Jodiug Sep 02 '18 at 13:50