Run Jenkins Locally for .NET Build Automation | Easy Setup Guide

Run Jenkins Locally for .NET Build Automation | Easy Setup Guide

Jenkins is a powerful open-source automation server widely used for continuous integration and continuous deployment (CI/CD). It enables developers to automate build, test, and deployment processes efficiently. In this guide, we will cover how to install Jenkins on Windows/Linux, set up a Jenkins pipeline for .NET Core projects, and automate build and deployment tasks.

Prerequisites

Before proceeding, ensure you have the following:

Step 1: Install Jenkins on Windows/Linux

Installing Jenkins on Windows

  1. Download the Jenkins Windows Installer from the official Jenkins website.
  2. Run the installer and follow the setup wizard.
  3. During installation, select the option to run Jenkins as a Windows service.
  4. Once installation is complete, Jenkins will start automatically and be accessible at http://localhost:8080.
  5. Retrieve the initial administrator password from:
    type C:\ProgramData\Jenkins\.jenkins\secrets\initialAdminPassword
    
  6. Complete the setup by installing suggested plugins and creating an admin user.

Installing Jenkins on Linux

  1. Open a terminal and install Jenkins using the following commands:
    sudo apt update
    sudo apt install openjdk-11-jdk
    wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
    sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
    sudo apt update
    sudo apt install jenkins
    
  2. Start the Jenkins service:
    sudo systemctl start jenkins
    
  3. Enable Jenkins to start at boot:
    sudo systemctl enable jenkins
    
  4. Access Jenkins at http://localhost:8080 and complete the setup as guided.

Step 2: Set Up a Jenkins Pipeline for .NET Core Projects

Install Required Plugins

  1. Navigate to Manage Jenkins -> Plugin Manager.
  2. Install the following plugins:
    • Git Plugin
    • Pipeline Plugin
    • Blue Ocean (for improved pipeline visualization)

Create a New Pipeline Job

  1. Open Jenkins and click on New Item.
  2. Choose Pipeline and name it accordingly.
  3. In the Pipeline section, select Pipeline script from SCM.
  4. Choose Git and enter your repository URL.
  5. Specify the branch to build (e.g., main or develop).
  6. Define the Jenkinsfile in the repository root.

Write a Jenkinsfile for .NET Core

Create a Jenkinsfile in the root of your repository:

pipeline {
    agent any
    
    stages {
        stage('Checkout') {
            steps {
                git 'https://github.com/your-repo.git'
            }
        }
        stage('Build') {
            steps {
                script {
                    sh 'dotnet restore'
                    sh 'dotnet build --configuration Release'
                }
            }
        }
        stage('Test') {
            steps {
                script {
                    sh 'dotnet test'
                }
            }
        }
        stage('Publish') {
            steps {
                script {
                    sh 'dotnet publish -c Release -o out'
                }
            }
        }
    }
}

Step 3: Automate Build and Deployment Tasks

Running the Pipeline

Once the pipeline is set up, trigger a build manually by navigating to the job and clicking Build Now.

Automate Deployment to IIS (Windows)

To automate deployment to IIS on Windows:

  1. Install IIS and configure a site.
  2. Add a deploy.ps1 PowerShell script:
    Stop-Service -Name 'W3SVC'
    Copy-Item -Path out\* -Destination 'C:\inetpub\wwwroot' -Recurse -Force
    Start-Service -Name 'W3SVC'
    
  3. Add a Deploy stage to the Jenkinsfile:
    stage('Deploy') {
        steps {
            script {
                powershell '.\deploy.ps1'
            }
        }
    }
    

Automate Deployment to Linux Server

For Linux deployments, use SCP and SSH:

  1. Install OpenSSH server on the target machine:
    sudo apt install openssh-server
    
  2. Add a deployment stage to the Jenkinsfile:
    stage('Deploy') {
        steps {
            script {
                sh 'scp -r out/* user@server:/var/www/dotnet-app'
                sh 'ssh user@server "systemctl restart dotnet-app.service"'
            }
        }
    }
    

Conclusion

Running Jenkins locally for .NET build automation provides a streamlined process for CI/CD. By installing Jenkins, configuring pipelines, and automating deployment tasks, developers can efficiently manage build workflows and ensure rapid, reliable software delivery. This setup can be expanded further with integrations for testing, monitoring, and security scanning to enhance the automation pipeline.

Sandip Mhaske

I’m a software developer exploring the depths of .NET, AWS, Angular, React, and digital entrepreneurship. Here, I decode complex problems, share insightful solutions, and navigate the evolving landscape of tech and finance.

Post a Comment

Previous Post Next Post