Prerequisites
Before deploying Jenkins on Debian, ensure your system meets the following requirements:
sudo apt update && sudo apt install -y openjdk-11-jdk
Verify installation with java -version
./var/lib/jenkins
).Step 1: Add Jenkins Repository and Install
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
echo "deb https://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list
sudo apt update && sudo apt install -y jenkins
Step 2: Start and Enable Jenkins Service
Start the Jenkins service and configure it to launch automatically on boot:
sudo systemctl start jenkins
sudo systemctl enable jenkins
Verify the service status with sudo systemctl status jenkins
(should show “active (running)”).
Step 3: Unlock Jenkins and Complete Initial Setup
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
http://<your_server_ip>:8080
. Paste the initial password into the “Unlock Jenkins” prompt.Step 4: Configure Global Settings
/usr/bin/git
(default on Debian).JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
) under “Manage Jenkins” > “Configure System” to standardize builds across agents.Step 5: Create a CI/CD Pipeline
Jenkins pipelines automate the entire CI/CD process (build, test, deploy). Use a declarative pipeline (recommended for readability) stored in a Jenkinsfile
within your code repository.
pipeline {
agent any // Uses the default Jenkins agent
environment {
MAVEN_OPTS = "-Dmaven.test.failure.ignore=true" // Ignore test failures during build
ARTIFACT_DIR = "target"
}
stages {
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/your-repo/your-app.git' // Replace with your repo URL
}
}
stage('Build') {
steps {
sh 'mvn clean package' // Compile and package the application
}
}
stage('Test') {
steps {
sh 'mvn test' // Run unit tests
junit "${ARTIFACT_DIR}/surefire-reports/*.xml" // Publish test results
}
}
stage('Deploy to Staging') {
when { branch 'main' } // Only deploy from the main branch
steps {
sshagent(['staging-ssh-key']) { // Use a Jenkins credential for SSH
sh 'scp -r ${ARTIFACT_DIR}/* user@staging-server:/opt/app' // Copy artifacts to staging
sh 'ssh user@staging-server "sudo systemctl restart app-service"' // Restart the app service
}
}
}
}
post {
always {
cleanWs() // Clean up workspace after build
}
success {
slackSend channel: '#ci-cd', message: "Build ${env.BUILD_NUMBER} succeeded!" // Notify Slack (optional)
}
failure {
mail to: 'team@example.com', subject: "Build ${env.BUILD_NUMBER} Failed", body: "Check console output at ${env.BUILD_URL}" // Email alert
}
}
}
main
) and the path to the Jenkinsfile
(leave blank for root).http://<jenkins_url>/github-webhook/
. In Jenkins, enable “GitHub hook trigger for GITScm polling”.H/5 * * * *
for every 5 minutes) to check for code changes.Step 6: Secure Jenkins
Security is critical for protecting your CI/CD environment. Implement these measures:
sshagent
or withCredentials
.Step 7: Monitor and Optimize
/etc/default/jenkins
) and adding:JAVA_OPTS="-Xmx4g -Xms2g" # Adjust values based on your server’s memory
Restart Jenkins after making changes.