如何在持续集成构建服务器上使用git

Rob*_*Rob 10 git continuous-integration

我们有一个构建服务器,用于从git签出代码版本并构建它.通常,服务器将签出并构建develop分支,但它可以由GUI控制以执行任何特定分支或标记的构建.

我们的git存档很大,所以我们只想执行git clone一次.所以我的问题是:应该发出什么样的git命令序列,以便使当前工作目录与远程git档案保持同步.

我最初的天真的尝试刚刚执行git checkout <branch>之后 git pull.但是这没有考虑到需要删除的先前构建所创建的所有工件以及构建过程所做的一些自动代码修改,例如汇编文件中的版本号.

所以我认为我们需要的是命令序列

  1. 摆脱对本地目录的任何修改
  2. 更新本地存储库以包括来自远程服务器的所有提交
  3. 签出指定的分支或标记

请记住,本地存储库中可能尚未知道指定的分支或标记.例如,如果release/xxx在远程服务器上创建了新分支,则不会事先知道本地构建计算机.这是我天真的方法偶然发现的另一个问题.

最后,git服务器可能偶尔会更正它的历史记录.我确信这将是一个罕见的事件,但如果集成服务器在历史重写后不需要任何调整,那将是可取的.

非常感谢

Rob*_*Rob 8

这是我们目前使用的机制。在git clone执行只有一次作为安装程序的一部分,然后将下面是每个构建完成。

为清楚起见,我删除了错误处理。


# Undo any modifications made to the working tree by the build process

git reset --hard

# Remove any untracked build artifacts created by the build process

git clean -fdx

# Fetch all commits and new branches/tags from the git server.

git fetch

# Set the index and working directory to the required tag or branch.
# Notes:
# * If the last build was for the same branch, then this will not
#   update the working directory.
# * The %TAG_OR_BRANCH% variable is passed in via the GUI.  This will
#   be in the form of a branch name (develop|release/vX.X.X) or a tag (vX.X.X)

git checkout %TAG_OR_BRANCH%

# Bring the index and working tree up-to-date.
# Notes:
# * the --ff-only flag is probably redundant, but we want to make it
#   clear that no merging is expected.

git pull --ff-only
Run Code Online (Sandbox Code Playgroud)

我们在开发人员机器上创建了新的标签/分支,将它们推送到服务器,这个脚本正确地获取它们并构建结果。我们尝试将origin/前缀添加到分支或标签名称,但这种方法对我们不起作用(无法识别标签名称)。

我认为此脚本与@Yasser 建议的更简单的答案之间的主要区别在于 git HEAD指向正确的位置,并且该命令git status给出了合理的答案。这是否重要 - 我不确定。


yel*_*yed 5

假设你.gitignore正确设置,你应该能够做到

git fetch
git reset --hard origin/branch
git clean -xfd
Run Code Online (Sandbox Code Playgroud)

这应该产生一个干净的构建.