简体   繁体   English

Jenkins Git 插件:如何建立特定标签?

[英]Jenkins Git Plugin: How to build specific tag?

I am having issues getting Jenkins to build a specified tag.我在获取 Jenkins 以构建指定标签时遇到问题。 The tag is part of a parametrized build, but I do not know how to pass this through to the git plugin to just build that tag.该标签是参数化构建的一部分,但我不知道如何将其传递给 git 插件以仅构建该标签。 This has been taking 3 hours of my day and I have conceded defeat to the masters at Stack Overflow.这已经占用了我一天的 3 个小时,我已经在 Stack Overflow 上向大师们认输了。

I was able to do that by using the "branches to build" parameter: 我能够通过使用“branches to build”参数来做到这一点:

Branch Specifier (blank for default): tags/[tag-name]

Replace [tag-name] by the name of your tag. 将[tag-name]替换为标记的名称。

None of these answers were sufficient for me, using Jenkins CI v.1.555, Git Client plugin v.1.6.4, and Git plugin 2.0.4. 使用Jenkins CI v.1.555,Git Client插件v.1.6.4和Git插件2.0.4,这些答案都不适合我。

I wanted a job to build for one Git repository for one specific, fixed (ie, non-parameterized) tag. 我想要为一个特定的,固定的(即非参数化)标签构建一个Git存储库。 I had to cobble together a solution from the various answers plus the "build a Git tag" blog post cited by Thilo . 我不得不拼凑出各种答案的解决方案以及Thilo引用“构建Git标签”博客文章

  1. Make sure you push your tag to the remote repository with git push --tags 确保使用git push --tags将标记git push --tags远程存储库
  2. In the "Git Repository" section of your job, under the "Source Code Management" heading, click "Advanced". 在您的工作的“Git存储库”部分中,在“源代码管理”标题下,单击“高级”。
  3. In the field for Refspec, add the following text: +refs/tags/*:refs/remotes/origin/tags/* 在Refspec的字段中,添加以下文本: +refs/tags/*:refs/remotes/origin/tags/*
  4. Under "Branches to build", "Branch specifier", put */tags/<TAG_TO_BUILD> (replacing <TAG_TO_BUILD> with your actual tag name). 在“要构建的分支”,“分支说明符”下,放置*/tags/<TAG_TO_BUILD> (将<TAG_TO_BUILD>替换为您的实际标记名称)。

Adding the Refspec for me turned out to be critical. 为我添加Refspec证明是至关重要的。 Although it seemed the git repositories were fetching all the remote information by default when I left it blank, the Git plugin would nevertheless completely fail to find my tag. 虽然看起来git存储库在我将其留空时默认获取所有远程信息,但Git插件仍然完全无法找到我的标签。 Only when I explicitly specified "get the remote tags" in the Refspec field was the Git plugin able to identify and build from my tag. 只有当我在Refspec字段中明确指定“获取远程标签”时,Git插件才能从我的标签中识别和构建。

