Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 809
Skorunka František Avatar asked Jun 24 '17 17:06

Skorunka František


4 Answers

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.

like image 116
Marina Liu Avatar answered Nov 12 '22 16:11

Marina Liu


There is now a variable specific to a release stage named "Release.Deployment.StartTime" or if you use it in powershell the environment variable is "Release_Deployment_StartTime".

It's in UTC and the format is "yyyy-MM-dd HH:mm:ssZ"
ex: "2018-11-09 21:23:27Z"

NOTE: This variable is set at the time the deployment stage is started, so if you have pre-deployment approvals the time will be set before any approvals are completed. From my testing if you have multiple stages that execute at the same time it will be the same between them, even if one stage waits for the other due to limited agent availability.

I'm using Azure DevOps online, unsure if local TFS installations will have this.

like image 13
JeffR Avatar answered Nov 12 '22 17:11

JeffR


For those who use Linux on tfs:

Define variable

Make sure it has "Settable at queue time set" enter image description here

Create a script in root of your repository

set-build.date.sh:

#!/usr/bin/env bash
DATE=$(date '+%d/%m/%Y %H:%M:%S') 
echo "##vso[task.setvariable variable=BUILD_DATE;]$DATE"

Other options are listed here.

Add shell script right after get sources

Type bash to find this task.

enter image description here

Done, you can use BUILD_DATE variable in later tasks :)

like image 7
Kuba Ptak Avatar answered Nov 12 '22 18:11

Kuba Ptak


An easier way is

$(Date:MMddyy)

Some options are only available in the Build Definition options section. The date formatting is one of them. However, if you were to go into the options section, set the build number format as $(Date:yyyyMMdd-HHmmss), you could then use the $(Build.BuildNumber) variable in your tasks.

More info here - https://learn.microsoft.com/en-us/vsts/build-release/concepts/definitions/build/variables?tabs=batch

like image 5
MrBeanzy Avatar answered Nov 12 '22 18:11

MrBeanzy