April 09, 2015 Jenkins CI in MVC.NET Part 1: Install, Setup and Build More on companies’ integration of continuous integration servers in their work process and their benefits. Continuous integration is widely used term today. The main reason for this is because more and more companies are integrating continuous integration servers in their work process. The benefits of this can be many. From faster issue detection, quick builds and deploys, project risk management, open tasks track, etc. All benefits and good practices can be found in books like “Continuous Integration” by Paul Duvall, Steve Matyas, and Andrew Glover or “Continuous Delivery” by Jez Humble and David Farley. For that reason there is a lot of tools created to resolve this tasks. Tools like “Team Foundation Server” and “Bamboo” have good integrated solutions, but they are paid and not all companies are willing to use them. Another alternative is Jenkin CI which is pretty popular mainly because it is very flexible and can perform wide array of tasks. But this flexibility come with the price, that it is not very easy to setup. So in series of blog post I will try to present to you a fairy common and complete setup of Jenkins continue integration process for a single MVC.NET application. The first step is installing the server itself under windows, adding some required plugins and tools in order to build .NET application and creating a new Job that build our application. Installing Go to jenkins-ci.org download and install Jenkins for Windows. After the setup finish it will automatically start it. Note that IIS is not required. Jenkins will run its own server. Visit the https://microsoft.com/en-US/download and install the latest version of Windows Software Development Kit. Last location is here. Go to your Jenkins manage page and make sure the Subversion plugin and/or the Git plugin are installed, so you can download your projects. In order to install git plugin you need to download git itself first. Go to http://git-scm.com/download and download the latest version. After you install git plugin you will need to set the Git location in the configuration. Example under git installations you may enter Name: “Default” and Path: “c:/Program Files (x86)/Git/bin/git.exe” if this is where you installed git. Git plugin install ‘ssh credentials’ plugin. It is a good idea to crate a ssh user what jenkins is going to use when connecting git. When using the plugin place your .ssh folder on the main drive root (Example c:.ssh). Go to the Microsoft download page and download the latest “Microsoft build tools” and install it. The ‘Microsoft build tools’ include MSBuild. After the install in the “Program Files (x86)” folder you should have MSBuild folder. Go to System -> Advanced system settings -> Environment Variables and set the PATH to point to the MSBuild folder (ex: C:Program Files (x86)MSBuild14.0Bin). After Path is set you need to restart jenkins. You can do this by navigating to “YourJenkinsDomain/restart” page. Download the NuGet executable. This tool is used to restore NuGet packages used by the project. NuGet is available at http://nuget.org/nuget.exe. It is good idea to hold all tools in one folder so with our example we will use c:Tools to hold all required tools. So the NuGet executable cab be in the ‘c:ToolsNuGetNuGet.exe’ Setup Git I think the best way to use continuous integration with git is to set all developers users to pull from the master branch, but not to be able to push to it. The only user that will be able to push to the master will be Jenkins. In this way on the master branch will always be a stable version and the developers will not use broken builds. This flow will look like this: [You] Pull from master. [You] Create new branch and make changes. [You] Rebase with master and push to origin premaster (Push to master will not be allowed). [CI] Get the latest version from premaster branch. [CI] Build, run test and analyzes tools. [CI] Merge with master. With this scenario on the master branch resign only stable build version. If QA or client require a new build version to be deployed to the test server this can be initialized with a stable build, although component or functional test can be run at this point. Unfortunately, revoking write permissions to master is not possible in Github (It is possible in GitLab by making the master branch protected). We may use other git servers to manage this scenario. Some example config in ‘gitolite’ can be: @all-devs = alice bob carol @ci = jenkins @admin = supermen repo test RW+ = @admin RW master$ = @ci RW premaster$ = @all-devs RW refs/heads$ = @all-devs R master$ = @all-devs With this configuration the users Alice, Bob and Carol can modify the branches and the premaster origin, but can only read from the master. The Jenkins user however can modify the master. Using Git on Developer Machine Let we assume I am “Alice” and I want to start using git and Jenkins in my project. When I clone a repository I can start pulling from the master branch and create branches out of it, but I can not push to the master branch. I need to push to a premaster branch (The branch that Jenkins track) after a change is made. Setup Git at Jenkins In order to make sure Jenkins is up and running we can make it build a project and push it to master. Go to Jenkins page and Go to ‘New Item’. Enter a name and select a project type (Freestyle project is ok for the test). On the ‘Source Code Management’ section choose Git (if you use another VCS setup it here). Set the repository path and credentials. If you use gitolite you can reference this. If you use gitlab you can install the gitlab plugin and use that. Check ‘Poll SCM’. This option will allow Jenkins periodically to check for new commit in version control system. For example ‘H/10 * * * *’ will check for new version every 10 min. At ‘Add post-build action’ select ‘Git Publisher’. Check Push Only If Build Succeeds. Add Banch origin master to push to. You can at this point try to push something to the repository and check if it is merged to master branch. Setup SVN Setup for SVN is straightforward it do not allow update from one branch and commit to another so the only thing you do is update. Build a project with Jenkins Create a project File => New => Project => ASP.NET Web Application. Set some name, for example: “FirstBuild”. Select the version control folder. From the templates select MVC. First thing to do is to clean the project if there is some files left from old build. If you use the (Default) option MSBuild may find the solution file by itself. Otherwise you have to specify Build File. MSBuild Build File: ${WORKSPACE}FirstBuild.sln MSBuild Command Arguments: /t:Clean /v:m /nologo As extra clean you can use bash script that delete all bin and obj folders in the projects. Jenkins CI MSBuild Clean After the clean we need to restore all NuGet packages. You do this with a bash script and accessing NuGet.exe. If your solution is named FirstBuild and you have installed NuGet at the specified folder you can execute the fallowing Bash script to restore your libraries. REM "Restore libraries." "C:ToolsNuGetNuGet.exe" restore FirstBuild.sln Building the project with MSBuild. We can use the options: /t:Build – start a build. /p:Configuration=Debug – build in Debug mode. Use can build in Release mode, also. /v:q – sets the verbosity to quit so the log not to be poluted with too much information. /nologo – remove the some unneeded information from the log file. MSBuild Command Arguments: /t:Build /p:Configuration=Debug /v:q /nologo Jenkins CI MSBuild Debug Before you build it you must remove from all web project (.csproj) files the Visual Studio targets paths. Open project files with text editor and remove the “PropertyGroup” above Imports. It should look like this: <PropertyGroup> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion> <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)MicrosoftVisualStudiov$(VisualStudioVersion)</VSToolsPath> </PropertyGroup> Post-build Actions As extra post-build actions you can use the fallowing plugins Task Scanner Plugin Plugin that scan for open task. In order to scan for tasks you need to specify a file scan pattern. This is one example that scan all .cs files and the javascript files in the “Scripts” folder, except the modernizer and jquery. Files to scan: **/*.cs, FirstBuildScripts*.js Files to exclude: FirstBuildScriptsmodernizr-2.6.2.js, FirstBuildScriptsjquery-2.1.1.js Worksplace Cleaner Plugin The plugin can clean up the workplace after build finish. If you use this plugin and remove the /obj and /bin folders, MSBuild cleanup is not required. References http://www.yegor256.com/2014/07/21/read-only-master-branch.html http://devsops.blogspot.com/2013/01/gitlab-jenkins-and-puppet-playing.html Tags DevelopmentSecurity Share Share on Facebook Share on LinkedIn Share on Twitter Share Share on Facebook Share on LinkedIn Share on Twitter Sign up for our monthly newsletter. Sign up for our monthly newsletter.