Delete TFS workspace mapping after jenkins job execution - jenkins

I am trying to run a Jenkins job that uses TFVC plugin. We earlier had a problem with length of the TFS workspace names (exception for length > 75 chars). In order to address this we made a change in the TFVC configuration.
Default workspace name in TFVC: Hudson-${JOB_NAME}-${NODE_NAME}
After change: Hudson-${JOB_NAME}
Post this change the job had one successful run. We are unable to run this repeatedly as the TFS workspace created during the successful run was not deleted. and is throwing the following exception:
FATAL: hudson.remoting.ProxyException: com.microsoft.tfs.core.exceptions.TECoreException: The workspace scanAPI;tfsjenkins already exists on computer ip-XX-XX-XX-XX.
I have tried deleting the Jenkins workspace in pre/post build steps. This has no impact on the TFS workspace.
Additional information: The jobs are being run on a linux node and hence I am unable to run windows commands

You can use Post Build script that use tf.exe to delete the TFVC workpsace, with the delete command.
Create a global environment variable to be able to access the TF.exe easier. for example:
Note: the path to tf.exe it depend to which Visual Studio is installed in the Jenkins machine.
Add a Windows batch command from the scripts menu with the following command:
%TFS% workspace /delete /noprompt /collection:”https://tfs.codeplex.com:443/tfs/TFS27″ “Hudson-%JOB_NAME%;snd\7astlivec_cp”
Replace the URL with your TFS Server URL and change snd\7astlivec_cp with your TFS user. The command is going to delete the newly created TFS workspace.
Another option is to add tf.exe. location to the machine PATH variable and use it directly: tf workspace /delete .......

Update
For Linux, you should be able to use this through team explorer everywhere. It also include a tf command line.
Take a look at Setting up a workspace using Team Explorer Everywhere on Linux
Should be similar on Linux.
Instead of creating the default workspace by specifying workspace name in UI setting, you could also use a Windows batch command to handle this process.
If you want to delete workspace, just add a new post build step, a cleanup command could be added to delete the previously created TFS workspace.
%TFS% workspace /delete /noprompt /collection:"{your-tfs-team-project-collection-url}" "Hudson-%JOB_NAME%;{your-domain-user-name}"
More details your could kindly refer this step-by-step tutorial Jenkins Get Source Code By Specific TFS Changeset

Related

Jenkins Job Failure Due To Old TFS Workspace

I have a jenkins job that is failing due the error:
An error occurred: The path XXXX is already mapped in workspace YYYY;ZZZ\ServiceAccount.
The service account that is being referenced is from the domain that the TFS server was recently migrated from.
I have tried various fixes to remove this workspace mapping:
Run "tfs workspace -delete "{workspace name};ZZZ\ServiceAccount" -noprompt -server:tfs server -login:YYY"
This fails with the error message "ZZZ\ServiceAccount" is not a valid account. It is true that the account does not exist on the new domain.
Run "tf workspaces /remove:*" to remove all workspace caches. This completes.
Deleted the contents of "%AppData%\Local\Microsoft\Team Foundation\4.0\Cache".
Our TFS Server is running version 2013.
Tried using Team Foundation Sidekicks 2013. But this also fails to find any workspaces for "ZZZ\ServiceAccount".
I have tried on both the slave where the Jenkins job is run, and the Jenkins master.
Where might this workspace mapping be cached and how can I remove it now that the domain and user no longer exist?
Thanks in advance.
You can try the workaround provided by Jake Wallace in this case:
An easy workaround that has been used for several pipelines that have
run into this issue is to rename the pipeline. Not ideal but can add a
suffix or prefix to the pipeline until you run into the issue again.
In addition, after you clean all entries in %LOCALAPPDATA%\Microsoft\Team Foundation\4.0\Cache\*.*, did you restart the Jenkins agent and then rerun the build?

Check-in files in a TFS vNext build

I need to check-in some files generated during a TFS 2017 vNext build.
In my old XAML build I could easily do that using the tf checkin command since the files reside in the build workspace.
In the new vNext build, The workspace owner is Project Build Service or Project Collection Build Service, even when I configure the agent to run under a different account.
Any way to perform the check-in on the build workspace? Or somehow configure who owns that workspace?
There are Add and Check in changes tasks in TFVC Build Tasks extension.
Regarding no pending changes, you need to add the files to the list of pending changes for the workspace by calling TF add command.
It's not a recommend way to check-in/ modify source code during a build pipeline. If you really want to do this, you could edit the build worspace files and use tf commands in custom/powershell task eq:
cd $env:BUILD_SOURCESDIRECTORY
$TFFile = Get-Item "C:\Program Files (x86)\Microsoft Visual Studio 1x.0\Common7\IDE\TF.exe"
$tfOutput = [string]( & $TFFile.FullName checkin /noprompt /override:"***NO_CI*** New version is $newVersion." /comment:"***NO_CI*** New version is $newVersion." 2>&1)
Another way is installing TFS Power Tool and use the Windows PowerShell Cmdlets to check in the files. Refer to this link for more details: PowerShell and TFS: The Basics and Beyond

how to checkin EAR to TFS from jenkins workspace

I have created a Jenkins Job which gets code from TFS and builds the EAR at jenkins default workspace (/opt/jenkins/lib/jobs/PROJECTNAME/workspace/dist).
After the EAR is built, I need to checkin this EAR to TFS, but I didn't see any option to checkin artifacts to TFS.
If you are using TFS Plugin in Jenkins to get the code from TFS. It will create a workspace when get the code. Settings as following:
And then, you can add an "Execute Windows batch command" build step to check in files to TFS after EAR build via TF Command.
Now you'll get the files been checked in:
In the command, I check in the folder generated by the build, you can also just add the items generated by the build.
might be helpful.
Visit how to add an existing solution to tfs
Another add solution to TFS server

TFS command line not working through Jenkins

I am new to this, so please excuse my ignorance if this is something very basic.
I use the following commands in the 'Execute windows Batch command' tab in a Jenkins job -
cd "C:\Data\Projects\<project name>\00_Mainline"
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\TF.exe" get "$/Analytical/<project name>/00_Mainline" /force /recursive
This works well if I type it in a command prompt; but when this is run from Jenkins, I get the following error -
Unable to determine the workspace. You may be able to correct this by running 'tf workspaces /collection:TeamProjectCollectionUrl'.
After going through several threads in this site, I added
pushd "C:\Data\Projects\<project name>\00_Mainline"
to the top of the script. But this didn't help either.
Any ideas?
Regards,
Sudhi
Since you haven't specified otherwise I'm going to assume Jenkins is running under a different user account, in which case there may not be a workspace for the Jenkins account.
In any case, in Jenkins you might need to use the tf workspace command to create a new workspace, and then the tf workfold command to set up a folder mapping before you can do the tf get.

how to remove TFS workspace mapping for another user

