TeamCity 将参数从服务器传递到构建代理

TeamCity pass parameter from server to build agent

我想获取构建代理的提交计数以进行手动版本控制,

version = git rev-list --count "branchname"

git 在构建代理上不可用,因为我有结帐 "Automatically on server".

有没有办法将 version 从结帐服务器传递到构建代理? (不更改 VCS checkout mode 以构建代理)?

我是运行最新的9.1.6版本TC

基本上,不,你不能以你想要的方式做你想做的事,你不能在获取更改时只在服务器上执行一些命令行命令。

为什么不正确配置内部版本号格式和内部版本计数器并使用它们呢?也可以在构建过程中动态设置构建号。

我在构建期间做了非常相似的事情来收集当前分支名称和 git 哈希。遗憾的是,不,您不能在构建期间将结帐模式设置为服务器来执行这些 git 命令。您需要更改签出模式以构建代理,以确保 .git 文件夹存在于工作目录中。从好的方面来说,我认为这没有坏处。它不复制遥控器,因此构建代理很难将更改推送到主存储库。

就@hexct 的观点而言,变基或合并或任何数量的事情都可能使此计数不可靠。最好将自己绑定到 git 哈希而不是提交数。

Is there a way to pass version from checkout server to build agent? (without changing VCS checkout mode to build agent)?

简短的回答是你做不到

您可以尝试做的是:

- Add a version file to your repository,   
- **before** commiting use a git hook to update this file with the desired number
- Read the content of the file on your build server and you have it.

- Use a git hook to call a job on your build server which gets the 
  branch name and the number of commits and store it for later use somewhere

重点是既然做不到就要有点创意


示例挂钩可以是:

pre-receive hook

#!/bin/sh

branchName=

# Get the number of commits you need to store:
version = git rev-list --count $branchName

#############
# Now write the desired number to the desired file and let
# the build read it
#############

# Output colors
red='3[0;31m';
green='3[0;32m';
yellow='3[0;33m';
default='3[0;m';

# personal touch :-)
echo "${red}"
echo "                                         "
echo "                   |ZZzzz                "
echo "                   |                     "
echo "                   |                     "
echo "      |ZZzzz      /^\            |ZZzzz  "
echo "      |          |~~~|           |       "
echo "      |        |-     -|        / \      "
echo "     /^\       |[]+    |       |^^^|     "
echo "  |^^^^^^^|    |    +[]|       |   |     "
echo "  |    +[]|/\/\/\/\^/\/\/\/\/|^^^^^^^|   "
echo "  |+[]+   |~~~~~~~~~~~~~~~~~~|    +[]|   "
echo "  |       |  []   /^\   []   |+[]+   |   "
echo "  |   +[]+|  []  || ||  []   |   +[]+|   "
echo "  |[]+    |      || ||       |[]+    |   "
echo "  |_______|------------------|_______|   "
echo "                                         "
echo "${default}"

# set the exit code to 0 so the push will occur
exit 0;

为什么你需要 git 计数器?

最简单的方法是遵循 TeamCity 的原则。使用 TeamCity 计数器而不是 git 计数器。每次提交后设置构建触发器。并将标签设置回 git 以在 git 历史记录中查看构建版本。