简短版本
当触发构建管道时,构建管道任务之一将获取最新文件,并且必须在当前运行管道的 git 存储库中添加/更新该文件。我尝试使用命令行任务来完成此操作,但它失败了,因为不是 git 存储库
fatal: not a git repository (or any of the parent directories): .git
长版
我尝试实现Dynamics 365实例的解决方案打包和解包过程。所以构建管道有以下任务
从 Dynamics 365 导出解决方案并将其存储在 $(build.binariesdirectory)
解压解决方案 zip 文件并存储在 $(Build.Repository.LocalPath) 中,即添加/更新现有文件
命令行任务提交文件并将其推送到当前管道存储库
ECHO "Setting git config..."
git config --global user.email "[email protected]"
git config --global user.name "Admin"
ECHO "CHECK GIT STATUS..."
git status
ECHO "GIT ADD..."
git add -A
ECHO "CHECK GIT STATUS..."
git status
ECHO "Commiting the changes..."
git commit -m "Latest Customizations updated"
ECHO "Pushing the changes..."
git push -u origin master
ECHO "Customization Committed Successfully"
已更新
如果使用 2 个命令行任务创建管道,即一个在
Build.Sourcedirectory
中创建随机文件,另一个用于提交更改,则 git commit 和 push 命令正在工作。
但是如果我们在命令行任务之前添加其他任务(例如在
Build.Sourcedirectory
目录中解压缩文件),则会收到错误,因为不是存储库。
如果您看到下面的屏幕截图,则在签出阶段 git 存储库已克隆到
D:\a\1\s
下面的屏幕截图来自命令行任务,其中当前工作目录与签出任务相同(即
D:\a\1\s
),我们可以看到“.git”文件夹存在,因此当前目录中有本地存储库,但仍然出现错误 fatal: not a git repository
我在这里遗漏了什么吗?
默认情况下,源文件会签出到
Build.SourcesDirectory
(例如:目录:D:\a\1\s
),它可以被视为临时git存储库。
根据错误消息,似乎命令行任务的工作目录不在
Build.SourcesDirectory
下,并且您没有再次git checkout
到该工作目录。
因此,请尝试解压解决方案 zip 文件并存储在
Build.SourcesDirectory
中,然后在命令行下运行以推送提交(它对我有用):
ECHO "Setting git config..."
git config --global user.email "[email protected]"
git config --global user.name "Admin"
ECHO "CHECK GIT STATUS..."
git status
git checkout -b master
ECHO "GIT ADD..."
git add -A
ECHO "CHECK GIT STATUS..."
git status
ECHO "Commiting the changes..."
git commit -m "Latest Customizations updated"
ECHO "Pushing the changes..."
git push -u origin master
ECHO "Customization Committed Successfully"
请注意,您需要向构建服务授予版本控制权限并启用允许脚本访问系统令牌。
我知道这已经好几年了,但对于任何遇到此问题的人来说,是因为当您解压
$(Build.Sourcedirectory)
的解决方案时,它会覆盖存储库的根目录并导致 fatal: not a git repository
问题。
要解决此问题,请将解决方案解压到子文件夹,例如
$(Build.SourcesDirectory)\$(solutionName)
,其中 $(solutionName)
是管道变量。