Prerequisites
Before starting, ensure your Debian system is updated and has Java (required by Jenkins) and Git installed. Use the following commands to install OpenJDK 11 (a common Jenkins dependency) and Git:
sudo apt update && sudo apt upgrade -y
sudo apt install openjdk-11-jdk git -y
Verify Java installation with java -version and Git with git --version.
1. Install Jenkins on Debian
Add the official Jenkins repository to your system’s package list to install the latest stable version:
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
echo "deb http://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list
sudo apt update
sudo apt install jenkins -y
Start the Jenkins service and enable it to launch at boot:
sudo systemctl start jenkins
sudo systemctl enable jenkins
Check the service status to confirm it’s running:
sudo systemctl status jenkins
2. Initial Jenkins Configuration
Access the Jenkins web interface at http://<your_server_ip>:8080 in a browser. You’ll need the initial admin password to unlock Jenkins—retrieve it with:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Paste the password into the unlock page. Next, install recommended plugins (like Git, Pipeline, and Blue Ocean) to enable core CI/CD functionality. Create an admin user to manage Jenkins going forward.
3. Configure Global Tools in Jenkins
Set paths for essential tools (Java, Git, Maven) so Jenkins can locate them during builds. Go to Manage Jenkins > Global Tool Configuration:
/usr/lib/jvm/java-11-openjdk-amd64 (default path for OpenJDK 11).4. Create a Jenkins Pipeline for CI/CD
Pipelines automate the entire CI/CD process (build, test, deploy) using a Jenkinsfile (stored in your project repository). Here’s a step-by-step guide:
In Jenkins, click New Item, enter a job name (e.g., “MyApp-CI/CD”), select Pipeline, and click OK.
Jenkinsfile from your repository.https://github.com/your-repo/your-project.git).Jenkinsfile (e.g., Jenkinsfile in the root directory).Save the configuration.
5. Write a Sample Jenkinsfile
Create a Jenkinsfile in your project root to define CI/CD stages. Below is a basic example for a Node.js app (adapt for your tech stack):
pipeline {
agent any // Use any available agent (server) to run the pipeline
stages {
// Stage 1: Checkout code from the repository
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/your-repo/your-project.git'
}
}
// Stage 2: Build the application (example for Node.js)
stage('Build') {
steps {
sh 'npm install' // Install dependencies
sh 'npm run build' // Run build script (if defined in package.json)
}
}
// Stage 3: Run tests (example for Node.js)
stage('Test') {
steps {
sh 'npm test' // Execute test suite (e.g., Jest, Mocha)
}
}
// Stage 4: Deploy to production (example for Kubernetes)
stage('Deploy') {
when {
branch 'main' // Only deploy if changes are pushed to the main branch
}
steps {
sh 'kubectl apply -f k8s/deployment.yaml' // Apply Kubernetes deployment config
sh 'kubectl rollout status deployment/myapp' // Check deployment status
}
}
}
// Post-build actions (optional)
post {
success {
echo 'Pipeline completed successfully!'
// Send notification (e.g., email, Slack) here
}
failure {
echo 'Pipeline failed. Check logs for details.'
// Send failure notification here
}
}
}
This file defines four stages: Checkout (pulls code), Build (installs dependencies and builds the app), Test (runs tests), and Deploy (deploys to Kubernetes if changes are on the main branch).
6. Trigger the Pipeline
You can trigger the pipeline manually (click Build Now in the Jenkins job page) or automatically using webhooks. To set up a webhook for GitHub:
http://<your_server_ip>:8080/github-webhook/) and select “Just the push event.”7. Monitor and Debug Builds
After triggering a pipeline, you can monitor its progress in the Build History section of the job page. Click on a build to see detailed logs, which help debug issues (e.g., failed tests, build errors). You can also use the Console Output tab to view real-time logs.
Optional Enhancements
Jenkinsfile to build and push Docker images (e.g., docker.build("myapp:${env.BUILD_ID}")).