Ads

Jenkins : Deploy ASP.NET Core web API to IIS or any windows directory Using Jenkins Pipeline project

Hello All,

In this article we will learn how to deploy the ASP.NET Core web API to local windows folder or IIS.

Below are the things we are going to learn

    1. Create the Web API Project

    2. Creating the repository in Git and Code check in

    3. Setting up the Deployment

Lets Start now....

1.Create the Web API Project

Open visual studio

·         Create a new project


Select template ASP.NET Core Web API


·         Enter the project name and location click Ã  Next


Select the framework, in my case I will choose .NET 6.0 then click on Ã  Create


Great! We have created the API, it’s time to run and test the API

We use open API, which is available in our application i.e. Swagger

https://localhost:7160/WeatherForecast

 And Output is.

[
  {
    "date": "2022-05-17T15:51:33.8672531+05:30",
    "temperatureC": 21,
    "temperatureF": 69,
    "summary": "Balmy"
  },
  {
    "date": "2022-05-18T15:51:33.8691463+05:30",
    "temperatureC": 10,
    "temperatureF": 49,
    "summary": "Cool"
  },
  {
    "date": "2022-05-19T15:51:33.8691654+05:30",
    "temperatureC": -18,
    "temperatureF": 0,
    "summary": "Sweltering"
  },
  {
    "date": "2022-05-20T15:51:33.8691658+05:30",
    "temperatureC": -16,
    "temperatureF": 4,
    "summary": "Warm"
  },
  {
    "date": "2022-05-21T15:51:33.8691661+05:30",
    "temperatureC": 37,
    "temperatureF": 98,
    "summary": "Bracing"
  }
]

Application is running without any issues.

2. Creating the repository in Git and Code check in

Here we will learn how to create the repository and check in the application which we have created in step 1

If you do not have github account, please visit github.com and create the account with your google email id, it’s very simple and easy process.

Below are steps to create the repository and code check-in to that

Create Repository, Click + icon and Click à Next Repository



Enter Repository Name and description and then click on Create Repository



Repository created successfully

Note: We need to Install the two software’s in windows machine to perform the Check in process
Copy the URL 


Go to Local drive in your machine and right click --> TortoiseGit --> Clone



Copy and Paste your project under cloned folder


Right Click On folder --> TortoiseGit --> Commit --> “main”…


Select All files --> Right Click --> Add


Provide the comment and Commit and Push


Code committed successfully


Go to Git repository and refresh the page, you will see the source code


3. Setting up the Deployment


Go to Jenkins



Login to Jenkins --> You will be redirected to Dashboard


Click on New Item --> Enter Project Name --> Select option Pipeline


Add the below line in Pipeline script part

//GITHUB URL
String githubUrl = "https://github.com/SourceLastBenchCoder/SampleNetCoreAPIForJenkinsDeployment"

//Project Name in GITHUB
String projectName = "APIForCICDDeployment"

//JENKINS WORKSPACE CODE PUBLISHED PATH
String publishedPath = "APIForCICDDeployment\\bin\\Release\\net6.0\\publish"

//IIS APPLICATION OR WEBSITE NAME
String iisApplicationName = "apiforjenkins"

//IIS WEBSITE PATH
String iisApplicationPath = "D:\\Deployment\\appforjenkins"

node () {
    stage('Checkout') {
        checkout([
            $class: 'GitSCM', 
            branches: [[name: 'main']], 
            doGenerateSubmoduleConfigurations: false, 
            extensions: [], 
            submoduleCfg: [], 
            userRemoteConfigs: [[url: """ "${githubUrl}" """]]])
    }
    stage('Build') {
        bat """
        cd ${projectName}
        dotnet build -c Release /p:Version=${BUILD_NUMBER}
        dotnet publish -c Release --no-build
        """
    }
    stage('Publish') {
        bat """
        cd ${projectName}
        dotnet publish -c Release --no-build
        """
    }
    stage('Deploy-DEV'){
       bat """ "C:\\Program Files (x86)\\IIS\\Microsoft Web Deploy V3\\msdeploy.exe" -verb:sync -source:iisApp="${WORKSPACE}\\${publishedPath}" -enableRule:AppOffline -dest:iisApp="${iisApplicationName} """
    }
}


Click On Save

Created project will be displayed on dashboard


Click on project and click on Build Now


Build started and completed


Deployment done successfully with the help of Jenkins


Thats it, code published to required folder. It is your IIS Directory or any window directory

D:\Deployment\appforjenkins


Lets connect in another article.
Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !