TeamCity在工件zip名称中显示分支 [英] TeamCity show branch in artifact zip name
问题描述
我在TeamCity中进行构建,我想在其中构建工件zip文件。
I have a build in TeamCity where I want to build an artifact zip file.
我可以使用编辑配置设置=>常规设置=> Artifact Paths
设置要压缩的文件和zip文件名。
I can use Edit Configuration Settings => General Settings => Artifact Paths
to set up the files to zip and the zip file name.
我想让名称更具描述性,TeamCity让我使用参数,例如:
I want to make the name more descriptive, and TeamCity lets me use parameters, e.g. :
out/mypackagedfiles/** => MyBuild_%build.number%.zip
会给我一个的zip文件MyBuild_46.zip
。
我还希望包含构建所基于的分支。也可以作为TeamCity参数使用,但它包含一个正斜杠(例如 feature / my_great_feature
)。因此,如果在 Artifact Paths
配置中使用此文件,则会得到一个包含zip文件的目录:
I'd also like to include the branch that the build was built from. This is also available as a TeamCity parameter, but it contains a forward slash (e.g. feature/my_great_feature
). So if I use this in the Artifact Paths
configuration, I get a directory containing a zip file:
out/mypackagedfiles/** => MyBuild_%build.number%_%vcsroot.branch%.zip
给出 my_great_feature.zip
在名为 MyBuild_46_feature
的目录中。
我想要的要做的就是以某种方式从分支名称中删除/替换正斜杠以获取一个zip文件,例如 MyBuild_46_feature_my_great_feature.zip
。
What I'd like to do is somehow remove/replace the forward slash from the branch name to get one zip file, such as MyBuild_46_feature_my_great_feature.zip
.
只要分支名称是可识别的,我就不必担心确切的格式。
I'm not to worried about the exact format - as long as the branch name is identifiable.
可能存在的想法-但我不能尚未找到:
Ideas for what might exist - but I can't find yet:
- 创建一个新参数,该参数从
vcsroot.branch $ c $派生经过清理的分支名称。 c>
- 在
Artifact Paths
配置中的某种字符串操作
- create a new parameter that derives the sanitised branch name from
vcsroot.branch
- some kind of string manipulation within the
Artifact Paths
configuration
==编辑==
基于以下oryades答案(类似于 linux bash 命令行),我将其转换为 PowerShell for Windows ,如下所示:
Based on oryades answer below (which looks like linux bash command line), I converted this to PowerShell for Windows as follows:
- 设置工件以使用%env.branch_name%(这也需要在参数选项卡中设置一个临时值/虚拟值,以使构建运行)
- 创建运行器类型为PowerShell的构建步骤
-
将脚本设置为源代码并粘贴以下内容:
- Set up artifacts to use %env.branch_name% (this also requires setting a temporary/dummy value in the Parameters tab for the build to run)
- Create a build step with runner type = PowerShell
Set script to Source Code and paste in the following:
$branch_name="%vcsroot.branch%".replace('/','_')
echo "Sanitised branch for Artifact zip = " $branch_name
echo "##teamcity[setParameter name='env.branch_name' value='$branch_name']"
请注意,TeamCity允许您替换%vcsroot.branch%-这是在PowerShell运行之前发生的替换。感谢 Team City and Power Shell 提供的提示
Note that TeamCity lets you do substitution of %vcsroot.branch% - this is a substitution that happens before PowerShell runs. Thanks to Team City and Power Shell for this tip
推荐答案
您可以在构建期间创建env变量,只需创建包含以下内容的构建步骤即可:
You can create env variable during build, just create a build step with the following content:
branch_name=$(echo %vcsroot.branch% | sed 's/\//_/')
echo "##teamcity[setParameter name='env.branch_name' value='$branch_name']"
在接下来的构建步骤中,您可以将%env.branch_name%用作具有所需值的变量。
On next build steps you can use %env.branch_name% as a variable with required value.
https://confluence.jetbrains.com/display/TCD10/Build+Script+Interaction+with+TeamCity
这篇关于TeamCity在工件zip名称中显示分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!