Step by step guide for Jenkins FreeStyle Project
Introduction
In this article, we will be creating jenkins free style project for build, test and deploy WebApi (asp.net core) project.Steps will be as follows:
1. Install Jenkins on Windows Machine
2. Create WebAPI and MSTest project using Visual Studio
3. Identify steps and plugins required for CI/CD
4. Install Jenkins Plugins
5. Configure Plugins
6. Create a FreeStyle Job
1. Create simple WebAPI project and MSTest project.
2. Create account on www.github.com and create repository on it.
3. Download git from https://git-scm.com/downloads and install it.
4. Push the code in new repository which is created on github.
2. MSBuild Plugin - Build the downloaded code
3. MSTestRunner Plugin - Run the unit test cases
4. MSTest Plugin - Publish MSTest test results in Jenkins
5. Publish Over FTP or Publish Over SSH Plugin - Publish the build output to hosting server
4. Now
again click on the Add Build Step and select ‘Build a Visual Studio project or
solution using MSBuild
Now, everything is done. Job is ready. Just click 'Build Now' button as shown in the below screen and see the expected output.
6. Create a FreeStyle Job
Step1: Install Jenkins on Windows Machine
- Download and Install latest version of jdk from below URL.https://www.oracle.com/technetwork/java/javase/downloads/index.html
- Download and Install Jenkins from https://jenkins.io/download/ by clicking on Windows option. Follow the installation instructions.
Step2: Create WebAPI and MSTest Project on Windows
1. Create simple WebAPI project and MSTest project.
2. Create account on www.github.com and create repository on it.
3. Download git from https://git-scm.com/downloads and install it.
4. Push the code in new repository which is created on github.
Step3: Identify steps and plugins required for CI/CD.
1. Git Plugin - Pull the code from GitHub2. MSBuild Plugin - Build the downloaded code
3. MSTestRunner Plugin - Run the unit test cases
4. MSTest Plugin - Publish MSTest test results in Jenkins
5. Publish Over FTP or Publish Over SSH Plugin - Publish the build output to hosting server
Step4: Install Jenkins Plugins
1. Go to Home->Manage Jenkins->Manage Plugins
2. Select the plugin which needs to be installed and click the button "Install without restart"
2. Select the plugin which needs to be installed and click the button "Install without restart"
Step5: Configure Plugins
1. Go to Home->Manage Jenkins->Global Tool Configuation.
Set the path for the git installed on your machine.
Set the path for the MSBuild installed on your machine.
Set the path for the MSTest installed on your machine.
Set up the SSH Server or FTP server on the machine where build is to be published.Here, I have created SSH server.And set the credentials at the location Home->Configuration
Step6: Create a FreeStyle Job
1. Go to Home->New Item
After entering item name(here WebApiDemo) and
clicking OK button, below screen will appear.
Enter the command for restoring NuGet packages.
Command : <“Nuget.exe’s location”>
restore <relative path of .csproj file>
The command is
"G:\Software\NugetCLIClient\nuget.exe"
restore ProductProj\ProductProj\ProductProj.csproj
In the following screen, enter
the details for building the solution.
5. Now
again, click on Add Build Step and select ‘Execute Windows batch command’.
Enter the command for running
MSTest test cases.
The command is :
del ProductProj\ProductProjUnitTest\bin\Release\TestResults.trx
"C:\Program Files
(x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\MSTest.exe" /resultsfile:"ProductProj\ProductProjUnitTest\bin\Release\TestResults.trx"
/testcontainer:ProductProj\ProductProjUnitTest\bin\Release\ProductProjUnitTest.dll
/nologo
EXIT /B 0
Here, del command
deletes any existing trx report.
Next MSTest.exe
command the MSTest test cases.
Third EXIT
command exits the Job while running if it fails while running MSTest test cases.
6. Now
Go to Home->WebApiDemo->Post-build Actions.
click on Add post-build action
and select ‘Archive the artifacts’
Enter the relative path for build output so that it can
be archived.
Files to archive : **/ProductProj/ProductProj/bin/Release/**/*.*
7. Again
click Add Build Step and select ‘Publish MSTest test result report’. And enter
Test report TRX file : ProductProj\ProductProjUnitTest\bin\Release\TestResults.trx
8. Again
click Add Build Step and select ‘Send build artifacts over SSH’.
Enter the details of SSH
server, source files and remote directory
Source files: ProductProj/ProductProj/bin/Release/net461/publish/*.*
Remove prefix: ProductProj/ProductProj/bin/Release/net461/publish
Remote directory: SSHFtp/WebApiDemo/
Now, everything is done. Job is ready. Just click 'Build Now' button as shown in the below screen and see the expected output.

























Nice Steps. Easy to understand 😊
ReplyDelete