TFS 2015 Release management access build variables - tfs-2015

In TFS 2015 we have a build, that will automatically trigger a new release.
It is realized with the new script based build definitions.
Now I want to pass a user variable from build to release.
I have created a variable "Branch" in the build.
In the automatically triggered release I try to access it. But it is always empty/not set.
I tried it with $(Branch) and $(Build.Branch).
I also tried to create a variable in release with these names, without success.
Is there any chance, to access a user variable from the build definition in the release?

I do it now with some custom powershell scripts.
In the build task I write a XML file with the variables I need in the release task. The XML file is part of the Artifact later.
So first of all I call my custom script with the path to the XML file, the variable name and the current value:
The powershell script is like this.
Param
(
[Parameter(Mandatory=$true)]
[string]$xmlFile,
[Parameter(Mandatory=$true)]
[string]$variableName,
[Parameter(Mandatory=$true)]
[string]$variableValue
)
$directory = Split-Path $xmlFile -Parent
If (!(Test-Path $xmlFile)){
If (!(Test-Path $directory)){
New-Item -ItemType directory -Path $directory
}
Out-File -FilePath $xmlFile
Set-Content -Value "<Variables/>" -Path $xmlFile
}
$xml = [System.Xml.XmlDocument](Get-Content $xmlFile);
$xml["Variables"].AppendChild($xml.CreateElement($variableName)).AppendChild($xml.CreateTextNode($variableValue));
$xml.Save($xmlFile)
This will result in an XML like this:
<Variables>
<Branch>Main</Branch>
</Variables>
Then I copy it to the artifact staging directory, so that it is part of the artifact.
In the release task I use another powershell script, that sets a task variable by reading the xml.
The first parameter is the position of the xml file, the second the task variable (you have to create the variable in the release management) and the last is the node name in the xml.
The powershell to read the xml and set the variable is like this:
Param
(
[Parameter(Mandatory=$true)]
[string]$xmlFile,
[Parameter(Mandatory=$true)]
[string]$taskVariableName,
[Parameter(Mandatory=$true)]
[string]$xmlVariableName
)
$xml = [System.Xml.XmlDocument](Get-Content $xmlFile);
$value = $xml["Variables"][$xmlVariableName].InnerText
Write-Host "##vso[task.setvariable variable=$taskVariableName;]$value"

Related

Change Visual Studio project property Start action to "Don't open page" in several projects

When I first download our solution from the version control server, I need to set every project Start action property manually. I would like to know if there is any 'unattended' way to do this. I don't mind creating a batch or powershell script, even opening every needed file and search and replace, assuming this property would be in plain text. I wasn't able to find it in the vbproj file.
In vbproj.user the property is <StartAction>, you must change it's value to NoStartPage.
I ended up putting together a powershell script (based on other answers at stackOverflow) to help me out with that.
gci -r -include "*.vbproj.user" |
foreach-object { $a = $_.fullname; ( get-content $a ) |
foreach-object { $_ -replace "(?<=<StartAction>).*(?=<\/StartAction>)", "NoStartPage" } |
set-content $a }

Set ClickOnce ApplicationVersion and MinimumRequiredVersion to date in VSTS Build

I have a visual studio build step in a CI build that creates the clickonce files of a desktop application using the MSBuild arguments below:
/target:publish /p:ApplicationVersion=$(Year:yyyy).$(Month).$(DayOfMonth).$(Build.BuildId) /p:MinimumRequiredVersion=$(Year:yyyy).$(Month).$(DayOfMonth).$(Build.BuildId) /p:InstallUrl=$(InstallUrl)
The $(Build.BuildId) and $(InstallUrl) variables get replaced with their correct values but the $(Year:yyyy), $(Month) and $(DayOfMonth) variables do not get replaced. I am using the same variables to set the Build number format on the General tab and they get replaced correctly. Is it not possible to use the date based variables in a build step in VSTS?
Edit: It appears using $(Build.BuildNumber) would work but I like to include the build definition name in the build number format, which obviously won't work for the version.
$(Year:yyyy), $(Month), $(DayOfMonth) are tokens you could use only in the Build Number Format field, not anywhere else.
I would suggest you to create yourself those variables on the fly, leveraging the following script run by the PowerShell task (with an Inline Script) just before your Visual Studio Build task:
$date = get-date
write-host "##vso[task.setvariable variable=Year;]$(($date).year)"
write-host "##vso[task.setvariable variable=Month;]$(($date).month)"
write-host "##vso[task.setvariable variable=Day;]$(($date).day)"
Then you could use $(Year), $(Month) and $(Day) in place of the tokens you currently use as additional MSBuild arguments.

