How can I report the current tfs user from tf.exe? - tfs

How can I report from my build scripts who the current tfs user is?
I'm setting up automated builds with TFS 2013 and VSO and I'm having workspace errors.
I'm unable to checkout files through the build script.
I'd like to know what the user is running the tf.exe command (I've already had issues with the user running the build being automatically set to a Network Service user, who has not permissions)

In any of the pre-/post-script print the USERNAME variable. In Powershell
Write-Output "Running as '$env:USERNAME"
In a batch file
echo Running as %USERNAME%

Related

Using ssh credentials in post-build of Jenkins matrix job

We have a Jenkins matrix job with "SSH Agent" enabled in "Build Environment" with SSH credentials and a post-build action of "Execute Scripts On Matrix" with a shell command that runs ssh expecting to use the credentials stored by ssh-agent.
We recently upgraded from Jenkins v2.249.3 to v2.263.1 (and potentially upgraded some plugins at the same time, though I don't believe that we upgraded any of the ssh-related ones.) The aforementioned shell command now fails because it no longer has access to the ssh credentials it requires.
Comparing the build logs, we see a new call to ssh-agent -k in the Jenkins v2.263.1 parent job log immediately after the matrix children complete and before "[PostBuildScript] - [INFO] Executing post build scripts." that wasn't present with Jenkins v2.249.3.
It would appear that the agent is being killed before running the post-build operations by Jenkins v2.263.1 whereas it wasn't with Jenkins v2.249.3. I was unable to find a setting that controls this.
I entered JENKINS-64394 for this, but I wasn't really sure which components to label it with which I suspect mean that the right people haven't seen it. Does anyone here have any ideas?

Can I extract TFS Service hook trigger message in Jenkins

I am trying to trigger jenkins via TFS service hook, I want to use jenkin to extract out check in information to create a log using powershell script. I have it set up to trigger the jenkins on check in. but I can not figure out a way to parse message info sent from TFS service hook. Looking at https://github.com/jenkinsci/tfs-plugin/blob/master/README.md I can see there are few environment variables created on trigger but I want to extract some of the check in information as in username who checked in and tfs id it was checked in against etc. TFS_USERNAME only record the username of the account that is configured to access tfs in Jekins
You can use TFS REST API to get a changset on Jekins side. The API is as below:
GET http://({server:port})/DefaultCollection/_apis/tfvc/changesets/{id}?api-version=1.0
There isn't any way to extract the hooks that Jenkins received in the build trigger. However, if the Jenkins job download the latest source code to local after the job is triggered, the information you want like the user who check in the changes is already in local. You can simple run tf changeset /latest /noprompt to get this information. And you can also install TFS Power Tools and then create a powershell script to get these information via TFS Powershell Command. For example:
add-pssnapin Microsoft.TeamFoundation.PowerShell
$cs = Get-TfsChangeset -Latest
Write-Host $cs.CommitterDisplayName
Write-Host $cs.Committer

Copying files from one directory to another in jenkins

I'm trying to move my test results onto a public webpage.
I set up a "Post-build action" > "Post build task" to execute a script.
The script is:
cp -r /var/lib/jenkins/jobs/instrumentation-tests/htmlreports/HTML_Report/ /var/www/html/test/
Jenkins outputs: cp: directory /var/www/html/test does not exist
If I'm logged in as user jenkins on the linux machine running jenkins, I can navigate to the source and SEE that there are files there currently. I can navigate to the destination and see that it DOES exist.
Also, I tried running that command from the terminal as the jenkins user, and the cp completed successfully.
As you mention in the comments, your test results are being generated during a build on a slave machine. Therefore you're trying to copy the files to /var/www on the slave, not the Jenkins master server.
There are a few different ways you could solve this:
Ensure that the build happens on the master server.
You can do this by choosing "Restrict where this project can be run" on the job configuration page, and entering "master" in the text field.
This ensures that your file copying will work at the end of a build, assuming that the Jenkins user has write permissions.
Use the Publish over SSH plugin to publish the files directly to /var/www on the Jenkins master, from any other machine.
This has the advantage that it will work, no matter which Jenkins build machine the build takes place on.
You could also split up the job into two parts: one job to run the tests and generate the results, and another job to publish the test results.
The first job could run on any machine, and would save the generated HTML files using the "Archive the artifacts" post-build action. It would then start the second job — via the "Build other jobs" post-build action — in order to do the publishing.
The second job could use either of the above approaches: either publish using SSH, or ensure that it runs on the master and use a simple shell step with cp.
In both cases, you would use the Copy Artifact plugin to copy the archived HTML files from the upstream build.

Execute Shell script from workspace on remote machine after build successful (Jenkins)

The scenario is - I have a job A which runs my ant script and packages the artifact's for me.
I am also using parametrized Triggered plug in to Trigger my "Job B" which will deploy my artifact on remote machine.
The job A is working fine and also Job B.
The tasks that i have to perform with Job B are
GIT checkout (which contains my deployment scripts) (successfully doning).
Copying artifacts from previous build to Remote machine. (successfully doing)
Run shell script on remote machine(script present in workspace folder )- Facing issues.
I browsed various plug ins for the same but no one is allowing me to run shell script after , "SCP to remote machine" which is present in Post build action.
I would like to execute the same sequence, however if you guys have any other suggestions please share.
Thanks in Advance.!
As part of Publish Over SSH Plugin, you can execute a script after the files had been copied over.
Under Post-build Actions
Add Send build artifacts over SSH
Select a preconfigured server (done in global configuration)
Select files to copy from workspace
Enter Exec command
If one of the files you copy is your shell script, you can enter it here as an "exec command"
To solve my query i used Jenkins SSH Plugin. This provides a configuration tab where i can add multiple hosts and after that used them in my job level configuration.
Link to Plugin
you get privilege to execute shell script on remote host as pre-build step or post build step.
updated the path of publish over ssh it worked for me

Building code from Jenkins using TFS

I am trying to use Jenkins to build my code which resides in a https://*.visualstudio.com/DefaultCollection.
I have tried to manually check the code out using a script in jenkins e.g.
tf workspace /delete JENKINS /noprompt /collection:https://*******.visualstudio.com/DefaultCollection /login:mylogin#myaccount.com,mypassword
tf workspace /new JENKINS /noprompt /collection:https://*******.visualstudio.com/DefaultCollection /permission:Private /login:mylogin#myaccount.com,mypassword
tf workfold /workspace:JENKINS /s:https://*******.visualstudio.com/DefaultCollection /map "$/MyProject" "%WORKSPACE%"
tf get /force /recursive
This runs fine from the command line but fails in Jenkins with
TF30063: You are not authorized to access
Jenkins is running as Local System Account. I have tried the plugin for jenkins which no joy either. This also failed on authentication.
Even if you have no intention of using it, I'd install and configure the TFS build controller software. This will create the authentication link between the cloud instance and the system profile. Jenkins, running in that user profile context, should then be able to issue TF commands and create workspaces.
That said, I'd also create a build service account and not use SYSTEM.

Resources