Use TFBuild variables in steps in TFS 2015 - tfs

I need use the $(Rev) property in an argument passed to MSBuild as part of my build process. I have defined MajorVersion, MinorVersion and PatchVersion as variables in my build definition, and I can use these in my arguments. If I pass an argument to MSBuild that contains this string: $(MajorVersion).$(MinorVersion).$(PatchVersion)-beta$(Rev:r) it evaluates to something like 1.3.2-beta$(Rev:r), which causes problems. If I pass this argument: $(MajorVersion).$(MinorVersion).$(PatchVersion)-beta$(Build.BuildId) then it correctly resolves to something like 1.3.2-beta204. I also in time would like to replace the SemVer tag with a variable, e.g. beta, rc, etc., so I can adopt a git-flow approach to my builds and versioning.
Any ideas why $(Rev) isn't usable?

Don't confuse build number format tokens with build variables.
$(Rev:.r) is a one of the tokens, resolved by the TFS build in runtime. You were correct to pass the resolved variable which encapsulates the build number format.
Alternatively, you can use $(Build.BuildNumber) in MSBuild with build number format 1.3.2-beta$(Rev:.r)

Related

In Jenkins, how do I get the substring of a build parameter to be part of the build name?

I've got a parameterized freestyle Jenkins job, and I'd like to include part of one of the parameters in the build name. The parameter that I'm interested in is called FILE_PATH. Due to the file directory structure of the project used by this job, the file path always starts with the same string, which is 9 characters long. So if FILE_PATH="somepath/what/I/want", I would like the name of my build to be what/I/want.
Some things I've tried putting in the "Set Build Name" field provided by the Build Name Setter plugin:
${FILE_PATH:9}
This gave me the following error:
Failed to evaluate name macro:org.jenkinsci.plugins.tokenmacro.MacroEvaluationException: Unrecognized macro 'FILE_PATH' in '${FILE_PATH:9}'
${"FILE_PATH":9}
FILE_PATH was just treated as a string:
New run name is '${"FILE_PATH":9}'
${$FILE_PATH:9}
This just led to the raw expression being included with FILE_PATH being expanded:
New run name is '${somepath/what/I/want:9}'
${ENV,var="FILE_PATH":9}
This didn't get processed at all:
New run name is '${ENV,var="FILE_PATH":9}'
I've got the Build Name Setter and the Token Macro plugins installed. I'd like to do this in the Build Name plugin, if possible, although I can install any other plugins I need to get this to work.
${ENV:9,var="FILE_PATH"} should do what you are looking for.
An alternative is to also eliminate the beginning of the string, as far as you know what exactly the string is:
${ENV#somepath/,var="FILE_PATH"}
In case you ever need to strip something from the end of the string instead, use % in place of #.
I'm assuming you are using a Jenkinsfile pipeline script?
If so, you can just use the groovy substring method.
variable.substring(9)
Not sure if you can do something similar using parameterized builds.

TFS Build Template Customization ( MSBuildArguments)

I have to customize TFS DefaultTemplate.xaml with MSBuildArguments Parameter.
Once we try to create new XAML build definition & select default template, once it gets loaded then I want to set code MSBuildArguments in Advance setting & parameter should be /p:DebugType=pdbOnly;Configuration=Release by default.
As of now this Arguments we have to put manually whenever we create a new build definition. I want to make it customize this template.
screen shot:
Specify MSBuild Arguments when create a build definition or queue a build is more convenient.
However if you want to bind the arguments with the xaml build template, then you need to custom the template.
In your scenario, you can simply replace the value of CommandLineArguments with below strings: (Note that the arguments you added here will be hidden, that means you can not see them in build definition, they are set to be the default arguments)
String.Format("/p:SkipInvalidConfigurations=true {0}", "/p:DebugType=pdbOnly;Configuration=Release").
But same time the MSBuildArguments you specified in build definition will not be available any more.
You can reference below articles to custom the build template to replace the MSBuildArguments:
Pass Relative Path Arguments to MSBuild in TFS2010 Team Build
Properly incorporate MsBuild arguments into your build process
template

Using Variables in a TFS Build Template

We created a build template that accepts the path of a powerscript filename as an argument. Right now it works if I type in the actual path of the script. I would like to pass $(SourceDir) as part of the path, and then replace it with the actual value during the build.
For example: $(SourceDir)\myapp\scripts\scripttorun.ps
Here are the steps that I have done:
In the template I have created a variable named DeploymentScript
Then ConvertWorkspaceItem DeploymentScript -> DeploymentScriptFilename
Then pass DeploymentScriptFilename as an argument to PowerShell
Possibly duplicate with this case TFS How to GetEnvironmentVariable value. You can't use $TF_BUILD_BUILDDIRECTORY in a custom build process.
Refer to the last paragraph in this MSDN document:
Use environment data from a custom build process
If you need to use an environment variable in your custom build
process template, you can use the GetEnvironmentVariable activity to
get the data. You can get data from any of the
WellKnownEnvironmentVariables.
For example, to get the path to the binaries directory, set the Name
property of the GetEnvironmentVariable activity to
Microsoft.TeamFoundation.Build.Activities.Extensions.WellKnownEnvironmentVariables.BinariesDirectory
Please refer more detailed info about how to use it from this blog Using Environment Variables in Visual Studio 2013 and TFS 2013

How to do this Jenkins conditional build?

We are a .net shop so use Jenkins MSBuild plugin to do the build. For each project we have a development and release build, but I notice their only differences are:
/p:Configuration=Debug or /p:Configuration=Release in MSBuild
Release build has an extra copy step at the end
So I am thinking if I can merge them together into one build, and change the above configurations in the build based on the git branch names. I can get the git branch name like this %GIT_BRANCH%= origin/development, but I can't figure out:
How to construct a conditional step to use the above %GIT_BRANCH%.
How to use the above %GIT_BRANCH% to change the /p:Configuration=?
Can anyone shed me some lights?
I assume that, in merging these into a single job, you're expecting to parameterize the build (e.g., defining a Choice parameter named "Type" with values of "Development" and "Release"). If so, you could check "Prepare an environment for the run", then add something like the following to the "Evaluated Groovy script" field:
if ("Release".equals(Type)) {
def map = [Configuration: "Release"]
return map
}
if ("Development".equals(Type)) {
def map = [Configuration: "Debug"]
return map
}
This will define a Configuration environment variable with the specified value, which can then be passed to MSBuild as a ${Configuration} or %Configuration% environment variable (e.g., "/p:Configuration=${Configuration}" as the Command Line Argument).
If you have an additional build step that should only execute conditionally (if the Type is "Release"), you can use the Conditional BuildStep Plugin, in which case you can use a Regular Expression Match, with the Expression "^Release$" and the Label ${ENV,var="Type"} to access the value of the "Type" environment variable and compare it to the "^Release$" regex pattern. You would then do your copy step as the step to execute if the condition is met.
Alternatively, and more simply, you can use IF logic in a Windows batch file, such as this:
IF "%Type%" EQU "Release"
(
rem Copy your files wherever
)

How can I get value "RequestedBy" in tfs 2013 build?

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.

Resources