Jenkins is a lightweight build automation tool. Jenkins does not include out-of-the-box support for TFS, so the following steps should be used in order to automate a TFS –managed project build and publish.
Jenkins uses build as a term for copying the files from source control. We will use “build” in this sense throughout the document, and publish to mean copying the files to various other directories other than the “build” directory.
Jenkins requires a plug-in to connect to TFS and check for updates to a project. The following steps walk through installing this plug-in.
Step 1: Log in to Jenkins. Then navigate to “Manage Jenkins” > “Manage Plugins”
Step 2: On this screen, select “Available” then enter “Team Foundation Server” into the search box at the top-right of the screen. Check the install box for the plugin, and click “Install without restart”.
Now that TFS support has been added to Jenkins, you can create a build item for a TFS project.
Step 1: Log in to Jenkins.
Step 2: Select “New Item”. On the next screen, give your build item a name, then select “Freestyle Project” and press “OK”.
Step 3: On the next screen, scroll down to “Source Code Management”. Select “Team Foundation Server” and fill in the information up to but not including the “User password”. We will generate credentials for the password field.
If your TFS is onpremise then the Server URL will be http://tfs-server-name:8080/tfs/
Also, while using TFS online, set the Server URL to http://yourdomainname.visualstudio.com/
For the user password, we will generate credentials using Visual Studio Team Services as Jenkins suggests.
Step 3.1: Log-in to Visual Studio Team Services. You should see a screen like shown below.
Step 3.1: In the top right, click on your user name. Select “My profile” from the drop-down menu.
Step 3.3: On the next screen, select the “Security” tab.
Step 3.4: On the security screen, select “Personal access tokens” from the left sidebar. Press “Add” to create a new token.
Step 3.5: On the “Create a personal access token” screen, give your new access token a name, set the expiration time, and select “All scopes” under “Authorized Scopes”. Press “Create Token” at the bottom of the screen.
Step 3.6: The previous step should being you to a screen listing your personal access tokens. One of those tokens will be the token you have just created. Under “Actions” for this token, copy the token manually or press the “Copy Token” link. YOU MUST DO THIS NOW. Note: There will be text in the area with the black box.
Now that you have generated credentials using Visual Studio Team Services, you need to use those credentials in Jenkins.
Step 4: Paste the token you copied in the previous step into the password box from step 3.
Step 5: Select the “Advanced…” button underneath the “User password” field. In the options below, uncheck “Use update”. This will ensure that all files in the latest build are overwritten with any updated files from TFS.
Step 6: Scroll down until you see the “Poll SCM” checkbox. Check this checkbox. Below this, fill in the “Schedule” textbox by following the instructions given by Jenkins. To view these instructions, press the help arrow to the far right of the screen.
Step 7: Scroll down to the “Build” section. Press “Add build step”. Select “Execute Windows batch command” form the drop-down options.
Step 8: Enter the location of your .cmd or .bat script file in the “Command” textbox. This script will handle copying files into publish directories and building the project (In the Visual Studio sense). An example script is given below. Press “Save”.
The script below is an example of some of the basic actions that your build script should take. This script copies the current code into a folder whose name is the current day (MMDDYYYY). It then builds and publishes the Dev branch into a publish folder which IIS points to. Read the comments (highlighted in green) for more information.
REM Get the date string to use as folder name (MMDDYYYY) set dateStr=%date:~4,2%%date:~7,2%%date:~10,4% REM The location of this script file and past builds set rootPub=E:\Publish\InergyLab REM Pub = location of this file\MMDDYYYY set pub=%rootPub%\%dateStr% REM Create the publish directory mkdir "%pub%" REM The location of the Jenkins “Build” (actually a TFS get latest) set rootBuild=E:\AutoBuild\Jenkins_workspace\InergyLab Continuous Integration\workspace REM Copy all of the Jenkins build files (Source files from TFS) into the publish directory (copy of latest build) xcopy "%build%" "%pub%" /E /Y /C /J /Q REM The location of the MSBuild executable set msBuildLoc=C:\Program Files (x86)\MSBuild\12.0\Bin REM Use MSBuild to build and publish the project code files for the web project using the provided publish profile. "%msBuildLoc%\MSBuild" "%pub%\Dev\Inergy\Inergy.csproj" /p:DeployOnBuild=true,PublishProfile="%rootPub%\publish.pubxml"
The script above requires a publish profile to be used in conjunction the MSBuild executable call. An example publish script is below, this should work for most web projects. If it does not work, follow the link provided by Microsoft in the comments, or generate a publish profile in Visual Studio (right-click on the web project and select publish).
"1.0" encoding="utf-8" xml version=<!-- This file is used by the publish/package process of your Web project. You can customize the behavior of this process by editing this MSBuild file. In order to learn more about this please visit http://go.microsoft.com/fwlink/?LinkID=208121. --> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <WebPublishMethod>FileSystem</WebPublishMethod> <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration> <LastUsedPlatform>Any CPU</LastUsedPlatform> <SiteUrlToLaunchAfterPublish /> <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish> <ExcludeApp_Data>False</ExcludeApp_Data> <publishUrl>E:\Websites\WebsiteFolder</publishUrl> <DeleteExistingFiles>True</DeleteExistingFiles> </PropertyGroup> </Project>