Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / .NET

Jenkins Get Source Code By Specific TFS Changeset

4.87/5 (11 votes)
26 Jul 2015Ms-PL2 min read 13.6K  
Shows how to create a Jenkins Job that gets the source code by specific changeset number. Uses a parameter to run the job in two modes.

The “Jenkins CI” Series, are all about useful techniques and approaches for creating jobs for building your projects or running your tests. Sometimes, it is required for all of your smoke tests to run regularly and to be green. However, there is always a new development, and it is possible to introduce bugs in the tests. Most of the times you want to run these tests only when you are 100% sure that the new changes won’t lead to broken builds or some unexpected failures. The solution in such cases is to download the source of the tests by a particularly stable changeset number.

So Far in the Jenkins Series

  1. Integrate Jenkins with Team Foundation Server
  2. Integrate Jenkins and GitHub
  3. Configure Jenkins MSTest Tests Execution
  4. Delete TFS Workspace in Jenkins Job
  5. Output MSTest Tests Logs To Jenkins Console Log
  6. Integrate Jenkins with MSBuild and NuGet
  7. Create Jenkins Job for Creating NuGet Packages
  8. Visual Studio Test Agents Cleaning Scripts
  9. Jenkins Get Source Code By Specific TFS Changeset

Get Source Code By Specific TFS Changeset

The default way to download your code from TFS is through the usage of the Source Code Management Tab. The Team Foundation Server option is added by the installation of the Team Foundation Server plug-in.

However, if you have used the plugin, you know that there isn’t a way to configure it to download the source code by a particular changeset number. So to download the source code by a specific changeset, a new Windows batch command should be added.

In the newly added box, the following commands should be added:

BAT
%TFS% workspaces -format:brief -server:{your-tfs-team-project-collection-url}
%TFS% workspace -new Hudson-%JOB_NAME%-MASTER;{your-domain-user-name} 
-noprompt -server:{your-tfs-team-project-collection-url}
%TFS% workfold -map $/{tfs-path-to-your-sln} C:\Jenkins\jobs\%JOB_NAME%\workspace\ 
-workspace:Hudson-%JOB_NAME%-MASTER -server:{your-tfs-team-project-collection-url}
%TFS% get $/{tfs-path-to-your-sln} -force -recursive -version:%ChangesetNumber% -noprompt
%TFS% history $/{tfs-path-to-your-sln} -recursive -stopafter:1 -noprompt 
-version:%ChangesetNumber% -format:brief -server:{your-tfs-team-project-collection-url}

You only need to change the snippets inside the curly brackets “{}” with your information. The code is going to create a new TFS workspace with the name of your Jenkins job. Next, it is going to generate a mapping to your TFS Project path to a local folder. Finally, it is going to download the source code by specific changeset number. It is passed to the command by the parameter %ChangesetNumber%. So you need to create a new job’s parameter of type string with the name ChangesetNumber.

Get Latest Code Windows Batch Command

It is possible to use similar commands to get the latest source code.

BAT
%TFS% workspaces -format:brief -server:{your-tfs-team-project-collection-url}
%TFS% workspace -new Hudson-%JOB_NAME%-MASTER;{your-domain-user-name} 
-noprompt -server:{your-tfs-team-project-collection-url}
%TFS% workfold -map $/{tfs-path-to-your-sln} C:\Jenkins\jobs\%JOB_NAME%\workspace\ 
-workspace:Hudson-%JOB_NAME%-MASTER -server:{your-tfs-team-project-collection-url}
%TFS% get $/{tfs-path-to-your-sln} -force -recursive -noprompt
%TFS% history $/{tfs-path-to-your-sln} -recursive -stopafter:1 -noprompt 
-format:brief -server:{your-tfs-team-project-collection-url}

The commands are identical with a small difference- the absence of the %ChangesetNumber% argument.

Combine Get Latest Code and Get by Specific Changeset Number

If you want to create a multipurpose Jenkins job, capable to run in two modes- get latest and get by specific changeset number. The first mode is going to be triggered if the %ChangesetNumber% is left empty. To accomplish that, a small tweak is needed to the previously mentioned commands.

BAT
IF "%ChangesetNumber%" == "" ( 
%TFS% workspaces -format:brief -server:{your-tfs-team-project-collection-url}
%TFS% workspace -new Hudson-%JOB_NAME%-MASTER;{your-domain-user-name} 
-noprompt -server:{your-tfs-team-project-collection-url}
%TFS% workfold -map $/{tfs-path-to-your-sln} C:\Jenkins\jobs\%JOB_NAME%\workspace\ 
-workspace:Hudson-%JOB_NAME%-MASTER -server:{your-tfs-team-project-collection-url}
%TFS% get $/{tfs-path-to-your-sln} -force -recursive -noprompt
%TFS% history $/{tfs-path-to-your-sln} -recursive -stopafter:1 -noprompt 
-format:brief -server:{your-tfs-team-project-collection-url}
) ELSE ( 
%TFS% workspaces -format:brief -server:{your-tfs-team-project-collection-url}
%TFS% workspace -new Hudson-%JOB_NAME%-MASTER;{your-domain-user-name} -noprompt 
-server:{your-tfs-team-project-collection-url}
%TFS% workfold -map $/{tfs-path-to-your-sln} C:\Jenkins\jobs\%JOB_NAME%\workspace\ 
-workspace:Hudson-%JOB_NAME%-MASTER -server:{your-tfs-team-project-collection-url}
%TFS% get $/{tfs-path-to-your-sln} -force -recursive -version:%ChangesetNumber% -noprompt
%TFS% history $/{tfs-path-to-your-sln} -recursive -stopafter:1 -noprompt 
-version:%ChangesetNumber% -format:brief -server:{your-tfs-team-project-collection-url}
)

Delete TFS Workspace

As a new post build step, a cleanup command should be added to delete the previously created TFS workspace.

BAT
DELETE %TFS% workspace /delete /noprompt /collection:"{your-tfs-team-project-collection-url}" 
"Hudson-%JOB_NAME%-MASTER;{your-domain-user-name}"

The post- Jenkins Get Source Code By Specific TFS Changeset appeared first on Automate The Planet.

All images are purchased from DepositPhotos.com and cannot be downloaded and used for free.
License Agreement

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)