在Windows运行Gitlab Runner打包基于.NET Framework 4.6.1的项目

摘要

本文详细描述了运行在Windows商的Gitlab Runner,如何自动集成.NET Framework的项目。

Gitlab中的变量

变量1:NUPKG_OUTPUT_ROOT

这个目录是在git获取的解决方案根目录之外,因为stages变了以后,当前Gitlab Runner工作的当前解决方案根目录下会被清空。我们希望build了以后经过单元测试,测试通过了才push到仓库。所以这个目录必须是在Gitlab Runner的build目录之外。

变量2:BUILD_LIBRARY_SCRIPT

Windows服务器上存放PowerShell的文件路径。

变量3:MSBUILD_PATH

Windows服务器上,msbuild.exe文件所在的目录。

Gitlab Runner服务器上的PowerShell

function Build-Project {

	param (
		[string]$msbuild_dir,
		[string]$ci_project_dir,
		[string]$project_path,
		[string]$version,
		[string]$output_path 
	)

$project_file = $ci_project_dir + "\\" + $project_path
if(( $project_path -like "/*") -or ($project_path -like "\\*")){
	$project_file = $ci_project_dir + $project_path
}
if (-not $output_path) {
	Write-Host "没有传入第5个参数,nupkg包将放在解决方案根目录的nupkg中"
	$output_path =$ci_project_dir + "/nupkg"
} else {
	Write-Host "指定了nupkg包的输出目录"
}
Write-Host "nupkg包的输出目录是:" + $output_path

echo "项目文件"$project_file 
echo "版本号"$version
echo "输出路径"$output_path
cd $msbuild_dir
dotnet nuget locals http-cache -c

$exePath = $msbuild_dir + "\\msbuild.exe"
$build_error_file = $ci_project_dir + "\\" + [System.Guid]::NewGuid().ToString("n") + ".log"

.\msbuild -restore $project_file -t:go -fl2 -flp2:logfile=$build_error_file -flp2:errorsonly
if(Test-Path $build_error_file){
	throw "还原项目时出错:" + $project_file
}

 .\msbuild $project_file /p:OutputPath=$output_path /p:Version=$version -t:go -fl2 -flp2:logfile=JustErrors.log -flp2:errorsonly
if(Test-Path $build_error_file){
	throw "编译项目时出错:" + $project_file
}

.\msbuild -t:pack $project_file /p:Configuration=Release /p:OutputPath=$output_path /p:PackageOutputPath=$output_path /p:PackageVersion=$version  -t:go -fl2 -flp2:logfile=JustErrors.log -flp2:errorsonly
if(Test-Path $build_error_file){
	throw "打包项目时出错:" + $project_file
}

}
  • -t:go -fl2 -flp2:logfile=$build_error_file -flp2:errorsonly

上述PowerShell脚本,如果参考用 MSBuild 获取生成日志来写输出错误日志的开关,MSBUILD会报错,刚刚已经在微软MSDN网页反馈了BUG。在我最新版本的MSBUILD就得如我上述提供的写法才能正确工作。

Visual Studio中编写.gitlab-ci.yml

stages:          # List of stages for jobs, and their order of execution
  - build

variables:
    YEE_CLOUD_VERSION_ID : 2024.5.$CI_PIPELINE_IID
    YEE_CLOUD_NUPKG_DIR : $NUPKG_OUTPUT_ROOT\\Yee.Cloud\\$YEE_CLOUD_VERSION_ID

build-job:       # This job runs in the build stage, which runs first.
  stage: build
  tags:
    - zhongfang-windows
  before_script:
    - .$BUILD_LIBRARY_SCRIPT
  script:
    - echo "Compiling the code..."
    # Yee.Cloud
    - Build-Project $MSBUILD_PATH $CI_PROJECT_DIR "Yee.Cloud/Yee.Cloud.csproj" $YEE_CLOUD_VERSION_ID $YEE_CLOUD_NUPKG_DIR

执行效果

有图有真相:

本文还会有后续更新。

热门相关:傲世武皇   终极高手   符宝   呆萌配腹黑:绝宠小冤家   年轻姐妹的味道