Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

deployment - VSO(TFS) - get current date time as variable

How can I get a current date-time and pass it as a variable to some Deployment task?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can define a variable with any value, and then modify the variable as current date. Detail steps as below:

Define a variable in release

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. enter image description here

Add a power shell task at the begin of deploy tasks:

Type: Inline Script.

Inline script:

$date=$(Get-Date -Format g);
Write-Host "##vso[task.setvariable variable=time]$date"

enter image description here

Note:

  • 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).

Update

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)]

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...