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
- Integrate Jenkins with Team Foundation Server
- Integrate Jenkins and GitHub
- Configure Jenkins MSTest Tests Execution
- Delete TFS Workspace in Jenkins Job
- Output MSTest Tests Logs To Jenkins Console Log
- Integrate Jenkins with MSBuild and NuGet
- Create Jenkins Job for Creating NuGet Packages
- Visual Studio Test Agents Cleaning Scripts
- 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