I am trying to create a variable in TFS for my task for timestamp in format yyyymmdd. I know that I create a task specifically for this using bash or powershell. However, I am looking to find some existing variable or some way to create this variable without having to setup a task dedicated for itself.
So far I have tried to use $(Date:yyyymmdd) in my variables but it does not put values in it, it uses variable name as is.
For example, C:\Alpha\beta\$(Date:yyyymmdd) instead of C:\Alpha\beta\20191107
Can anyone help me with this ? Thanks a lot
Actually, it is hence not expanded. We do not have this kind of system or environment variable which get current date time and work every. You may have noticed you could use $(Date), however which is only available in the Build number format section. Others such as $(Rev:r) and $(DateOfYear) are the same, do not work outside the BuildNumberFormat-Settings..
Take a look at the list of all system and environment variables here: Predefined variables
As you have pointed out, you need to use a script in a PowerShell Task to set a variable in your build definition, a sample:
$date=$(Get-Date -Format 'yyyymmdd');
Write-Host "##vso[task.setvariable variable=time]$date"
Then you can use $(time) in your subsequent build tasks.
More details also take a look at this similar question: VSO(TFS) - get current date time as variable
Related
I've been looking around my machine to see where the postman environment variables are stored. I've looked under AppData\Local\Postman, and C:\Users\username\Postman folders, and haven't found a config file that has a last modified date matching my change of environment variables.
I know I can export the environment variables, but I want to search over the current variables. And the exports don't include the current values, unless they replace the initial value, which I want to keep.
There are still ways to get around this. But I want to write a simple command to fetch some current environment variables via cmd, ex using grep.
So is there a way to check for the current environment variables? Where are they stored?
I don't think this will be a successful attempt. Postman seems to use a database, e.g. leveldb, according to information I found here. That will be stored as a binary file on your disk.
You can, however, have a look into the DB by going to View => Developer => Show DevTools and then going to Storage => IndexedDB => variable_sessions => workspace. I can find a current value for an environment variable like this:
But I don't see a way to search in this other than by keys which are uuids and not variable names or values.
All in all, exporting your environments into a text file might be the easiest option.
According to https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=tfs-2018
These variables are automatically set by the system and read-only. (The exceptions are Build.Clean and System.Debug.)
Nonetheless, if one tries to create a vnext build with the following tasks
Inline Powershell - Write-Host $env:BUILD_SOURCEVERSION
Inline Powershell - Write-Host ("##vso[task.setvariable variable=build.sourceversion;]"+'someNewValue')
Inline Powershell - Write-Host $env:BUILD_SOURCEVERSION
Task 2 won't fail, and the last task will output something like
2018-10-24T07:37:23.1232438Z someNewValue
instead of the expected original source version (the value printed in the first task).
So,
Either I am misreading the docs / they are unclear on that account
Or is it some genuine defect in TFS that one should pursue with MS?
That's expected behavior.
The predefined variables are automatically set by the system and read-only.
However if you have defined a pipeline variable of the same name
as an environment variable (for example, PATH), your pipeline variable
value overrides the agent host's environment variable.
See Environment variables for details.
So, in your scenario actually you defined a new pipeline variable with the same name as the pre-defined one, but not really overwrite the predefined variable. And they can only be used in the pipelines...
UPDATE:
Well, the documentation is somewhat misleading about the Environment Variables and makes some slightly contradictory claims about their readonlyness. Actually all variables (*predefined, build, environment...) basically work as the environment variables mentioned here.
BTW, you can get all the available environment variables by get-childitem -path env:* in pipeline.*
PowerShell script for example:
$environmentVars = get-childitem -path env:*
foreach($var in $environmentVars)
{
$keyname = $var.Key
$keyvalue = $var.Value
Write-Output "${keyname}: $keyvalue"
}
I want to rename file in Jenkins so that a file name would contain a timestamp. I installed zentimestamp plugin and tried to use it:
SET ts=${BUILD_TIMESTAMP}
ren file_name.zip file_name_%ts%.zip
but what i get is
file_name_${BUILD_TIMESTAMP}.zip
I got Date and Time Pattern in Global Properties set to: yyyyMMddHHmm.
Can someone please explain me how to use BUILD_TIMESTAMP properly?
It looks like your code is from a Microsoft Batch file? If that's the case you'd reference BUILD_TIMESTAMP like this:
ren file_name.zip file_name_%BUILD_TIMESTAMP%.zip
BUILD_TIMESTAMP should be an environment variable if it's being injected correctly by the plugin. How you reference environment variables changes depending on what scripting solution is being used (e.g. batch, bash, ant, gradle, etc).
I'm using TFS2015 update 1. I want to pass information from one build step to the next, how is this possible?
That seems like a very simple task, but I can't figure out how that's suppose to work. Passing a variable to a build step is easy, but passing information from one step to the next seems to be impossible. I hope I'm wrong.
You can call the task.setvariable Logging Command, which sets a variable in the variable service of taskcontext. The first task can set a variable, and following tasks are able to use the variable. The variable is exposed to the following tasks as an environment variable.
Example:
##vso[task.setvariable variable=testvar;]testvalue
In tfs 2010 build, it's easy to get that vale using "BuildDetail.RequestedBy". what's the equivalent in TFS 2013? I couldn't find "BuildDetail" even.
It's still build detail. However, in the new templates a lot of the complexity is hidden by default. Use the GetBuildDetail activity to retrieve the variable you need.
Thank you MrHinsh
It's very good suggestion!! Really appreciated!!
The value of "RequestedBy" is actually required by powershell that need to be invoked at later stage to log people name whoever trigger the build.
The following steps are what I've done to make "RequestedBy" value available in powershell script:
In TfvcTemplate.12.xaml, create variable "myBuildDetails".
Drag "GetBuildDetail" into template and specify "myBuildDetails" as result of it.
Modify "Run optional script after Test Runner" property "EnvironmentVariables", set it's value to "New Dictionary(Of String, String) From {{"RequestedBy", MyBuildDetails.RequestedBy}}"
In my ps1, the value can be retrieved by "$Env:RequestedBy"
It's not perfect solution, but it's working.
Instead of modifying build template,I though if we could inject expression something like "$(myBuildDetail.RequestedBy)" from script argument of build definition would be much tidy solution.