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?
deployment azure-devops
add a comment |
How can I get a current date-time and pass it as a variable to some Deployment task?
deployment azure-devops
add a comment |
How can I get a current date-time and pass it as a variable to some Deployment task?
deployment azure-devops
How can I get a current date-time and pass it as a variable to some Deployment task?
deployment azure-devops
deployment azure-devops
asked Jun 24 '17 at 17:12
Skorunka FrantišekSkorunka František
1,9212236
1,9212236
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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.
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"
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)
.
This is brilliant!
– Skorunka František
Jun 26 '17 at 8:31
Excellent.. Helped me
– Arun Rana
Jul 8 '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 7: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 5 '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 - MSFT
Jan 8 '18 at 8:43
|
show 2 more comments
For those who use Linux on tfs:
Define variable
Make sure it has "Settable at queue time set"
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.
Done, you can use BUILD_DATE
variable in later tasks :)
add a comment |
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://docs.microsoft.com/en-us/vsts/build-release/concepts/definitions/build/variables?tabs=batch
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 1 '18 at 7:50
add a comment |
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44738944%2fvsotfs-get-current-date-time-as-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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"
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)
.
This is brilliant!
– Skorunka František
Jun 26 '17 at 8:31
Excellent.. Helped me
– Arun Rana
Jul 8 '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 7: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 5 '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 - MSFT
Jan 8 '18 at 8:43
|
show 2 more comments
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.
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"
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)
.
This is brilliant!
– Skorunka František
Jun 26 '17 at 8:31
Excellent.. Helped me
– Arun Rana
Jul 8 '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 7: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 5 '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 - MSFT
Jan 8 '18 at 8:43
|
show 2 more comments
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.
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"
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)
.
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.
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"
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)
.
edited Jan 1 at 7:36
Saeb Amini
13.5k55155
13.5k55155
answered Jun 25 '17 at 13:13
Marina Liu - MSFTMarina Liu - MSFT
23.6k21830
23.6k21830
This is brilliant!
– Skorunka František
Jun 26 '17 at 8:31
Excellent.. Helped me
– Arun Rana
Jul 8 '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 7: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 5 '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 - MSFT
Jan 8 '18 at 8:43
|
show 2 more comments
This is brilliant!
– Skorunka František
Jun 26 '17 at 8:31
Excellent.. Helped me
– Arun Rana
Jul 8 '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 7: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 5 '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 - MSFT
Jan 8 '18 at 8:43
This is brilliant!
– Skorunka František
Jun 26 '17 at 8:31
This is brilliant!
– Skorunka František
Jun 26 '17 at 8:31
Excellent.. Helped me
– Arun Rana
Jul 8 '17 at 16:42
Excellent.. Helped me
– Arun Rana
Jul 8 '17 at 16:42
1
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 7:07
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 7: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 5 '18 at 13:26
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 5 '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 - MSFT
Jan 8 '18 at 8:43
@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 - MSFT
Jan 8 '18 at 8:43
|
show 2 more comments
For those who use Linux on tfs:
Define variable
Make sure it has "Settable at queue time set"
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.
Done, you can use BUILD_DATE
variable in later tasks :)
add a comment |
For those who use Linux on tfs:
Define variable
Make sure it has "Settable at queue time set"
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.
Done, you can use BUILD_DATE
variable in later tasks :)
add a comment |
For those who use Linux on tfs:
Define variable
Make sure it has "Settable at queue time set"
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.
Done, you can use BUILD_DATE
variable in later tasks :)
For those who use Linux on tfs:
Define variable
Make sure it has "Settable at queue time set"
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.
Done, you can use BUILD_DATE
variable in later tasks :)
answered Oct 29 '18 at 11:21
Kuba PtakKuba Ptak
4914
4914
add a comment |
add a comment |
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://docs.microsoft.com/en-us/vsts/build-release/concepts/definitions/build/variables?tabs=batch
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 1 '18 at 7:50
add a comment |
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://docs.microsoft.com/en-us/vsts/build-release/concepts/definitions/build/variables?tabs=batch
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 1 '18 at 7:50
add a comment |
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://docs.microsoft.com/en-us/vsts/build-release/concepts/definitions/build/variables?tabs=batch
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://docs.microsoft.com/en-us/vsts/build-release/concepts/definitions/build/variables?tabs=batch
edited Jan 9 '18 at 17:01
answered Jan 9 '18 at 16:30
MrBeanzyMrBeanzy
1,19331931
1,19331931
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 1 '18 at 7:50
add a comment |
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 1 '18 at 7:50
2
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 1 '18 at 7:50
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 1 '18 at 7:50
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 9 '18 at 21:35
JeffRJeffR
1,18111017
1,18111017
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44738944%2fvsotfs-get-current-date-time-as-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown