TeamCity show branch in artifact zip name

2019-05-27 01:30发布

问题:

I have a build in TeamCity where I want to build an artifact zip file.

I can use Edit Configuration Settings => General Settings => Artifact Paths to set up the files to zip and the zip file name.

I want to make the name more descriptive, and TeamCity lets me use parameters, e.g. :

out/mypackagedfiles/** => MyBuild_%build.number%.zip

will give me a zip file like MyBuild_46.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

which gives my_great_feature.zip in a directory called MyBuild_46_feature.

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:

  • create a new parameter that derives the sanitised branch name from vcsroot.branch
  • some kind of string manipulation within the Artifact Paths configuration

== EDIT ==

Based on oryades answer below (which looks like linux bash command line), I converted this to PowerShell for Windows as follows:

  1. 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)
  2. Create a build step with runner type = PowerShell
  3. 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']"
    

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

回答1:

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']"

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