Thursday, January 9, 2025

How to configure CI/CD using Jenkin, GITHub and NET core API in the local laptop.

Step1: First install Jenkin and Git in your laptop

Step2: Install plugin such as git, pipeline, MSBuild

Step3: Create a .net core api project and push code in the github. note: your github project must have read and write permission. Otherwise, Jenkin won’t be able to build the code.

Step4: Do some setup in jenkin such as go to Dashboard>Manage Jenkins>Tools and then add this

Git installations >> C:\Program Files\Git\cmd\git.exe (set git.exe path after git installations)

 

Step5: Open Jenkin and go to new item > enter name and then select pipeline.  And then in the pipeline past this script

 

Without approver:


pipeline {

    agent any

    stages {

        stage('Clone') {

            steps {

                git credentialsId: 'deb-credentials', url: 'https://github.com/debobrotadas/itemdetails.git'

            }

        }

        stage('Restore Dependencies') {

            steps {

                bat 'dotnet restore'

            }

        }

        stage('Build') {

            steps {

                bat 'dotnet build'

            }

        }

        stage('Publish') {

            steps {

                bat 'dotnet publish -c Release -o output'

            }

        }

        stage('Deploy to IIS') {

            steps {

                bat '''

                xcopy /s /e /y output "C:\\inetpub\\wwwroot\\itemdetails"

                iisreset

                '''

            }

        }

    }

}

 

With approver and email send:

pipeline {

    agent any

    environment {

        DEPLOY_DIR = 'C:\\inetpub\\wwwroot\\itemdetails'

        OUTPUT_DIR = 'C:\\inetpub\\wwwroot\\itemdetailspublish'

    }

    triggers {

        // Automatically build when changes are pushed to the repository

        githubPush()

    }

    stages {

        stage('Clone') {

            steps {

                git credentialsId: 'deb-credentials', url: 'https://github.com/debobrotadas/itemdetails.git'

            }

        }

        stage('Restore Dependencies') {

            steps {

                bat 'dotnet restore'

            }

        }

        stage('Build') {

            steps {

                bat 'dotnet build'

            }

        }

        stage('Publish') {

            steps {

                bat "dotnet publish -c Release -o ${OUTPUT_DIR}"

            }

        }

        stage('Approval') {

            steps {

                // Wait for manual approval before deploying

                input message: "Do you approve deployment to IIS?", ok: "Yes, Deploy"

            }

        }

        stage('Deploy to IIS') {

            steps {

                bat """

                xcopy /s /e /y ${OUTPUT_DIR} "${DEPLOY_DIR}"

                iisreset

                """

            }

        }

    }

    post {

        always {

            echo 'Cleaning up workspace...'

            cleanWs() // Clean up the workspace after build

        }

        success {

            script {

                echo 'Build and deployment succeeded!'

            }

            emailext subject: "SUCCESS: Job '${env.JOB_NAME}'",

                     body: """Build and deployment for '${env.JOB_NAME}' succeeded.

                              Deployed to: ${DEPLOY_DIR}""",

                     to: 'debobrotadas@gmail.com'

        }

        failure {

            script {

                echo 'Build or deployment failed!'

            }

            emailext subject: "FAILURE: Job '${env.JOB_NAME}'",

                     body: """The build or deployment for '${env.JOB_NAME}' has failed.

                              Please check the Jenkins logs for more details.""",

                     to: 'debobrotadas@gmail.com'

        }

    }

}

 

If you setup approver, then see this point to approve.

 

 

Here credentials 'deb-credentials’ need to setup in Jenkin.  This credential is Github credentials.

User name: debobrotadas

Password: Github personal access token

After build file will be located: C:\\inetpub\\wwwroot\\itemdetails

 

 

 

 

Step6: once all configuration is done, then click build now. Then you can see the stage. Each step will be successful.