我没有使用yaml,因为我的公司使用TFVC,所以我需要经典的方式。
使用 $[pipeline.startTime] 我得到开始时间,但现在我需要以这种方式格式化:dd.MM.yyyy
像VSO(TFS) - 将当前日期时间作为变量那样的powershell脚本帮助了我,但是直接在变量中设置日期将是一种更干净的方法
如何设置包含以下格式的日期的 Azure DevOps Pipeline 变量:25.07.2020
由于您使用的是经典方法,因此构建管道中不支持嵌套变量。所以,我们不能使用像
$(Get-Date -Format Date:MMddyy)
这样的变量来设置日期时间。
我们只能设置如下变量:
$[format('{0:ddMMyyyy}', pipeline.startTime)]
这样我们就可以得到值
10072020
,而不是没有10.07.2020
的.
。我无法在 ddMMyyyy
之间添加任何间隔,Azure 管道不支持它。
此外,作为解决方法,我们可以在 Options 选项卡中使用值
$(DayOfMonth).$(Month).$(Year:yyyy)
: 定义内部版本号格式
然后我们可以直接使用变量
$(Build.BuildNumber)
来获取日期时间:
您可以使用表达式定义 Azure DevOps 管道变量:https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops 它支持 .NET 自定义日期和时间格式说明符:
$[format('{0:dd}.{0:MM}.{0:yyyy}', pipeline.startTime)]
@ChamindaC 的回答启发了我的解决方案:
Write-Host "Setting up the date time for build variable"
$myDate=$(Get-Date -format yyyyMMdd-Hmmss)
Write-Host "##vso[task.setvariable variable=MyDate]$myDate"
然后在我的管道中我可以参考
$(MyDate)
。
对于您的特定日期格式,您可以这样做:
Write-Host "Setting up the date time for build variable"
$myDate=$(Get-Date -format dd.MM.yyyy)
Write-Host "##vso[task.setvariable variable=MyDate]$myDate"