Jenkins: Get Source Code By Specific TFS Changeset
Learn some neat tricks using the TFS plugin for Jenkins.
Join the DZone community and get the full member experience.
Join For Freethe “ 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.
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}"
Published at DZone with permission of Anton Angelov, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments