触发Jenkins建立在新标签和&承诺

时间:2016-11-25 16:31:50

标签: git github jenkins jenkins-pipeline

我们正在使用Git插件:https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin

目前,通过webhooks,只要将更改推送到GitHub,我们就会启动Jenkins构建。现在,我们希望在添加新标记时触发相同的构建。所以我们有两个触发条件:

  1. 代码更改被推送到GitHub
  2. 创建标记
  3. 如果我们在此线程中尝试修复提及,则构建仅针对标记启动。 jenkins trigger build if new tag is released

    我们怎样才能在两种情况下都这样做?

    问题#02:如何在Jenkins构建中获取标记名称,是否有任何环境变量。

2 个答案:

答案 0 :(得分:2)

4步骤:

  1. 提交代码:git commit -m "Some meaningful message"
  2. 创建标记
    • 要发布到舞台:git tag -a release_stage_<meaningful tag>
    • 要发布到prod:git tag -a release_production_<meaningful tag>
  3. 推送标签推送标签:git push origin release_stage_<same_meaningful_tag>
  4. 推送提交 git push origin <branch_name>
  5. Jenkins文件:

    stage('Checkout Project') 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
                }
    }
    

    ```

答案 1 :(得分:0)

尝试使用Jenkins GitHub Plugin

对于我们标记创建和更改推送都非常有效。

对于可以使用 $ {GIT_BRANCH} 环境变量的标记,它应包含 origin / tags /%tag%格式的标记