Update 2014-5-7 : Unfortunately, this solution does come with an undesirable side-effect for Jenkins CI (v.1.555) and the Git repository push notification mechanism à la Stash Webhook to Jenkins : any time any branch on the repository is updated in a push, the tag build jobs will also fire again. 更新2014-5-7 :不幸的是,这个解决方案确实带来了Jenkins CI(v.1.555)和Git存储库推送通知机制( Stash Webhook到Jenkins )的不良副作用:任何时候存储库上的任何分支都会更新在推送中,标签构建作业也将再次触发。 This leads to a lot of unnecessary re-builds of the same tag jobs over and over again. 这导致一遍又一遍地重复相同标签作业的大量不必要的重建。 I have tried configuring the jobs both with and without the "Force polling using workspace" option, and it seemed to have no effect. 我尝试使用和不使用“强制轮询使用工作区”选项配置作业,它似乎没有任何效果。 The only way I could prevent Jenkins from making the unnecessary builds for the tag jobs is to clear the Refspec field (ie, delete the +refs/tags/*:refs/remotes/origin/tags/* ). 我可以阻止Jenkins为标记作业进行不必要的构建的唯一方法是清除Refspec字段(即删除+refs/tags/*:refs/remotes/origin/tags/* )。

If anyone finds a more elegant solution, please edit this answer with an update. 如果有人找到更优雅的解决方案,请通过更新编辑此答案。 I suspect, for example, that maybe this wouldn't happen if the refspec specifically was +refs/tags/<TAG TO BUILD>:refs/remotes/origin/tags/<TAG TO BUILD> rather than the asterisk catch-all. 我怀疑,例如,如果refspec具体是+refs/tags/<TAG TO BUILD>:refs/remotes/origin/tags/<TAG TO BUILD>而不是星号全能,那么这可能不会发生。 For now, however, this solution is working for us, we just remove the extra Refspec after the job succeeds. 但是现在,这个解决方案对我们有用,我们只需在作业成功后删除额外的Refspec。

Can't you tell Jenkins to build from a Ref name? 难道你不能告诉Jenkins从Ref名称构建? If so then it's 如果是的话,那就是

refs/tags/tag-name

From all the questions I see about Jenkins and Hudson, I'd suggest switching to TeamCity. 从我看到的关于Jenkins和Hudson的所有问题,我建议切换到TeamCity。 I haven't had to edit any configuration files to get TeamCity to work. 我没有必要编辑任何配置文件以使TeamCity工作。

I did something like this and it worked : 我做了类似这样的事情并且有效:

Source Code Management

 Git    
    Repositories    


 Advance

Name: ref
Refspec : +refs/tags/*:refs/remotes/origin/tags/* 

 Branches to build  
 Branch Specifier (blank for 'any') : v0.9.5.2

在此输入图像描述

Jenkins log confirmed that it was getting the source from the tag Jenkins日志证实它正在从标签中获取源代码

Checking out Revision 0b4d6e810546663e931cccb45640583b596c24b9 (v0.9.5.2) 检查修订版0b4d6e810546663e931cccb45640583b596c24b9 (v0.9.5.2)

If you are using Jenkins pipelines and want to checkout a specific tag (eg: a TAG parameter of your build), here is what you can do: 如果您正在使用Jenkins管道并想要签出特定标记(例如:构建的TAG参数),您可以执行以下操作:

stage('Checkout') {
  steps {
    checkout scm: [$class: 'GitSCM', userRemoteConfigs: [[url: 'YOUR_GIT_REPO_URL.git', credentialsId: 'YOUR_GIT_CREDENTIALS_ID' ]], branches: [[name: 'refs/tags/${TAG}']]], poll: false
  }
}

I set the Advanced->Refspec field to refs/tags/[your tag name] . 我将Advanced-> Refspec字段设置为refs/tags/[your tag name] This seems simpler than the various other suggestions for Refspec, but it worked just fine for me. 这似乎比Refspec的其他各种建议更简单,但它对我来说效果很好。

UPDATE 23/7/2014 - Actually, after further testing, it turns out this didn't work as expected. 更新23/7/2014 - 实际上,经过进一步的测试,结果证明这没有按预期工作。 It appears that the HEAD version was still being checked out. 似乎HEAD版本仍在检查中。 Please undo this as the accepted answer. 请将此撤消作为已接受的答案。 I ended up getting a working solution by following the post from gotgenes in this thread (30th March). 我最终得到了一个有效的解决方案,在这个帖子 (3月30日)中关注了来自gotgenes的帖子。 The issue mentioned in that post of unnecessary triggering of builds was not an issue for me, as my job is triggered from an upstream job, not from polling SCM. 在那篇关于不必要的构建触发的帖子中提到的问题对我来说不是问题,因为我的工作是从上游工作触发而不是从轮询SCM触发。

UPDATE APR-2018 - Note in the comments that this does work for one person, and agrees with Jenkins documentation. 更新APR-2018 - 注释中注明这对一个人有用,并且同意Jenkins文档。

In a latest Jenkins (1.639 and above) you can: 在最新的Jenkins(1.639及以上)中,您可以:

  1. just specify name of tag in a field 'Branches to build'. 只需在“要构建的分支”字段中指定标记的名称。
  2. in a parametrized build you can use parameter as variable in a same field 'Branches to build' ie ${Branch_to_build}. 在参数化构建中,您可以在同一字段“Branches to build”中使用参数作为变量,即$ {Branch_to_build}。
  3. you can install Git Parameter Plugin which will provide to you functionality by listing of all available branches and tags. 你可以安装Git参数插件 ,它将通过列出所有可用的分支和标签为你提供功能。

I was able to get Jenkins to build a tag by setting the Refspec and Branch Specifier as detailed in this blog post . 通过设置Refspec和Branch Specifier,我能够让Jenkins构建一个标签, 详见本博文

I also had to set the Repository Name (to "origin" in my case) so that I could reference it in the Refspec (otherwise it would apparently use a randomly generated name). 我还必须设置存储库名称(在我的情况下为“origin”),以便我可以在Refspec中引用它(否则它显然会使用随机生成的名称)。

What I did in the end was: 我最后做的是:

  • created a new branch jenkins-target , and got jenkins to track that 创建了一个新的分支jenkins-target ,并让jenkins跟踪它
  • merge from whichever branch or tag I want to build onto the jenkins-target 从我想要构建的jenkins-target上的任何分支或标记合并
  • once the build was working, tests passing etc, just simply create a tag from the jenkins-target branch 一旦构建工作,测试传递等,只需从jenkins-target分支创建一个标签

I'm not sure if this will work for everyone, my project was quite small, not too many tags and stuff, but it's dead easy to do, dont have to mess around with refspecs and parameters and stuff :-) 我不确定这是否适用于所有人,我的项目非常小,没有太多的标签和东西,但它很容易做,不必乱搞refspecs和参数和东西:-)

You can build even a tag type, for example 1.2.3-alpha43 , using wildcards: 您甚至可以使用通配符构建标记类型,例如1.2.3-alpha43

Refspec: +refs/tags/*:refs/remotes/origin/tags/* Refspec: +refs/tags/*:refs/remotes/origin/tags/*

Branch specifier: origin/tags/1.2.3-alpha* 分支说明符: origin/tags/1.2.3-alpha*

You can also tick " Build when a change is pushed to GitHub " to trigger the push, but you have to add "create" action to the webhook 您还可以勾选“ 将更改推送到GitHub时构建 ”以触发推送,但您必须向webhook添加“创建”操作

Adding my two cents here since I have not seen an answer that uses the option "Build with parameters" in Jenkins. 因为我没有看到在Jenkins中使用“使用参数构建”选项的答案,所以在这里加上我的两分钱。

Here I am using Jenkins CI browser console for project starwars_api and I was able to build directly with "Build with parameters" with value refs/tags/tag-name 在这里,我使用Jenkins CI浏览器控制台进行项目starwars_api,我能够使用值refs / tags / tag-name直接构建“使用参数构建”

  1. choose the "build with parameters" option. 选择“使用参数构建”选项。
  2. add value in the box as "refs/tags/tag_142" (tag_name = tag_142 for my example) 在框中添加值“refs / tags / tag_142”(对于我的示例,tag_name = tag_142)

build with ref tag name 用ref标签名称构建

I found the best solution for this issue, You should use the GitSCM class instead of git class:我找到了这个问题的最佳解决方案,您应该使用 GitSCM 类而不是 git 类:

stages {
        stage('SCM') {
            steps {
                script {
                    wrap([$class: 'BuildUser']) {
                        currentBuild.displayName = "#${BUILD_NUMBER} - ${env.BUILD_USER} --> Build from branch ${BRANCH}"
                        slackSend channel: "#jenkins-build", color: "#02fe21", message: "${JOB_NAME} started by ${env.BUILD_USER} : #${BUILD_NUMBER} --> Build from ${BRANCH} branch with ${TAG} TAG"
                    }
                }
                checkout(
                        changelog: false, poll: false, scm: [
                        $class           : 'GitSCM',
                        branches         : [
                                [name: "$BRANCH"],
                        ],
                        userRemoteConfigs: [
                                [
                                        url: "<YOUR-GIT-URL>/${REPOSITORY}.git", credentialsId: '<YOUR-CREDENTIAL>'
                                ],
                        ],
                ])
            }
        }

For git-tag "v1.5.4" I used the follow regex对于 git-tag "v1.5.4" 我使用了以下正则表达式

:(. v[\d.]{1,15}. ) :(. v[\d.]{1,15}. )

詹基斯配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM