TFS integration - how to set environment variables accessible from test code - tfs

I'm writing a TFS / VSTS integration with our server by using extension.
I want to present extra data after the build, specifically I want to show an IFRAME and navigate to our server, to a specific URL, determined in part by a dynamically generated unique string.
I have the function that generates the string, but I need to set it as an environment variable before the tests start to run. This is important because the tests need to create that string on the server.
I searched the documentation, examples and other places, but couldn't find a complete example that sets a dynamically generated environment variable and then runs tests.
How do I do it?

You can add/set an environment variable through Logging Commands (e.g. PS: Write-Host "##vso[task.setvariable variable=testvar;]testvalue"), then the following tasks can get this variable as general variable.
With Logging Commands, you also can add additional information in build summary. (##vso[task.uploadsummary]local file path)
You also can deploy a build result extension to display additional information. vsts-extension-samples

Related

Jenkins: Pass environment variable to the job parameter

Since I have the same static rarely changed parameters used by several jobs I decided to put it somewhere in one place of my Jenkins and use it across jobs.
The first thought that came to my mind was to move my 'static data' to the environment variables and get it using Active choice reactive parameter plugin which allows running simple groovy scripts on the job parameters page.
Please note that I know how to get environment parameters in the pipeline, but I do really need to have this data on the build with parameters screen, e.g. once I clicked build with parameters - I need my groovy code inside Active choice reactive parameter was able to read this environment variable and display as a parameter to the user.
A simple example of this need:
The environment variable contains the list of servers, the job is going to perform deployment of the application to the selected server. In this case, I want to be able to write something like this in the groovy script section of Active choice reactive parameter:
return[${env.SERVERS_LIST}]
Unfortunately the example above doesn't work. I wasn't able to find any working solution for this yet.
Well, after a few more tries I finally found a solution.
Instead of trying to read the environment variable in the pipeline manner the simple
return [SERVERS_LIST]
works perfect

TFS: Deploy same application to multiple geographic sites

Using TFS I want to deploy the same web application to two different locations (West,Central). For this, I am trying to run phases concurrently to both locations using the Run on multiple agents in parallel option.
The following sample contains only one step Deploy Website files
Here my question.
How can I provide a different value for the $(WebsiteServer) variable based on the multiplier?
I have tried to configure multiple variables in the environment.
And then use them like $($(Location).WebsiteServer)) but that does not expand correctly.
I am thinking now on creating a custom task that will create variables at release time base on the hardcoded values and use those variables instead. But it seems hacky.
Is there a better way to solve this?
Make WebsiteServer your multiplier, then make the value a comma-separated list of the servers.
You could add some variables as followed.
WebsiteServer, User and Pass variables are some intermediate variables to store your WebsiteServer IP, User Name and Password.
You could configure your environment as followed.
You could add a PowerShell Task to modify the values of WebsiteServer, User and Pass variables by logging command "##vso[task.setvariable variable=variableName]variableValueā€¯ from nested varialbes, such as $($(Location).WebsiteServer).
You could refer to the following powershell script:
Script:
Write-Host "##vso[task.setvariable variable=WebsiteServer]$($(Location).WebsiteServer)"
Write-Host "##vso[task.setvariable variable=User]$($(Location).User)"
Write-Host "##vso[task.setvariable variable=Pass]$($(Location).Pass)"
You could use WebsiteServer, User and Pass variables in Copy files Task like the following.

FAKE get TeamCity build branch

I'm constructing some build scripts using FAKE for a TeamCity setup. Part of my build requires me to access the teamcity.build.branch parameter, however because it's not an environment variable, I'm not sure how to access it.
I've tried adding %teamcity.build.branch% to an environment variable in TeamCity but it doesn't seem to like that. I also can't access it using the FAKE git module because team city agents don't use git to get the project files, TeamCity hands it to them.
How would I go about getting the teamcity.build.branch parameter through to my FAKE scripts? Ideally I'd like it as an environment variable so I can pick it up straight from within the script, but I'm open to any other ideas.
Try passing %teamcity.build.branch% as a parameter in your build step.
I figured it out with a bit of help from #Nadeem's answer.
I now have my build.bat/cmd file taking in a parameter which I'm then passing into my Fake.exe call like so:
"packages\FAKE\tools\Fake.exe" build.fsx branch=%1
Then inside my FAKE code I'm using the getBuildParamOrDefault "branch" <default branch here>.
I'm then using this as the branch name. I'm also using a string split on the build param that comes in because normally it's in the format of ref/head/<branch name> and I only want the branch name.
I also had to pass in %teamcity.build.vcs.branch.<project>% instead of %teamcity.build.branch% as my agents didn't like the implicit requirement. I'm not entire sure why this is but all is working well now.

Using environment variables with Jenkins

I'm building a group of projects from the SVN. There is a possibility of changing the SVN location time to time. As there are bunch of projects I hope to give the repository url with a environment variable so i can change all the url's easily. Any idea how to do that??
In Subversion Source Code Management, you can use variable in the Repository URL, simply type:
http://my.svn.com/path/to/${VARIABLE}
${VARIABLE} is a job parameters that is defined earlier. Never heard of anyone wanting to use actual environment variables for this, but you can try with the same syntax.
By default, it will give you a red warning that this is not a valid URL. You can disable this warning by going to Manage Jenkins -> Configure System and look for Validate repository URLs up to the first variable name. Put a checkmark there and save.

Get result of a build step in Hudson/Jenkins to re-use it in another one

My question may be silly but I've been trying several ways and I still can't do what I want, i.e.:
use the scp target of Ant to target a remote machine and execute
a script there
this script creates a dynamic list of files
get this list of files (only their names) back in Hudson to use it in the next build step (another scp from Ant)
I tried to use environment variables but they are interpreted by Hudson so I'm stuck here...
Globally my question would be: how to get a result from an Ant build step ?
Thanks for your ideas,
Emmanuel
You may find File parameter useful. This allows you to create an input file, pass it to build. You may need to write script/ant script to process the file though.
In the long term you may evaluate a Hudson farm. This will allow to create tasks that span multiple machines , pass results around. (https://wiki.jenkins-ci.org/display/JENKINS/Plugins)
You can get the ID(s) of the job that triggered your job via the API and fetch their status.

Resources