上一篇:《劈荆斩棘:Gitlab 部署 CI 持续集成》
上一篇所配置的.gitlab-ci.yml
:
stages:
- build
- test
before_script:
- echo "Restoring NuGet Packages..."
- C:\NuGet\nuget.exe restore "src\CNBlogsCI-Sample.sln"
only:
- master
build_job:
stage: build
script:
- echo "Release build..."
- C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe /consoleloggerparameters:ErrorsOnly /maxcpucount /nologo /property:Configuration=Release /verbosity:quiet "src\CNBlogsCI-Sample.sln"
except:
- tags
only:
- master
test_job:
stage: test
script:
- echo "Tests run..."
- C:\xunit.runner.console\tools\xunit.console.exe "src\ClassLibrary2\bin\debug\ClassLibrary2.dll"
- C:\xunit.runner.console\tools\xunit.console.exe "src\ClassLibrary3\bin\debug\ClassLibrary3.dll"
only:
- master
有几个问题:
before_script
要执行两次。C:\NuGet\nuget.exe
写死路径的写法不可取。test_job
需要上传debug
中的dll
文件。
对于上面的问题,完善如下:
stages:
- build
build_job:
stage: build
script:
- echo "Restoring NuGet Packages..."
- nuget restore "src\CNBlogsCI-Sample.sln"
- echo "Release build..."
- msbuild /consoleloggerparameters:ErrorsOnly /maxcpucount /nologo /property:Configuration=Release /verbosity:quiet "src\CNBlogsCI-Sample.sln"
- echo "Tests run..."
- xunit.console "src\ClassLibrary2\bin\debug\ClassLibrary2.dll"
- xunit.console "src\ClassLibrary3\bin\debug\ClassLibrary3.dll"
except:
- tags
only:
- master
nuget
,msbuild
和xunit.console
命令都需要添加环境变量,为什么要去除before_script
和test_job
?因为每执行一个job
,git
都需要Fetching changes...
,所以会清除不受git
版本控制的文件。
还有就是,对于上面的第三个问题,因为msbuild
是Release
模式生成,而我们test
的debug dll
,所以就必须上传文件,我们把test
中的debug
改为Release
就可以了。
我们可以把nuget
,msbuild
和xunit.console
独立出批命令实现。
restore.cmd
:
echo "NuGet Sources List..."
nuget Sources List
echo "Restoring NuGet Packages..."
nuget restore "src\CNBlogsCI-Sample.sln"
build.cmd
:
echo "Release build..."
msbuild /consoleloggerparameters:ErrorsOnly /maxcpucount /nologo /property:Configuration=Release /verbosity:quiet "src\CNBlogsCI-Sample.sln"
test.cmd
:
echo "Tests run..."
xunit.console "src\ClassLibrary2\bin\Release\ClassLibrary2.dll"
xunit.console "src\ClassLibrary3\bin\Release\ClassLibrary3.dll"
.gitlab-ci.yml
:
stages:
- build
build_job:
stage: build
script:
- ./restore.cmd
- ./build.cmd
- ./test.cmd
except:
- tags
script
中的cmd
命令之前需要添加./
。