ASP.Net Web Application Continuous Integration (CI) using Jenkins automation server: Creating Jobs In Jenkins: Getting source code from Bitbucket hosted git repository, polling the SCM to check whether changes were made, restore Nuget packages, build project using MSBuild, publish and archive the build
In this blog post, we will see how we can create a now Job in Jenkins. We will see how we can configure Jenkin’s Job to :
- Get latest code from Bitbucket hosted git repository
- Poll the SCM to check whether changes were made (i.e. new commits) and builds the project if new commits where pushed since the last build
- Restore Nuget packages
- Build & publish Visual Studio solution/project using MSBuild
- Archive the builds
Note: You are currently on Part IV of this series, do refer below parts for more details.
Part I: Basic Setup, Installation & Configuration
Part II: Starting & setting up Jenkins
Part III: Creating & Adding SSH key to Bitbucket
A. Create new Job
Click on “New Item” link.
Enter an item name, select “Freestyle project” & click “OK”.
B. “General” Tab
Add “Description”. You can leave rest of the field as is.
C. “Source Code Management” Tab
I. Select “Version Control System”
My project is using Git version control system, so I have selected “Git”.
II. Add repository details
a. Repository URL
My Git repository is hosted on Bitbucket. From Bitbucket, I will get the SSH clone url.
Enter the SSH clone url of the repository, to “Repository URL”.
b. Add Credentials
You need to add your repository credential. We have already created the SSH public & private keys for Bitbucket. Also, we have added the public key content to the Bitbucket SSH key. In this step, we will need to add private key content to Jenkins credentials.
- For “Kind”, select “SSH Username with private key”.
- For “Username”, enter any name for your credentials.
- For “Private Key”, select “Enter directly” and enter the contents for your SSH private key in “Key” textbox.
- Enter the “Passphrase” that you have entered while creating SSH keys.
Click “Add” after filling the details.
Now, select the “Credentials” that you have created. All the connection error will be fixed now.
c. Branches to build
Enter “Branch Specifier”. In my case, I need to build the master branch.
D. “Build Triggers” Tab
a. Select “Poll SCM”
Poll SCM periodically polls the SCM to check whether changes were made (i.e. new commits) and builds the project if new commits where pushed since the last build.
b. Set “Schedule” to H/5 * * * * to poll every five minutes.
E. “Build” Tab
I. Add Build Step, for Nuget Packages Restore
Add “Execute Windows batch command” build step.
In “Command” textbox, enter nuget restore
II. Add Build Step, for building a Visual Studio project or solution using MSBuild
Add “Build a Visual Studio project or solution using MSBuild” build step.
For “MSBuild Version”, select MSBuild-Default
For “Command Line Arguments”, set the following:
/p:Configuration=Release /p:DeployOnBuild=true /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:DeleteExistingFiles=true /p:publishUrl=I:\Jenkins\jenkins_home_dir\workspace\publish\MySampleApplication\Latest
MSBuild will build the solution in “Release” mode and on successful build it will publish the build to “I:\Jenkins\jenkins_home_dir\workspace\publish\MySampleApplication\Latest
” directory.
III. Add Build Step, for archiving the builds
Add “Execute Windows batch command” build step.
In “Command” textbox, set the following:
rem Create TIMESTAMP:
FOR /f %%a IN ('WMIC OS GET LocalDateTime ^| FIND "."') DO SET DTS=%%a
SET TIMESTAMP=%DTS:~0,8%_%DTS:~8,6%
rem Create folder:
md I:\Jenkins\jenkins_home_dir\workspace\publish\MySampleApplication\%TIMESTAMP%
xcopy /E "I:\Jenkins\jenkins_home_dir\workspace\publish\MySampleApplication\Latest" "I:\Jenkins\jenkins_home_dir\workspace\publish\MySampleApplication\%TIMESTAMP%"
This will create a new folder with current Timestamp as name and copy the contents of build published ("I:\Jenkins\jenkins_home_dir\workspace\publish\MySampleApplication\Latest"
) in previous step to it.
F. Save the Job
The job you have created is ready now.
G. Build the project
You can trigger the build manually by clicking “Build Now” icon.
Under “Build History“, you can see that the build is triggered.
Click on the build number, to check the details of particular build.
Click on “Console Output”, to check full build logs.
Previous: Creating & Adding SSH key to Bitbucket
That it !!!
.NET Professional | Microsoft Certified Professional | DZone’s Most Valuable Blogger | Web Developer | Author | Blogger
Doctorate in Computer Science and Engineering
Microsoft Certified Professional (MCP) with over 12+ years of software industry experience including development, implementation & deployment of applications in the .NET framework
Experienced and skilled Agile Developer with a strong record of excellent teamwork, successful coding & project management. Specialises in problem identification and proposal of alternative solutions. Provided knowledge and individual mentoring to team members as needed
Among top 3% overall in terms of contribution on Stack Overflow (~2.3 million people reached my posts). Part of the top 1% Stack Overflow answerers in ASP.NET technology.
DZone’s Most Valuable Blogger (MVB)
Created and actively maintain the TechCartNow.com tech blog while also editing, writing, and researching topics for publication.
Excellent skills in Application Development using C#/Vb.Net, .NET Framework, ASP.NET, MVC, ADO.NET, WCF, WPF, Web API, SQL Server, jQuery, Angular, React, BackboneJS
3 Responses
[…] Part IV: Creating Jobs In Jenkins: Getting source code from Bitbucket hosted git repository, polling the SCM … […]
[…] Part IV: Creating Jobs In Jenkins: Getting source code from Bitbucket hosted git repository, polling the SCM … […]
[…] Part IV: Creating Jobs In Jenkins: Getting source code from Bitbucket hosted git repository, polling the SCM … […]