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:
- A system running Windows or Linux
- Java Development Kit (JDK) installed
- .NET SDK (Download Here)
- Jenkins (Download Here)
- Git installed (Download Here)
Step 1: Install Jenkins on Windows/Linux
Installing Jenkins on Windows
- Download the Jenkins Windows Installer from the official Jenkins website.
- Run the installer and follow the setup wizard.
- During installation, select the option to run Jenkins as a Windows service.
- Once installation is complete, Jenkins will start automatically and be accessible at
http://localhost:8080
. - Retrieve the initial administrator password from:
type C:\ProgramData\Jenkins\.jenkins\secrets\initialAdminPassword
- Complete the setup by installing suggested plugins and creating an admin user.
Installing Jenkins on Linux
- 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
- Start the Jenkins service:
sudo systemctl start jenkins
- Enable Jenkins to start at boot:
sudo systemctl enable jenkins
- 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
- Navigate to
Manage Jenkins -> Plugin Manager
. - Install the following plugins:
- Git Plugin
- Pipeline Plugin
- Blue Ocean (for improved pipeline visualization)
Create a New Pipeline Job
- Open Jenkins and click on
New Item
. - Choose
Pipeline
and name it accordingly. - In the
Pipeline
section, selectPipeline script from SCM
. - Choose
Git
and enter your repository URL. - Specify the branch to build (e.g.,
main
ordevelop
). - 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:
- Install IIS and configure a site.
- Add a
deploy.ps1
PowerShell script:Stop-Service -Name 'W3SVC' Copy-Item -Path out\* -Destination 'C:\inetpub\wwwroot' -Recurse -Force Start-Service -Name 'W3SVC'
- Add a
Deploy
stage to theJenkinsfile
:stage('Deploy') { steps { script { powershell '.\deploy.ps1' } } }
Automate Deployment to Linux Server
For Linux deployments, use SCP and SSH:
- Install OpenSSH server on the target machine:
sudo apt install openssh-server
- 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.