Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Jenkins: Get Source Code By Specific TFS Changeset

DZone's Guide to

Jenkins: Get Source Code By Specific TFS Changeset

Learn some neat tricks using the TFS plugin for Jenkins.

· DevOps Zone ·
Free Resource

Learn more about how CareerBuilder was able to resolve customer issues 5x faster by using Scalyr, the fastest log management tool on the market. 

The  “Jenkins CI” Series is all about useful techniques and approaches for creating jobs for building your projects or running your tests. Sometimes it is required 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 particular stable changeset number.

Jenkins TFS Specific ChangesetJenkins TFS Specific 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.

Default Source Code ManagementDefault Source Code Management

 

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.

Add Windows Batch Command JenkinsAdd Windows Batch Command Jenkins

 









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.

Jenkins Job Changeset Number ParameterJenkins Job Changeset Number Parameter


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}"


Find out more about how Scalyr built a proprietary database that does not use text indexing for their log management tool.

Topics:
jenkins ,devops ,tfs ,source code ,scm

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}