如何 运行 git 命令在 .net 解决方案中构建管道 gitlab
How to run git commands in .net solution build pipeline in gitlab
我有一个依赖于另一个存储库的 .net 存储库,所以我将另一个存储库用作子模块。
我正在使用这个 docker 图像来构建存储库
image: mcr.microsoft.com/dotnet/framework/sdk
但是要使用子模块,我必须获取最新的子模块,这可以使用 git 这样的命令来完成
before_script:
- git submodule sync --recursive
- git submodule update --init --recursive
但是 git 命令在 docker 图像中不可用,因此它无法显示 git is not recognized
任何人都可以帮助我应该做什么才能使 git 可用?
我尝试了很多方法,例如将构建分为两个阶段,并尝试为 git 使用不同的 docker 图像等,但没有任何东西完全符合我的要求。
更新
这是 .gitlab-ci.yml
文件,包括 jmaitrehenry
在他的回答中建议的解决方案
stages:
- build
variables:
GIT_SUBMODULE_STRATEGY: recursive
before_script:
- docker build -t git-for-windows-issue
# Build
Build:
image: mcr.microsoft.com/dotnet/framework/sdk
stage: build
script:
- echo Ok
only:
- branches
tags:
- windows-runner
environment:
name: development
但这也给出了这个错误
docker : The term 'docker' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and
try again.
如果您的 gitlab runner 不是太旧 (v1.10+),您可以将其添加到您的管道中:
variables:
GIT_SUBMODULE_STRATEGY: recursive
这样,gitlab 将在 clone/prepare 项目时为您进行同步和更新。
如果不能,则需要在映像中安装 git。您可以查看此回购协议以帮助您 windows 上的 git:https://github.com/StefanScherer/dockerfiles-windows/tree/master/git-for-windows-issue
您可以将其添加到 before_script:
before_script:
- Invoke-WebRequest 'https://github.com/git-for-windows/git/releases/download/v2.15.1.windows.2/MinGit-2.15.1.2-busybox-64-bit.zip' -OutFile 'mingit.zip' -UseBasicParsing
- Expand-Archive mingit.zip -DestinationPath c:\mingit ; \
- Remove-Item mingit.zip -Force ; \
- c:\mingit\cmd\git.exe submodule sync --recursive
- c:\mingit\cmd\git.exe submodule update --init --recursive
Gitlab 文档:https://docs.gitlab.com/ee/ci/git_submodules.html
如何在 windows 上安装 git:
您可以使用 cake 脚本来处理其他 .net 构建过程,并且您可以创建可重用的任务
https://cakebuild.net/dsl/git/
我有一个依赖于另一个存储库的 .net 存储库,所以我将另一个存储库用作子模块。
我正在使用这个 docker 图像来构建存储库
image: mcr.microsoft.com/dotnet/framework/sdk
但是要使用子模块,我必须获取最新的子模块,这可以使用 git 这样的命令来完成
before_script:
- git submodule sync --recursive
- git submodule update --init --recursive
但是 git 命令在 docker 图像中不可用,因此它无法显示 git is not recognized
任何人都可以帮助我应该做什么才能使 git 可用?
我尝试了很多方法,例如将构建分为两个阶段,并尝试为 git 使用不同的 docker 图像等,但没有任何东西完全符合我的要求。
更新
这是 .gitlab-ci.yml
文件,包括 jmaitrehenry
在他的回答中建议的解决方案
stages:
- build
variables:
GIT_SUBMODULE_STRATEGY: recursive
before_script:
- docker build -t git-for-windows-issue
# Build
Build:
image: mcr.microsoft.com/dotnet/framework/sdk
stage: build
script:
- echo Ok
only:
- branches
tags:
- windows-runner
environment:
name: development
但这也给出了这个错误
docker : The term 'docker' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
如果您的 gitlab runner 不是太旧 (v1.10+),您可以将其添加到您的管道中:
variables:
GIT_SUBMODULE_STRATEGY: recursive
这样,gitlab 将在 clone/prepare 项目时为您进行同步和更新。
如果不能,则需要在映像中安装 git。您可以查看此回购协议以帮助您 windows 上的 git:https://github.com/StefanScherer/dockerfiles-windows/tree/master/git-for-windows-issue 您可以将其添加到 before_script:
before_script:
- Invoke-WebRequest 'https://github.com/git-for-windows/git/releases/download/v2.15.1.windows.2/MinGit-2.15.1.2-busybox-64-bit.zip' -OutFile 'mingit.zip' -UseBasicParsing
- Expand-Archive mingit.zip -DestinationPath c:\mingit ; \
- Remove-Item mingit.zip -Force ; \
- c:\mingit\cmd\git.exe submodule sync --recursive
- c:\mingit\cmd\git.exe submodule update --init --recursive
Gitlab 文档:https://docs.gitlab.com/ee/ci/git_submodules.html 如何在 windows 上安装 git:
您可以使用 cake 脚本来处理其他 .net 构建过程,并且您可以创建可重用的任务 https://cakebuild.net/dsl/git/