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. Annika Hey Design Principal Atanas Atanasov Software Development Manager – Agile Frameworks Björn Stansvik Founder & Chief Executive Officer Daniela Nazim MentorMate Alumni Dimitar Dobrev MentorMate Alumni Craig Knighton Chief Operating Officer Eleonora Georgieva Global VP, Delivery George Dormishev System Administration Manager Ivaylo Kostadinov Director, Software Engineering - .NET Jamie Bolseth MentorMate Alumni Jay Miller President Jeni Kyuchukova Director, Quality Engineering Jessica Anderson VP of Finance and Administration Liz Spolyar Global Director, Continuation Engineering Nick Curran Technical Architect Nikolay Lyubchev Global Director, Talent Acquisition, MentorMate Stefan Tsvyatkov Director, Software Engineering - Mobile Stefan Tzanev Chief Financial Officer Vesselin Dobrev General Manager (Bulgaria) Sylvia Vassileva Software Development Manager - Spok Filip Gajtanovski Software Development Manager - Storyworks Krasimir K. Nikolov VP of Technology Katherine Kelly Director of Operations (USA) Carrie Siewert Strategic Account Manager Brady Swanson Global Director, Marketing Eve Poeschl MentorMate Alumni Ryan Peña MentorMate Alumni Vassil Vassilev Software Development Manager - .NET Pavel Petrov Director, Software Engineering - LAMP&FE Ivan Peev Senior Technology Manager Bob Reuss MentorMate Alumni Vera Kasapova QA Manager Greta Yamacheva QA Manager Robert Samuelsson General Manager (Sweden) Kyle Simmons Solutions Architect Robin Thomas Solutions Architect Nataliya Naydenova MentorMate Alumni Adam Malone Alexander Dimitrov Enterprise Architect Andrea Kates CEO, LaunchPad Central Andrew Eklund CEO, Ciceron Andrew Marinov Angel Nikolov MentorMate Alumni Anurag Shukla Aron Wolde MentorMate Alumni Ashley Goodridge Office Assistant Benjamin Gramlich MentorMate Alumni Chris Black MentorMate Alumni Christa Haeg MentorMate Alumni Colin Lee MentorMate Alumni Deyan Stoynov MentorMate Alumni Dimitar Danailov MentorMate Alumni Dobrinka Tabakova Doug Leatherman Emily Genco MentorMate Alumni Fanka Vassileva Gabriela Zagarova MentorMate Alumni Gary Conkright CEO, physIQ Gary Fingerhut Executive Director, Cleveland Clinic Innovations Gavin Finden MentorMate Alumni Georgi Graham Klang Hyusein Hyuseinov Senior Automation QA Ian Good Global VP, Operations Iva Jack Cosentino James Williams John Byrne Kaloyan Stoilkov MentorMate Alumni Kosta Hristov Krasimir Gatev Senior Android Developer Lazar Petrakiev Lyubomir Dobrev Senior .NET Developer Lubomir Velkov Marin Yotovski Mark Smith MentorMate Alumni Martin Dimitrov MentorMate Alumni Martin Kalyonski Mike Hagan MentorMate Alumni Nikolay Andonov Nikolay Arhangelov Riley Panko Guest Contributor Roger Ferguson MentorMate Alumni Ryan Sysko Chairman, WellDoc Ryan Blake MentorMate Alumnus Sarah Rockholt MentorMate Alumni Sean McDevitt CEO, Sensei Siyana Slavova Stanislas Walden MentorMate Alumni Stanislav Atanasov Stanislava Bogdanova MentorMate Alumni Stefanie Trimble MentorMate Alumnus Stephen Fluin Stoyan Stoyanov MentorMate Alumnus Tessa Cacek Staffing Manager Tom Clemens MentorMate Alumnus V8 JavaScript Engine Viktor Mitev Yolanda Petkova Marketing Design Lead Pete Anderson Lead Product Owner, Target Vasil Nonchev Java Software Development Manager Dilyana Totseva QA Manager Stanimir Nikolov Software Development Lead - iOS, MentorMate Rosen Kolev Technology Principal Dimitar Mihaylov MentorMate Alumni Nikola Genov Software Architect - .NET Neli Todorova Software Development Manager - LAMP Yavor Dimitrov MentorMate Alumni Georgi Karanedyalkov Software Development Lead - Android, MentorMate Denislav Ganchev Technology Principal Stefan Shopov QA Manager Konstantin Rusev Java Developer Borislav Dimitrov Senior Android Developer, MentorMate Tsvetelina Lazarova MentorMate Alumni Dimitar Gadzhev Developer Plamen Stoev Software Development Manager - Front-end Jake Nelsen Senior Experience Designer Zlati Pehlivanov Senior Software Engineer II Kate Tolmie Senior Experience Designer Martin Angelov Director, Software Engineering - LAMP&FE, MentorMate Dimitar Zhelev Senior .NET Developer Joel Swenson Content Manager Kiril Ivanov Quality Assurance Analyst Viktor Hristoskov Software Development Lead - iOS, MentorMate Violeta Nikolcheva Database Developer Biliana Kadakevlieva Senior Quality Assurance Analyst Chris McLeod Senior Solutions Consultant Antonii Georgiev Junior .NET Developer Alexander Rusev Front-End Developer Matt Erickson MentorMate Alumni Brian Buchkosky Global Director, PMO David Tran MentorMate Alumni Kristin Krueger MentorMate Alumni Magdalena Chervenkova Business Analyst Denny Royal Chief Design Officer Joe Bodell MentorMate Alumni Viktoria Chuchumisheva HR Manager Kalina Tekelieva Senior Content Marketing Artist Daniel Rankov MentorMate Alumni Alexander Alexandrov BA Lead MentorMate Clint Rowles VP, Business Development Nikola Donev SysOps & DevOps Lead Tseko Tsolov Frontend Developer Denislav Lefterov Automation QA Analyst Dilyana Kodjamanova MentorMate Alumni Emma Jorstad Project Manager, Lead Georgi Georgiev Software Development Lead - LAMP, MentorMate Martin Panayotov Senior iOS Developer, MentorMate John Blake Senior Account Manager Tyler Compton Solutions Architect Nikola Peevski Software Developer — Lamp & Front-End Aaron Whitney Director of Client Strategy Veliko Ivanov Senior Cloud Engineer Suzanne O’Brien Senior Project Manager Svetlin Stanchev Software Development Lead - Front-end, MentorMate Todor Todorov Senior Cloud Engineer Kate Stamatova Senior QA Analyst Frank Anselmo Global Director, Project Management Gyuner Zeki Solutions Architect Galin Stanchev QA Analyst Sarah Hoops Business Development Manager Brenden Diehl Business Development Manager Anna Krivova Software Development Lead - Front-end, MentorMate Ivelina Kavalova Senior Business Analyst, MentorMate Paul Sanders MentorMate Alumni Jim Cikanek Senior Client Strategist Samuil Yanovski Software Development Manager - Android, MentorMate Krasimir Gatev Senior Android Developer, MentorMate Kristina Goryalova Talent Acquisition Manager Elena Petrova HR Specialist Jay Matre Senior Business Architect, MentorMate Lilyana Dimitrova QA Specialist Josh Marquart Chief Strategy Officer Mario Gorki Senior Mobile Developer Simeon Zhekov Cloud Engineer Hristo Stoyanov Cloud & DevOps Lead Ben Wallace Enterprise Architect Boyan Stoyanov Data & Dota Specialist Petya Ivanova Director, Software Engineering - Java Sebastian Ortiz-Chamorro VP of Engineering, Latin America Consuelo Merino Director of Operations 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.