TFS2015 Powershell on Target Machine

I am trying to pass some data to a remote powershell script within the TFS2015 build step.
My step is calling a remote Powershell script on a target machine. I am passing data as script parameters. The following script parameters are what I have defined.
This parameter list works:
-buildVersion $(Build.BuildNumber) -queuedBy $env:USERNAME (but the name is the account running the script)
,but I really want the Build.QueuedBy username to get passed so I have tried:
-queuedBy $(Build.QueuedBy)
….or
-queuedBy $env:BUILD_QUEUEDBY
This does not work. Am I specifying something incorrectly or is there a better way?
I would also like to get some of the Build definition Variables to the remote script as well.
I have displayed the variables available to me with a Command line step running: cmd /k set
In order to get the correct value you need something like this:
$a = Get-Item -Path "Env:BUILD_QUEUEDBY"
$a = $a.Value

TCL: how to execute program using enviorment PATH variable

I've got following line in my script
exec $::env(PATH)/program.exe
In my env PATH variable I've got a directory where I've got this executable file. For example:
PATH env variable got among other this - D:\my_program\bin
I've got error:
Error:
couldn't execute C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;D:\my_program\bin;\program": no such file or directory
Any suggest how to execute .exe file using system variable like PATH in tcl?
Thanks
PS
OK, when I've create a new env variable (PATH1 - without any other paths, just one) and set .exe file path to it, it seems to work. Any solution to do with PATH (with multiple paths) excluding set D:\my_program\bin in first place?
You should simply use the Tcl library function made for this auto_execok.
Try this:
exec {*}[auto_execok program.exe]
It automatically searches the PATH and constructs the right path for using with exec.
For example, to start notepad.exe:
% auto_execok notepad.exe
C:/windows/system32/notepad.exe
% exec {*}[auto_execok notepad.exe]
To see why the {*} is needed, have a look at http://wiki.tcl.tk/765. Basically auto_execok is pretty smart and can return a list, if needed, e.g. for running start on windows, which needs the expansion to work properly with exec.

Why is this PowerShell script constructing unexpected file paths?

Using tips gleaned from this, this, and this, I've finally been able to get a series of file backup scripts going. However, there's one little thing that I've been unable to solve. No runtime errors, but when I run this script,
$originalPath = "\\Server\Path\_testData\"
$backupPath = "\\Server\Path\_backup\"
#
function supportBackup
{
"$($originalPath) copying DOC XLS PPT JPG GIF PDF WAV AVI to $($backupPath)"
Get-ChildItem $originalPath\* -Include *.doc*, *.xls*, *.ppt*, *.jpg, *.gif, *.pdf, *.wav, *.avi | `
foreach {
$targetFile = $backupPath + $_.FullName.SubString($originalPath.Length);
New-Item -ItemType File -Path $targetFile -Force;
Copy-Item $_.FullName -destination $targetFile
}
"Support File Backup Completed"
}
supportBackup
The original file path gets dumped into the destination directory instead of just the files.
What I want:
\\Server\Path\_backup\files-from-testData-directory
What I get:
\\Server\Path\_backup\_testData\files-from-testData-directory
I know the problem is closely related (if not identical) to this question, but after studying it and trying to apply some of the wisdom from there, using various iterations of the $_.Name variables, I realize I don't have as good an understanding as I thought I did. I need someone to explain to me HOW the destination path and filename are being constructed with the given variables, and what alternate variables (or code) I need to use to achieve my desired results. There's something that's not clicking for me and I need help understanding it.
You're trying too hard. This should suffice:
$originalPath = '\\Server\Path\_testData'
$backupPath = '\\Server\Path\_backup'
$extensions = *.doc*,*.xls*,*.ppt*,*.jpg,*.gif,*.pdf,*.wav,*.avi
function supportBackup {
"$($originalPath) copying DOC XLS PPT JPG GIF PDF WAV AVI to $($backupPath)"
Get-ChildItem "$originalPath\*" -Include $extensions |
Copy-Item -Destination "$backupPath\" -Force
"Support File Backup Completed"
}
supportBackup
You can pipe the results of Get-ChildItem directly into Copy-File. The destination path must end with a backslash, though, otherwise the instruction would try to replace the folder $backupPath with a file of the same name, thus causing an error.