Click here to Skip to main content
13,898,496 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

9.7K views
11 bookmarked
Posted 26 Jul 2015
Licenced Ms-PL

Jenkins Get Source Code By Specific TFS Changeset

, 26 Jul 2015
Rate this:
Please Sign up or sign in to vote.
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:

%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.

%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.

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.

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)

Share

About the Author

Anton Angelov
CEO Automate The Planet
Bulgaria Bulgaria
Anton Angelov is an IT Consultant and Quality Assurance Architect at Innovative Lab. He is passionate about automation testing and designing test harness and tools, having the best industry development practices in mind. In addition, he is an active blogger and the founder of Automate The Planet. He strives to make the site one of the leading authorities in Automation Testing by presenting compelling articles, inspiring ardent discussions amongst the community. He is also one of the most-rated-answer authors of questions about Test Automation Frameworks (WebDriver) on Stack Overflow.

You may also be interested in...

Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190306.1 | Last Updated 27 Jul 2015
Article Copyright 2015 by Anton Angelov
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid