[英]Azure Pipeline - Increment build number and display in web app
我有以下简单的构建管道在 Azure DevOps 中工作,并将发布部署到暂存槽。
我想要一个构建修订/版本字符串,它是自动递增的。 然后我想在我的 web 应用程序中显示它,这样我就可以知道哪个版本的软件正在生产中。
目前我显示 .csproj 文件中的版本字符串。 像这样的东西
<Version>1.1.4.7</Version>
然后使用以下代码显示在 web 页面上:
Version: @typeof(Startup).Assembly.GetName().Version.ToString()
如果我可以更新现有的版本字符串,那会很棒,但我愿意更改为最容易集成到 CI 过程中的版本。
在.Net Core世界中简化了版本控制。
编辑csproj并按如下方式修改它:
<PropertyGroup>
<Version Condition=" '$(BUILD_BUILDNUMBER)' == '' ">1.0.0.0</Version>
<Version Condition=" '$(BUILD_BUILDNUMBER)' != '' ">$(BUILD_BUILDNUMBER)</Version>
</PropertyGroup>
如果您的文件没有版本节点,请添加以上内容。
上面的设置意味着本地调试将为您提供1.0.0.0版本,如果您在非Azure DevOps环境中构建,您最终将获得1.0.0.0版本。 $(BUILD_BUILDNUMBER)是由Team Build设置的环境变量,它将在构建时由VSTS或TFS更新。
.Net版本需要采用[major]。[minor]。[build]。[revision]格式,每个段都是0到65000之间的数字。您可以在Options选项卡中配置内部版本号格式,请参见此处有关格式的更多信息。 有关配置构建的有用步骤,请参见此处 。
补丁变量是$[counter(format('{0}.{1}',variables['Major'], variables['Minor']),0)]
然后我将所有这些值作为 powershell 任务直接放入一个变量中。
- task: InlinePowershell@1
displayName: Create Version Number
inputs:
Script: |
param($major, $minor, $patch)
$bv = "$major.$minor.$patch"
Write-Host "##vso[task.setvariable variable=buildVersion]$bv"
Write-Host "Version of net App : $bv"
ScriptArguments: '-major $(Major) -minor $(Minor) -patch $(Patch)'
现在,当我进行 .net 发布时,我只是通过了版本
- task: DotNetCoreCLI@2
displayName: Publish Application
inputs:
command: 'publish'
publishWebProjects: false
projects: '**/MyAPIProject.csproj'
arguments: '-r $(buildPlatform) -o $(Build.BinariesDirectory) -c $(buildConfiguration) --self-contained true /p:Version=$(buildVersion)'
zipAfterPublish: false
modifyOutputPath: false
真正好的一点是,补丁的种子数为零,然后每个后续构建都会自动增加。
在 .NET Core(尤其是 .NET 6.0)应用程序中,我在 .csproj 中设置了以下代码:
<PropertyGroup>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
</PropertyGroup>
然后在 Azure DevOps 中,我添加了以下 powershell 脚本任务以在我的构建中设置版本。
请注意,我使用的是以下约定:Major -> Phase/Product cycle。 我正在使用变量/参数在管道中传递这个值。 次要 -> Sprint 编号。 我正在使用变量/参数在管道中传递这个值。 Revision -> 此数字的最大限制为 65000,因此不能使用 Build.BuildNumber。 我正在使用(日期:YY)(DayOfYear)。 Build -> 获取 Build.BuildNumber 的第二部分(一天的增量)
- task: PowerShell@2
displayName: 'Edit AssemblyInfo'
inputs:
targetType: 'inline'
script: |
$Major = "$(Major)"
Write-Host "Major: $Major"
$Minor = "$(Minor)"
Write-Host "Minor: $Minor"
$DayOfYear = (Get-Date).DayOfYear
$Year = (Get-Date –format yy)
$Revision = '' + $Year + $DayOfYear
Write-Host "Revision: $Revision"
$BuildNumber = "$(Build.BuildNumber)"
Write-Host "BuildNumber: $BuildNumber"
$Build = $BuildNumber.Split('.')[1]
Write-Host "Build: $Build"
$pattern = '<AssemblyVersion>(.*)</AssemblyVersion>'
Write-Host "Will search in paths.."
$csProjFiles = Get-ChildItem .\*.csproj -Recurse
Write-Host "CsProjFiles found are: "
Write-Host "$csProjFiles"
foreach ($file in $csProjFiles)
{
(Get-Content $file.PSPath) | ForEach-Object{
if($_ -match $pattern){
# We have found the matching line
# Edit the version number and put back.
Write-Host "Matched $file"
$fileVersion = [version]$matches[1]
$newVersion = "{0}.{1}.{2}.{3}" -f $Major, $Minor, $Revision, $Build
'<AssemblyVersion>{0}</AssemblyVersion>' -f $newVersion
} else {
# Output line as is
$_
}
} | Set-Content $file.PSPath
}
您应该使用Azure DevOps管道版本(也称为Azure管道版本)而不是Azure DevOps管道版本(也称为Azure管道版本)。 默认情况下,Azure管道版本将自动增加您的版本。
Azure管道版本默认情况下没有自动版本编号。 因为在它之后增加版本应该在发布阶段完成,因为构建应该只涉及连续集成,而不是用作版本化要发布的构建。
这是Azure Pipelines自动增加版本的官方文档: https : //docs.microsoft.com/en-us/azure/devops/pipelines/release/?view = edit #numbering
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.