0

OK - I've read all about environment variables and how they can't be set and read by the same process (even this can't be read by a later step in the build):

Environment.SetEnvironmentVariable("Major_Build_Number", BaseReleaseNumber, EnvironmentVariableTarget.Machine)

So has anyone come up with a simple way to pass along info from one build step to another? In my first step I determine the build number (this is a fairly complex process believe it or not) and I need to pass that build number to the last build step (which is a Copy Files step) so that it can copy the build into a folder that's named with the build number. I've tried setting an environment variable, but unfortunately that can't be set and read from the same session. There's gotta be a simple way to do this. Yes I could write a PS or batch script to do it and store the value in a file or the registry, but I would prefer to use the Copy Files task and I can't figure out how to pass that value along.

I tried defining a variable in the build definition and storing the value there but I can't seem to change that value after it's set in the build definition.

BTW, this is an on-premises installation - not VSTS.

Anyone have any ideas?

Thanks Andy for your response. So I tried this in SetBuildNumberENVVar.ps1

param([Int32]$MajorBuildNumber=0,[Int32]$MinorBuildNumber=0)

Write-Host "##vso[task.setvariable variable=MajorBuildNumber]$MajorBuildNumber"
Write-Host "##vso[task.setvariable variable=MinorBuildNumber]$MinorBuildNumber"

I then run it from the command line:

c:\powershell .\SetBuildNumberENVVar.ps1 23 45

and then try to echo the variable:

echo %MajorBuildNumber%
%MajorBuildNumber%

and as you can see it doesn't appear to work. I tried this from a C# script:

int.TryParse("$(MajorBuildNumber)", out mbn);

and mbn = 0 after this runs.

Any idea what I'm doing wrong?

Ben_G
  • 770
  • 2
  • 8
  • 30

1 Answers1

0

Generally you can use the predefined variale $(Build.BuildNumber) to name the folder, it can be used directly in entire build process. See Predefined variables for details.

If you customed the build number variable as you said : "I determine the build number (this is a fairly complex process believe it or not)". Then you can pass the value with the Logging Command: ##vso[task.setvariable]value:

  1. Add a PowserShell task in you build definition
  2. Copy and paste below script and save it as *.ps1 file

    $value= "The value of the build number here" Write-Host "##vso[task.setvariable variable=BuildNumber]$value"

  3. Check in the PS file, then run the PS file in PowerShell task

After that, you can use the variable $BuildNumber directly in any tasks behind the PowserShell task.

You can reference my answer in another similar thread : Custom TFS Enviroment Variable doesn't read $(Date)


UPDATE:

You need to run the PowerShell script in TFS build process.

See below example:

I created two PS scripts, one to set the variables to pass the value, another to use the variables to create a folder named with the passed values:

PS1: PassBuildNumber

param([Int32]$MajorBuildNumber=0,[Int32]$MinorBuildNumber=0)

Write-Host "##vso[task.setvariable variable=MajorBuildNumber]$MajorBuildNumber"
Write-Host "##vso[task.setvariable variable=MinorBuildNumber]$MinorBuildNumber"

PS2 : Use the variables

Write-Host "The Major build number is:" $env:MajorBuildNumber
Write-Host "The Minor build number is:" $env:MinorBuildNumber

$foldername = $env:MajorBuildNumber + "." + $env:MinorBuildNumber
Write-Host "foldername:" $foldername

$path = "\\myshare\DirA\$foldername"
Write-Host "path:" $path

New-Item -Path $path -ItemType directory # Create a folder
Write-Host "##vso[task.setvariable variable=path]$path"  # Set path as a variable to be used in Copy Task

Then you can use Copy Files task to copy files to that folder.

enter image description here

Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55
  • Thanks Andy. I can't format a comment so I'm going to edit my post with new questions since this doesn't appear to be working. Please take a look at my new comments and let me know what I'm doing wrong if you get a chance. – Ben_G Mar 14 '18 at 17:23
  • @Ben_G The logging commands only available in TFS/VSTS build/release process. That means you need to run the powershell script in TFS (local command line is not available). Just exactly follow the steps I mentioned in above answer. – Andy Li-MSFT Mar 15 '18 at 04:04
  • So I can't set the variables in a C# program at the start of the build? I'm leaning toward just writing the values to a file in a C# program and then reading them later in another C# program or a PowerShell script. – Ben_G Mar 15 '18 at 14:41
  • @Ben_G Not checked that, but the simplest way is using the Logging Command as I provided in above answer. Once the variables set, you can use the variables in the later steps with C# program or a PowerShell script during the build. Whatever here is a [guide to set environment variables with C#](http://www.dreamincode.net/forums/topic/178713-working-with-environment-variables-in-c%23/), you can have a check with that. – Andy Li-MSFT Mar 16 '18 at 09:56
  • Hey @Andy Li-MSFT - So I got this working up until the point of the New_Item when I get the error: New-Item : Access is denied. I can run New-Item from the command-line and I even logged on as the service that TFS Build is running under and I can run it from the command line there too. Any idea what's going on? I got it to work by using New-PSDrive with -Credential passed in and then using that new mapped drive to create the folder but that requires that I hard-code the ID and password in he script which I'd prefer not to do. I'm going to create separate post about this. – Ben_G Mar 29 '18 at 18:14
  • The separate post is here: https://stackoverflow.com/questions/49562969/ps-new-item-access-is-denied-runs-fine-from-command-line-but-not-during-execut – Ben_G Mar 29 '18 at 18:39