How to retrieve or pass custom argument TFS Build XAML in Post Build Powershell script - powershell

I have created a new template from default build template for Visual Studio 2013 and created a new custom argument Brand to prompt parameter during queue new build.
I tried to read the build argument in the post-build script within Powershell scripts. But the value is always empty.
I used $env:Brand passing parameter to post build arguments like -arg $(Brand) with and without double quotes. But is shows empty.
Please help how to retrieve Build arguments in the Post Deploy scripts.

I assume you are setting up your property to take your custom parameter. Something like
<x:Property Name="BuildEnvironmentName" Type="InArgument(x:String)" />
Set it up so that it's shown as an option when you queue the build. For example
<mtbw:ProcessParameterMetadata Category="Build" ParameterName="BuildEnvironmentName" DisplayName="Target Environment" Description="The environment where the deployment will take place" Required="true" BrowsableWhen="Always" />
If you want to pass this parameter to the post build script, you will need to pass it in your Xaml. An an example
<mtba:RunScript DisplayName="Run Post-build Script" FilePath="PostBuildScript" Arguments='[BuildEnvironmentName]' Result="PostBuildScriptResult]" mtbwt:BuildTrackingParticipant.Importance="Low"/>

The issue is resolved by adding custom argument into Environment variables to the Run Script Task XAML definition.
New Dictionary(Of String, String) From {{"Key", Value}}

Related

TFS Build and Powershell: how to acces predefined build variables

I have issues with accessing build variables from powershell script for example I want to access Agent.BuildDirectory.
I tried:
$Build.SourcesDirectory
$(Build.SourcesDirectory)
none works.
I know that I can use $Env:TF_BUILD_SOURCESDIRECTORY but not all variables are available this way.
Do you have any suggestions ?
So this is TFS 2017 Update 3. You should be able to do what you are trying to do. Where are you trying to use the variables, as parameter in the build or actually inside a PowerShell script? Those variable can be used as parameters and will be substituted at run time by the agent and passed to scripts, but if you are trying to access them inside a script like $(...) it will not work. You do need to use $env:VARIABLE. All variables in the variables section get converted to environment variables with their name at runtime. So for example if you are running an inline PowerShell like the image bellow, you can use $(..)
or if you want to pass parameters to the a PowerShell script you can also.
But from inside a script you cannot.
You should use $(Agent.BuildDirectory) not $(Agent.SourcesDirectory). Check Agent variables from the link below:
https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#agent-variables
Update:
Adding a screenshot:

variable set in Windows Powershell of Jenkins build, not available in other build steps

I have a Jenkins build with one parameter called VERSION.
Based on the length of the variable i am modifying its value in Windows Powershell and then in the next build step, i want to use it.
But the modified value is not being reflected in the next build step execution, it still refer to the intial value entered as a parameter. I tried ENV,script,global none of them seems to work.
Windows powershell build step
input VERSION=1810(via jenkins build)
if ("$ENV:VERSION".length -eq 4)
{
$ENV:VERSION = "$ENV:VERSION",3 -join "" (here it will be 18103)
}
Write-Output "$ENV:VERSION" (18103 here aswell)
later in the Nexus artifact uploader i refer to this variable as ${VERSION} and the above updated value is not being reflected
(here it is 1810 and not 18103)
Please help
This is a general issue with environment variable scope. Every process inherits the environment variables from its parent, but has its own copy, and any modifications you make will only be reflected in the current process and child processes.
I think you will have to find some way to pass the value to a future step that doesn't rely on environment variables.
You can try to use EnvInject Plugin and set additional PROJ_VERSION=$ENV:VERSION variable in your job. In this case it should be working correctly. If it isn't working within Properties Content directly, try to use injection via file as in this example.
I found another option which works in Freestyle project jobs.
the 1st powershell step:
[Environment]::SetEnvironmentVariable('IMAGE_VERSION', $imageVersion, 'Machine')
the 2nd powershell step:
$imageVersion = [Environment]::GetEnvironmentVariable('IMAGE_VERSION', 'Machine')

