41

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

Skorunka František
  • 5,102
  • 7
  • 44
  • 69

5 Answers5

59

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.

Community
  • 1
  • 1
Marina Liu
  • 36,876
  • 5
  • 61
  • 74
  • 1
    Excellent.. Helped me – Arun Rana Jul 08 '17 at 16:42
  • 1
    I am trying to use this trick to use an agent scope variable (Build.SourceVersion), but I can't seem to pass it into the script properly. I have `-version $(Build.SourceVersion)` as the parameter. My script is: `Param( [string]$version ) Write-Host "##vso[task.setvariable variable=SourceControlVersion]$version"` I am not very proficient in powershell, all help is appreciated. – DanCaveman Sep 15 '17 at 07:07
  • This works, but I am running into problems trying to create a Task Group which contains this type of PowerShell Inline Script task. Since the PowerShell function syntax $() matches the VSTS variable syntax $(), the Task Group appears to be parsing required variables such as "Get-Date -Format g" when creating the Task Group. Is there any way to work around this or to escape the PowerShell syntax to avoid this parsing behavior? – Ian Jan 05 '18 at 13:26
  • @Ian Since the problem you are meeting is different from the op's situation, you can create a new question so that it can be answered more specific. – Marina Liu Jan 08 '18 at 08:43
  • Thanks for the detailed info. Really helpful. – AH. May 01 '18 at 07:50
  • @Ian The first line can be changed to this to prevent a VSTS task group from marking it as a parameter: `$date=Get-Date -Format g` Also, one potential issue with this is that if you're deploying to a phase that has multiple machines then the timestamp could potentially be off by a second or two since the POSH script is run on each machine separately. Would not work if the time needed to be exactly the same between all machines in a deployment phase. – JeffR Nov 09 '18 at 20:18
  • 1
    Note that this does work, but the value you set may not be available in all parts of your build. For example, it doesn't work if you are using the variable in your release tag - the variable will always have the default value. In that case you have to set one of the existing variables, like the build number. – SouthShoreAK May 10 '19 at 20:40
  • @DanCaveman your script looks like it has syntax issues. Try `param([string]$version) ...` as specified in the [documentation about functions](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7#named-parameters) – Josh Gust Apr 20 '20 at 21:26
13

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.

JeffR
  • 1,782
  • 1
  • 15
  • 19
  • 3
    Just to mention, it does not work in Build time, only in Release time. – Skorunka František Nov 14 '19 at 11:30
  • This variable doesn't appear in the official documentation, but it works perfectly for Releases. https://learn.microsoft.com/en-us/azure/devops/pipelines/release/variables?view=azure-devops&tabs=batch – Lee Harrison Apr 22 '20 at 14:16
  • 2
    Thank you, $(Release.Deployment.StartTime) worked like a charm in pipeline variables! – David Apr 24 '20 at 08:06
  • @LeeHarrison that's because it explicitly states that not all variables are listed. Although I couldn't find a "Full" list of variables anywhere. Sigh – Gaspa79 Sep 24 '21 at 10:36
7

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

Kuba Ptak
  • 73
  • 2
  • 4
5

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

MrBeanzy
  • 2,286
  • 3
  • 28
  • 38
  • 2
    That does not work. I had to add this format specifier to get the date (or rather the "time") as DD/MM/YY $date=$(Get-Date -UFormat %D); – AH. May 01 '18 at 07:50
4

Based on answer of Marina Liu (here) for quick copy and paste.

Define variable on top:

variables:
  buildTimeStamp: # will be set by script
  # ...

Add this as first task (and change format as needed):

- task: PowerShell@2
  displayName: set variable buildTimeStamp
  inputs:
    targetType: 'inline'
    script: |
      $date=$(Get-Date -Format yyyy-MM-dd_HH.mm);
      Write-Host "##vso[task.setvariable variable=buildTimeStamp]$date"

Now you can use variable $(buildTimeStamp) in your code below.

Beauty
  • 865
  • 11
  • 14