如何在Teamcity中只有特定文件更改时不触发构建

6
我正在使用Teamcity作为git仓库的构建管理器;目前,创建新构建的触发器是有新更改时。
问题在于,在构建时,我运行的脚本会创建一个特定文件version.txt的新提交(我在那里增加了数字)。
尽管成功完成,但提交被视为新更改,并且在下一次运行时,即使没有进行其他提交,也将触发夜间构建。
我想能够创建一个规则,如果唯一的待处理更改是Version.txt文件,则不会触发构建。
有没有办法做到这一点?
编辑: 我已经向Team City添加了一个条件规则:如果version.txt文件已更改,则不要触发构建(请参见屏幕截图)。但是,似乎这条规则会导致如果有2个待处理更改,其中一个是version.txt文件,则不会触发构建。

enter image description here

提前感谢。

1个回答

4

是的,你可以做到。


在执行构建之前更新版本文件version.txt

使用git hook来捕获提交,然后你就可以更新构建并提交,这样你就会有两个提交,一个是原始的提交,另一个是版本更新,构建就会执行这两个提交。


Git hooks

对于被提交的文件,在提交钩子中检查它们。 如果只有版本文件则跳过构建。

这里有一个例子post-merge钩子,当用户将代码推送到远程分支后运行。

#!/bin/sh

# Check to see if this is the first commit in the repository or not
if git rev-parse --verify HEAD >/dev/null 2>&1
then
    # We compare our changes against the prevoius commit
    against=HEAD^
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# Redirect output to screen.
exec 1>&2

# Check to see if we have updated the version.txt file
if [ $(git diff-tree -r --name-only $against | grep version.txt ) ];
then

    # Output colors
    red='\033[0;31m';
    green='\033[0;32m';
    yellow='\033[0;33m';
    default='\033[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 "                                         "
    echo "      ${green}You have just commited code ${red}   " 
    echo "         Your code ${yellow}is bad.!!!  ${red} Do not ever commit again       "
    echo "                                         "
    echo "${no_color}"
#fi;

exit 0;

使用smudge更新版本

当将代码添加到索引(git add)时,版本增量将会被执行。 enter image description here


感谢codeWizard提供详细的答案!但是,此时此刻,我想知道是否有一种解决方案,不需要改变脚本与git仓库交互的行为,而是一个与Teamcity相关的解决触发规则问题的方案。 - Imaybewrong
不需要改变脚本与git仓库交互的行为,你可以使用服务器端钩子,这与你本地仓库无关。 - CodeWizard

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接