AndrasCsanyi
AndrasCsanyi

Reputation: 4245

How to set datetime to value of variable using Powershell in VSTS builds?

The case: I have a project where I use nuget packages sharing small libraries between components. I use nuget packages produced by CI builds and once they are done I let the pull request go into master branch and this build will produce a new nuget package which will be used in production.

Nuget package created by CI build has the following properties:

Nuget package create by Release build has the following properties:

VSTS build provides the option to use datetime in build name, so it will be unique always. The problem is here.

As a result I have a strangle looking consistency between my builds and nuget packages:

Another problem NugetCommand and VSTS cooperation is that, they take the timestamp in different moment, so there is a difference between the two timestamp beside the two hours. It is not that big deal because I don't kick off builds on same build-branch in every second, but still...

NugetCommand has the ability to read the version number of the package it will create from BuildNumber, which is the same as VSTS going to give the new artifact.

So, I would like to solve this problem until they make it more consistent. I already reported to them a few requests. What I figured out so far is using PowerShell to populate a variable with timestamp and it will be used by VSTS build and NugetCommand task.

I followed the example here but it deals with only strings and not date.

I put together the following script, but it does not provide the result I would like to have.

Powershell task 1:

Write-Host "Setting up the date time for build variable and nuget package identifiers"
Write-Host "##vso[task.setvariable variable=dateTimeIdentifier]Get-Date -format yyyyMMdd-Hmmss"
Write-Host "Get-Date -format yyyyMMdd-Hmmss"
Get-Date -format yyyyMMdd-Hmmss

Powershell task 2 where the argument is:

-dateTimeIdentitfier"$(dateTimeIdentifier)"

Write-Host "Checking: $(dateTimeIdentifier)"

And the result is where you can see that the Powershell command is the value of the variable not the formatted datetime.

Setting up the date time for build variable and nuget package identifiers
Get-Date -format yyyyMMdd-Hmmss
20170709-202457
Checking:  Get-Date -format yyyyMMdd-Hmmss

I know how to solve issues like this in bash, but I don't have build agent where bash is available.

Bash like solution would like to be like this, which is not correct, but the command between `` will be executed first and the result will be passed through the remaining command as parameter.

Write-Host "##vso[task.setvariable variable=dateTimeIdentifier]`Get-Date -format yyyyMMdd-Hmmss`"

My question is, how to solve this in VSTS build and its task environment? Is there a solution for this? Is my approach correct?

Upvotes: 2

Views: 3563

Answers (2)

Marina Liu
Marina Liu

Reputation: 38096

You can change your first PowerShell task (Powershell task 1) as below:

Write-Host "Setting up the date time for build variable and nuget package identifiers"
$date=$(Get-Date -format yyyyMMdd-Hmmss)
Write-Host "##vso[task.setvariable variable=dateTimeIdentifier]$date"
Write-Host "Get-Date -format yyyyMMdd-Hmmss"
Get-Date -format yyyyMMdd-Hmmss

Add a variable in power shell script, and then set a VSTS (Azure DevOps) variable to use in subsequent tasks

Upvotes: 3

Dylan West
Dylan West

Reputation: 1

If you want to have the datetime show when you write the output using write-host, and I highly recommend you use write-output instead, you can do the following:

$DateTime = Get-Date -format yyyyMMdd-Hmmss
Write-Host "Checking: $DateTime"

This will output this:

Checking: 20170710-64624

EDIT: Now for your issue, sorry I missed it the first time, the dateTimeIdentifier being returned looks like it is a string "Get-Date -format yyyyMMdd-Hmmss"

So if you want to execute that code, you need to first, make a script block, then execute that script block.

$ScriptBlock = [ScriptBlock]::Create("Get-Date -format yyyyMMdd-Hmmss")
& $ScriptBlock

Upvotes: 0

Related Questions