看到阿迪王那边出品了一个基于Azure Devops自增版本号  链接
http://edi.wang/post/2019/3/1/incremental-build-number-for-net-core-via-azure-devops

<http://edi.wang/post/2019/3/1/incremental-build-number-for-net-core-via-azure-devops>

恰巧我自己也有一个版本(虽然核心原理是差不多的)也分享下

 

(以下均基于Tfs 2018的截图,Azure Devops Server暂时还没发布,只能Tfs将就着了,虽然Azure Devops跟当前的Tfs
2018已经界面有"一些"改动不过流程是相通)

 


先说下我们的场景,我们使用Tfs来进行发布,所以我希望做到一个事情是Tfs里Release的版本号能跟我dll的版本号对应上,这样便于版本的对应和关联(实际上我们站点在启动的时候还会上报版本号到exceptionless,假如有机器漏发布了我们就能知道)


看下我们的最终效果,每次Tfs发布之后,都会改变我当前项目产生的dll的版本号,并且这个版本号(4位的)的其中后2位一定跟Tfs里的生成号一样,而Tfs的生成好的第三位是当前的月份和日期,第四位是当天build的次数

首先我能将版本号和发布或者说编译关联,其次我也能知道当天到底编译了几次


<https://img2018.cnblogs.com/blog/658343/201903/658343-20190303231704784-537136980.png>

 

 

 

先说2个前置知识

①Azure Devops里预定义的变量
https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml

<https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml>

②通过Powershell如何读取Azure Devops里CI/CD的变量呢? 直接 $Env.变量名  (注意,他预定义变量名里有时候会有英文句号.
要将其转为下划线,如Build.BuildNumber你要读取的时候应该是 $Env.Build_BuildNumber)

 

再说说.Net里的文件版本号的是怎么来的,主要有Framework和Core两个体系,因为我们两种项目都有(当前还是Framework是大头)所以2个都要支持

Framework,主要是在项目的Properties文件夹里的AssemblyInfo.cs定义


<https://img2018.cnblogs.com/blog/658343/201903/658343-20190303231705662-2035531562.png>


<https://img2018.cnblogs.com/blog/658343/201903/658343-20190303231706136-1579583440.png>

Core,主要是项目里的csproj


<https://img2018.cnblogs.com/blog/658343/201903/658343-20190303231706648-1578151764.png>

 

 

顺带我再加上一个自己的假设,在这些配置文件夹里只有我们的版本号会是正则表达式为
.\d+.\d+.\d+.\d+(也就是x.x.x.x)的规则,我只要用正则找出他替换就行了。

 

等下,好像哪里不对劲。

在core的csproj里因为还管着nuget的引用,所以里面会有他引用的nuget包信息,所以此时他里面可能会有这些内容


<https://img2018.cnblogs.com/blog/658343/201903/658343-20190303231707185-653621065.png>

 

于是在core项目里,我们是将version统一抽走到要给独立的props文件里

 

搞一个props文件,类似这样


<https://img2018.cnblogs.com/blog/658343/201903/658343-20190303231707702-1290900359.png>

然后在你的项目导入你的props


<https://img2018.cnblogs.com/blog/658343/201903/658343-20190303231708234-626590932.png>

 

其实这样子之后,比如如我截图里的,我在里面定义了我项目使用C#
7.2,那么我所有项目都跟着是7.2,我定义了打包带symbol就所有都带,统一了配置,也更好一些(个人感觉)

 

然后就是上powershell(这段powershell并非我原创,也不记得很久前哪里找来的了,然后稍加修改后一直使用至今)
# Enable -Verbose option [CmdletBinding()] # Regular expression pattern to
find the version in the build number # and then apply it to the assemblies
$VersionRegex = "(?<=\d+\.\d+\.)\d+\.\d+" # Make sure path to source code
directory is available if (-not $Env:BUILD_SOURCESDIRECTORY) { Write-Error
("BUILD_SOURCESDIRECTORY environment variable is missing.") exit 1 } elseif
(-not (Test-Path $Env:BUILD_SOURCESDIRECTORY)) { Write-Error
"BUILD_SOURCESDIRECTORY does not exist: $Env:BUILD_SOURCESDIRECTORY" exit 1 }
Write-Verbose "BUILD_SOURCESDIRECTORY: $Env:BUILD_SOURCESDIRECTORY" # Make sure
there is a build number if (-not $Env:BUILD_BUILDNUMBER) { Write-Error
("BUILD_BUILDNUMBER environment variable is missing.") exit 1 } Write-Verbose
"BUILD_BUILDNUMBER: $Env:BUILD_BUILDNUMBER" # Get and validate the version data
$VersionData = [regex]::matches($Env:BUILD_BUILDNUMBER,$VersionRegex)
switch($VersionData.Count) { 0 { Write-Error "Could not find version number
data in BUILD_BUILDNUMBER." exit 1 } 1 {} default { Write-Warning "Found more
than instance of version data in BUILD_BUILDNUMBER." Write-Warning "Will assume
first instance is version." } } $NewVersion = $VersionData[0] Write-Verbose
"Version: $NewVersion" # Apply the version to the assembly property files
$commonPropsFiles = Get-ChildItem -include common.props,AssemblyInfo.cs
-recurse if($commonPropsFiles) { Write-Verbose "Will apply $NewVersion to
$($commonPropsFiles.count) files." foreach ($file in $commonPropsFiles) {
$filecontent = Get-Content($file) attrib $file -r Write-Host $NewVersion
$filecontent -replace $VersionRegex, $NewVersion | Out-File $file Write-Verbose
"$file.FullName - version applied" } } else { Write-Warning "Found no
commonPropsFiles." }
  

 

这段powershell可以弄成一个.ps1文件放到Azure Devops里然后引用




<https://img2018.cnblogs.com/blog/658343/201903/658343-20190303231708670-1227348975.png>

注意使用的时候一定要在高级里配置”工作文件夹”到你项目根目录(一般是csproj所在文件夹)

 

或者直接用Powershell Inline

也就是阿迪王分享的博客里的直接将powershell贴入到里面去执行的那个方法

 

两者效果一样

 

然后在Azure Devops里下其生成的版本号规则,即可完成匹配


<https://img2018.cnblogs.com/blog/658343/201903/658343-20190303231709128-1461198491.png>

 

然后在进行编译的时候你就能看到你的版本号被修改拉


<https://img2018.cnblogs.com/blog/658343/201903/658343-20190303231709648-1086800292.png>

友情链接
ioDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信