I am using Visual Studio 2010. Some months back I was using some 'ABC' TFS user to connect to TFS and mapped some folders on my local drive. Now my previous user 'ABC' is gone and I am allotted a new tfs user 'XYZ' to connect to TFS. So naturally I tried mapping some remote folder to my existing local folder and I got the following error.
"The working folder 'Some_Local_Path' is already in use by the workspace WORKSPACE_NAME:USER_NAME on computer 'MACHINE_NAME'"
I have tried removing the cache folder contents but the same error (C:\Users{UserName}\AppData\Local\Microsoft\Team Foundation\3.0\Cache).
I have also tried running the commonly found TFS command (tf workspaces /remove:*) to delete the caches for all worksapaces but still the same error.
When I try to edit my workspace, it shows source control and local folders for my current user 'XYZ' but what I want is to remove the folder bindings for my previous user 'ABC'. How can I achieve that?
You need to get your TFS administrator to delete the workspace if you have no access to the account.
You may be able to do it by calling "tf workspace" with the explicit user specified, but you need "manage other users workspace" permission. TF Sidekicks uses the same commands so would require the same permission. It is a TFS admin productivity tool.
https://msdn.microsoft.com/en-us/library/y901w7se(v=vs.100).aspx
You can use:
tf workspace /delete "WORKSPACENAME;PREVIOUSUSERACCOUNT"
I had a similar issue and could not install sidekick because I'm running VS 2k17.
I was unable to delete the workspace because it kept telling me it could not find the workspace.
tf workspaces /computer:* /owner:*
This would list all of my workspaces and the owners. So I would try
tf workspace /delete myWorkSpaceName;Bob Smith
TF14061: The workspace myWorkSpaceName;Bob Smith does not exist.
That is the exact workspace and owner name I was getting in the original workspace listing above.
The solution was to ask for xml format
tf workspaces /computer:* /owner:* /format:xml > c:\temp\workspaces.xml
(Note: Output to file is optional, but recommended)
This gives workspace owner aliases and one of them was a long name with a guid and the account email. This finally worked:
tf workspace /delete myWorkSpaceName;aabe3ec12-1254-4956-b1ee-3fb26506931e\bsmith#myDomain.onmicrosoft.com
It asked for a confirmation and finally deleted my orphaned workspace.
If you have administrative rights to the collection you can use the TF command located in the Visual Studio\Common7\IDE directory to do this without having to install another tool.
First list the workspaces associated with the user:
TF workspaces /collection:"http://tfsserver:8080/tfs/collection_name" /owner:owner_id
This will return the list of workspaces owned by the user and computer they are associated with
To delete a named workspace:
TF workspace /delete workspacename;owner_id /collection:"http://tfsserver:8080/tfs/collection_name"
First of all you need to install TFS Sidekick (you can download it from http://www.attrice.info/cm/tfs/) In installation phase choose Integrated with IDE mode
Then a new menu will be added to Visual Studio as below
Menus -> Tools -> TeamFoundation Sidekick
Then open workspace sidekick
Search for the 'ABC' user and then you can delete his workspace
Open Source Control Explorer(View->Other Windows->Source Control Explorer)
On toolbar pane there will be Workspace combo. Choose from that combo Workspacess..
Edit
Remove
1.First we will check the list of workspaces from VS 2015 Developer command prompt,
Ex - tf workspaces /owner:*
2.Now we will get the xml format from VS 2017 Developer command prompt which will have ‘owner id’ for the particular workspace,
Example 1 - tf.exe workspaces /owner:* /computer:ComputerName /collection:https://YOUR-TFS-URL.visualstudio.com /format:xml
3.We can now delete the workspace for the particular user,
Example 1- Tf workspace /delete ComputerName;email#email.com /server:"https://URL.visualstudio.com"
OR
Example 2 - Tf workspace /delete ComputerName;1e178c77-bb8b-6f05-bf99
/server:https://URL.visualstudio.com
(Where 1e178c77-bb8b-6f05-bf99 is ID of the workspace which you get from Step 2 XML format)
4.Again we will check the list of workspaces from VS 2015 Developer command prompt,
Ex - tf workspaces /owner:*
Steps to delete workspaces from the TFS server:
Open Visual Studio Developer Command Prompt.
Goto Program Files (x86) folder, depending on the installed visual studio select "Microsoft Visual Studio 12.0" folder. Here this I have selected it because I am having Visual Studio 2013 installed on my machine. Add this path in the command prompt. Add like
"cd C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE"
Note: If you have an access of the TFS server from the network then you can try it from any machine where Visual Studio has been installed or from the same TFS machine server if it has Visual Studio there.
Check a list of workspaces under specific collection. Type below command to get the workspaces under one collection.
cd C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\tf workspaces /server:http://{TFSServername}:8080/tfs/{CollectionName} /owner:*
In the above command replace {TFSServername} this with your TFS servername or the Server IP.
In the above command replace {CollectionName} this with actual TFS Collection Name.
How to remove workspaces under specific collection. Check below command for the same.
tf workspace /server:http://{TFSServername}:8080/tfs//{CollectionName} /delete “{workspacename};{owner}”
Replace {workspacename} this with "Workspacename” which is there in the list of workspaces.
Replace {CollectionName} this with TFS Collection Name.
E.g. tf workspace /server:http://{TFSServername}:8080/tfs//{CollectionName} /delete {Enter Exact Workspacename};{Enter ExtactOwnerName}
I had the same issue that after deleting the workspace using tf delete command, I was getting the error that the workspace was already mapped! Then I found out tf delete workspace command leaves the job incomplete so you have to also delete it from the cache manually as was suggested here:
https://community.dynamics.com/365/financeandoperations/b/dynamicsaxinsightbyanas/posts/d365fo-the-path-is-already-mapped-in-workspace
I end up in same issue after the person who setup Jenkins left our company. He had setup workspace and TFS checkout was using alternate authentication. As his credentials are removed from msdn, TFS checkout started choking.
Following LarryG's solution helped to remove the workspace. Only difference is, I just used the windows live ID of the person, who left the company.
tf workspace /delete myWorkSpaceName;windowsliveID

Resources