VSTS builds - how to define new variable in custom build step and pass to next build steps

We are using VSTS to build our VS solution.
Is there a way to define custom Build Step, for example a PowerShell script, that creates a new variable to be passed to further build steps?
There's nothing about it in MSDN:
https://msdn.microsoft.com/Library/vs/alm/Build/scripts/variables
You can use Task Logging Commands to do this.
To invoke a logging command, simply emit the command via standard
output. For example, from a PowerShell task:
"##vso[task.setvariable variable=testvar;]testvalue"
For Mac, do this :
echo '##vso[task.setvariable variable=variableName;]'$variableValue

Rakefile Parameter substitution in teamcity

I'm trying to pass a standard teamcity build parameter vcsroot.url as the parameter of a rake task, using Teamcity's built in Rake build step. However, the build parameter doesn't seem to be evaluated.
In the "Rake Tasks" box, I've got:
setup_github_pages["%vcsroot.url%"]
When I run this build I get the following error:
[Execute setup_github_pages] NoMethodError: undefined method `[]' for nil:NilClass
Yet on the build results parameter tab, I see the correct value for the vcsroot.url parameter.
Are there rules about which build step fields do / don't have parameter substitution performed? Or is there an escape sequence required (I've scoured the teamcity docs in vain...)
Try adding a custom environment variable to expose the configuration variable you're trying to access:
Reference Teamcity and Rake: Where are the tc system properties?
For example, you want to pass system.CUSTOM property defined in the agent.conf file. Click the Add new variable link, specify CUSTOM as a name and %system.CUSTOM% as a value. Now in the rakefile you can access it as ENV['CUSTOM'].
I've been able to access vcsroot.url directly from within the rake task using this approach.

Is there a way to access TeamCity system properties in a Powershell script?

I'm trying to set up a new build configuration in TeamCity using the Powershell runner. However, I can't seem to find a way to access the TeamCity System Properties in the build script. I've seen hints that it is possible, but cannot find documentation on how to do it.
I have tried accessing the system properties using Powershell variable syntax, $variable. I have also printed out all variables in memory and see no teamcity variables to use.
Is this possible with the Powershell runner, and if so what is the syntax necessary to get it working?
TeamCity will set up environment variables, such as build.number (you can see a list of these within TeamCity).
In Powershell you can access environment variables using the env "provider", e.g.
$env:PATH
TeamCity variables are accessible by replacing the . with a _, so the build.number variable can be accessed as
$env:build_number
As it says in the TeamCity documentation, the system parameters are passed to the build script runner, but not all build script runners know what to do with them. In the case of the Powershell script runner, when using a script file, they don't propagate down to your scripts.
It's occurred to me to write a psake-optimized build runner that does, but in the meantime you can do one of the following:
explicitly map any of the TeamCity build properties to script parameters using the parameter expansion that's available within the Script Source box. eg .\build.ps1 -someParam:%build.name%
use environment parameters, which can be accessed explicitly within PowerShell using $env:NAME_IN_TEAMCITY syntax, eg $env:TEAMCITY_VERSION, or looped over and pushed into variable scope
access the build properties file that TeamCity makes available during the build. The file is available at $env:TEAMCITY_BUILD_PROPERTIES_FILE, and if you load the XML version it's fairly easy to loop through and push them all into scope (though you do get everything as a string of course). I posted a gist on how to do this (https://gist.github.com/piers7/6432985). Or, if using Psake, modify the script above to return you a hashtable which you can pass directly to Psake's -properties argument.
It is posible. Here is example how to pass system properties into PSake script:
& .\psake.ps1 -parameters #{build_number=%build.number%; personal_build=%build.is.personal%}
If you don't use Psake, you can define your variables like this:
$build_number = %build.number%
The %build.number% part will be replaced with TeamCity provided data. I think, it works only in Source code script input mode.
I created a meta-runner that will pass through System parameters to parameters declared in the Powershell script. It's not perfect (if you put '# in your source it will break) but it works for what I needed, you can find it here: https://gist.github.com/anonymous/ef60ada3f48f0fb25093