问题描述
如何获取当前日期时间并将其作为变量传递给某个部署任务?
How can I get a current date-time and pass it as a variable to some Deployment task?
推荐答案
您可以定义一个任意值的变量,然后将该变量修改为当前日期.详细步骤如下:
You can define a variable with any value, and then modify the variable as current date. Detail steps as below:
假设变量名是time
,我们设置值为none
.如果需要将变量用于环境,可以在环境变量中定义.否则你应该在变量选项卡中定义它.
Assume the variable name is time
, and we set the value as none
. If you need to use the variable for a environment, you can define it in environment variables. Else you should define it in variables Tab.
类型:内联脚本.
内联脚本:
$date=$(Get-Date -Format g);
Write-Host "##vso[task.setvariable variable=time]$date"
- 我在这里使用的日期格式为
MM/DD/YYYY HH:MM AM/PM
.您可以使用其他日期格式. - 对于后续的部署任务,如果要使用当前日期时间,可以直接使用
$(time)
.
- I use the date format as
MM/DD/YYYY HH:MM AM/PM
here. You can use other date formats. - For the subsequent deploy task, if you want to use current date time, you can direct use
$(time)
.
定义变量:使用表达式设置变量 在创建每天重置的计数器值的示例中为这个问题的答案提供了金块.
Documentation for Defining Variables: Set Variables Using Expressions has a nugget of gold for the answer to this question in the example for creating a counter value that is reset daily.
a: $[counter(format('{0:yyyyMMdd}', pipeline.startTime), 100)]
此处使用的 pipeline.startTime
变量很微妙,Pipeline Predefined Variables 文档,即使小心地找到正确的文档基于正在使用的管道方法.正如所建议的 HERE 以及在此线程的一些答案中,某些变量可能具有不同的值或根本不存在,具体取决于您尝试访问它们时的位置.
The pipeline.startTime
variable used here is subtle and not mentioned anywhere in the Pipeline Predefined Variables documentation, even when being careful to land on the correct documentation based on the pipeline method being used. As is suggested HERE and in some of the answers on this thread, certain variables may have different values or not exist at all depending on where you are while trying to access them.
这篇关于VSO(TFS) - 获取当前日期时间作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!