我使用 Jenkins 的 PowerShell 插件并使用 Powershell 查找与模式匹配的所有文件(例如 AssemblyInfo.*),然后读取文件并使用 PowerShell 中的内置正则表达式功能(-match 和 -replace 操作) 来查找和替换 AssemblyVersion 属性,将最后一个八位字节更改为当前的 Jenkins 内部版本号。
function assign-build-number
{
#get the build number form Jenkins env var
if(!(Test-Path env:\BUILD_NUMBER))
{
return
}
#set the line pattern for matching
$linePattern = 'AssemblyFileVersion'
#get all assemlby info files
$assemblyInfos = gci -path $env:ENLISTROOT -include AssemblyInfo.cs -Recurse
#foreach one, read it, find the line, replace the value and write out to temp
$assemblyInfos | foreach-object -process {
$file = $_
write-host -ForegroundColor Green "- Updating build number in $file"
if(test-path "$file.tmp" -PathType Leaf)
{
remove-item "$file.tmp"
}
get-content $file | foreach-object -process {
$line = $_
if($line -match $linePattern)
{
#replace the last digit in the file version to match this build number.
$line = $line -replace '\d"', "$env:BUILD_NUMBER`""
}
$line | add-content "$file.tmp"
}
#replace the old file with the new one
remove-item $file
rename-item "$file.tmp" $file -Force -Confirm:$false
}
}