2

I am using Jenkins for a CI/CD. Git repository has a Jenkins hook so it triggers new build on each push, which includes tags.

My build, triggered by tag, produces following output.

    commit notification 282aa9df9bc037df26f82d0aaf7d65e57b3c6c00
    Building in workspace /var/jenkins_home/workspace/testproject
    > git rev-parse --is-inside-work-tree # timeout=10
    Fetching changes from the remote Git repository
    > git config remote.origin.url ssh://GIT-HOST/PRJ/REPO.git # timeout=10
    Cleaning workspace
    > git rev-parse --verify HEAD # timeout=10
    Resetting working tree
    > git reset --hard # timeout=10
    > git clean -fdx # timeout=10
    Fetching upstream changes from ssh://GIT-HOST/PRJ/REPO.git
    > git --version # timeout=10
    > git fetch --tags --progress ssh://GIT-HOST/PRJ/REPO.git +refs/heads/*:refs/remotes/origin/*
(1) > git rev-parse 282aa9df9bc037df26f82d0aaf7d65e57b3c6c00^{commit} # timeout=10
(2) > git branch -a -v --no-abbrev --contains 18dea36c9ada06c1c176b8864bb28166e2bdac3f # timeout=10
    Checking out Revision 18dea36c9ada06c1c176b8864bb28166e2bdac3f (tagtag, origin/tagtag)
    > git checkout -f 18dea36c9ada06c1c176b8864bb28166e2bdac3f

As you can see, build is triggered by 282aa9d (1), which is a tag, but later on, it's translated to commit 18dea36 (2) and from that point only a commit info is available for the user (ex: env variables.) This is a valid commit, but tag info is not available anymore.

How can I know a build has been triggered by a tag, get that tag info and be able to use it in a pipeline?

Edit: Just to clarify - I am looking for an option to have a job that triggers a build on all pushes, but in case of a tag does execute additional steps (or suppress other steps.)

wst
  • 4,040
  • 4
  • 41
  • 59

3 Answers3

1

Seems that's not available using Jenkins alone. There are two possible solutions.

  1. Filter notification on source - Bitbucket, GitHub, etc. might be able to notify Jenkins only in case of certain events, or in case of all but certain events. One of such event may be tag creation.
  2. If above is not an option, write a filtering service that would impersonate Jenkins. Git server would send notification to filtering service, then you could filter it however you like and send (or not) notification to real Jenkins.

Former is trivial but not always available. Latter requires a little bit of work (but not that much, really) and gives a lot of flexibility.

wst
  • 4,040
  • 4
  • 41
  • 59
0

How can I know a build has been triggered by a tag

You could have a job triggered only by tags, as in this setup.

From there, any commit used by the build is one obtained from a tag, which means you can find back the tag from the commit.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for the answer, but this I knew. I will update the question. What I meant is - get one job but run additional steps in case of tag. – wst Mar 26 '18 at 14:42
  • @waste Then the second part of my answer should be relevant: from a commit, you can check if there is a tag. – VonC Mar 26 '18 at 15:18
  • It is relevant only if, as you suggested, build could be triggered by tag push. Look at updated question - I need to trigger the build always. In such case, after pushing new commit to master with tag, Jenkins would trigger two builds. Git-describe would let you know that both builds checkout master commit and tag exists on this commit. How would you differentiate? Or publish on commit push, but only send a notification on tag push? Now, Jenkins knows exactly which objectId started the build. If Jenkins knows - I want to know as well. – wst Mar 26 '18 at 23:56
0

There is a new "when" parameter: buildingTag.

buildingTag - A simple condition that just checks if the Pipeline is running against a tag in SCM, rather than a branch or a specific commit reference.

https://jenkins.io/blog/2018/04/09/whats-in-declarative/

SaundersB
  • 607
  • 2
  • 12
  • 25