7
votes

We are using the Git Plugin : https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin

Currently, via webhooks we start a Jenkins build whenever a change is pushed to GitHub. Now we want to trigger the same build when a new tag is added. So we have two triggering conditions :

  1. A code change is pushed to GitHub
  2. A tag is created

If we try the fix mention in this thread then the builds start only for tags. jenkins trigger build if new tag is released

How can we do it for both scenarios ?

Question # 02 : How can we get the tag name inside a Jenkins build, is there any environment variable for it.

2
did you ever find an answer to this?dim_user
@SaulCruz : I am not working on this project anymore and have hazy memory, but I think at the time we didn't find a good solution for it. One solution that I thought of was to start two seperate jobs : one triggers for git commits and the other for tags, and they trigger a common second job. Pipeline plugin can be helpful with that. Hope this helps!Beenish Khan
Thanks, that's basically what I'm doing now, will see if I can find something bettter :) cheersdim_user

2 Answers

3
votes

4 Step Process:

  1. Commit Code : git commit -m "Some meaningful message"
  2. Create a tag
    • To release to stage : git tag -a release_stage_<meaningful tag>
    • To release to prod : git tag -a release_production_<meaningful tag>
  3. Push the tag : git push origin release_stage_<same_meaningful_tag>
  4. Push Commit git push origin <branch_name>

Jenkins File:

        properties([pipelineTriggers([[$class: 'GitHubPushTrigger']])])
        checkout scm
        git_branch = env.BRANCH_NAME
        git_branch_to_release = env.BRANCH_NAME
        git_tag = sh returnStdout: true, script: 'git tag -l --points-at HEAD'```

`//And now you can use to do anything with tags`

if(currentBuild.result=='SUCCESSFUL' || currentBuild.result=='SUCCESS' || currentBuild.result == null)
        {
            if (git_tag.contains('release_stage') || git_tag.contains('release_production'))
            {
            // Do anything which you want to do with tags
            }
}

0
votes

Try using Jenkins GitHub Plugin

It works perfectly well for us for both tag created and changes pushed.

For tags you can use ${GIT_BRANCH} environment variable, it should contain the tag in origin/tags/%tag% format