此答案适用于使用Azure Build Pipeline的人,他们希望将BuildId值插入为程序集版本的最后一个数字,并且BuildId
. (> 65535)
我的解决方案是使用注入文件的最后 4 或 5 位数字。
我不使用模运算,因为版本号看起来与 BuildId 完全不同(达到限制后)。相反,在我的解决方案中,“短路”版本看起来类似于 BuildId。BuildId
AssemblyInfo.cs
例子:
AssemblyVersion
is 1.0.0.0
and the is BuildId
333.
--> 新的 AssemblyVersion 变为1.0.0.333
. (数量少,没问题。)
AssemblyVersion
is1.0.0.0
和 the是BuildId
55555。
--> 新的 AssemblyVersion 变为1.0.0.55555
. (还在范围内。)
AssemblyVersion
is1.0.0.0
和 the is BuildId
66666.
--> 新的 AssemblyVersion 变为1.0.0.6666
. (使用最后 4 位数字。不可能更多。)
AssemblyVersion
is1.0.0.0
和 the是BuildId
111111。
--> 新的 AssemblyVersion 变为1.0.0.11111
. (使用最后 5 位数字。)
按照以下步骤轻松使用
第 1 步:通过此代码段在管道中定义变量shortBuildId
。
variables:
- name: shortBuildId # note: The last 4 or 5 digits of the BuildId, because for the assembly version number the maximum value is 65535
value: '[not set]' # will be set by powershell script
或者,您可以像这样定义它。这取决于您如何定义已经存在的变量的样式。
variables:
shortBuildId: '[not set]'
第 2 步:将这些任务插入现有任务之上。
第一个任务创建简短的 BuildId 并将其保存到 variable shortBuildId
。
第二个任务更新文件中的第 4 个版本字段AssemblyInfo.cs
。因此,短 buildId 被注入到AssemblyVersion
和AssemblyFileVersion
.
注意:在此文件中,您需要一个带有 4 个数字的程序集版本(例如1.0.0.0
)。如果您只有 3 个数字(例如1.0.0
),它将不起作用。
- task: PowerShell@2
displayName: Define short build ID
# If allowed, use the last 5 digits. If they are larger than 65000, use the last 4 digits. Leading zeros are removed.
# This is needed, because the full build ID can't be used as number for the assembly version.
inputs:
targetType: 'inline'
script: |
$shortId = $env:BUILD_BUILDID
$shortId = $shortId % 100000
if ($shortId -gt 65000) { $shortId = $shortId % 10000 }
Write-Host "Build ID: $env:BUILD_BUILDID --> $shortId"
Write-Host "##vso[task.setvariable variable=shortBuildId]$shortId"
showWarnings: true
- task: RegexReplace@3
displayName: Insert shortBuildId into AssemblyInfo:
InputSearchPattern: 'myProjectDirectory\Properties\AssemblyInfo.cs'
FindRegex: '(\[assembly: (AssemblyVersion|AssemblyFileVersion)\("\d+\.\d+\.[0-9*]+)\.[0-9*]+("\)\])'
ReplaceRegex: '$1.$(shortBuildId)$3'
UseUTF8: true
UseRAW: true
第 3 步:调整与您的项目相关的第二个任务中的路径。
编辑 的值InputSearchPattern
。
如果要将 shortBuildId 插入解决方案的所有项目中,只需编写InputSearchPattern: '**\AssemblyInfo.cs'
信用
感谢 Edmund Weitz 博士的伟大工具The Regex Coach,它可以免费使用。