Prerequisites
Before deploying Jenkins on Debian, ensure your system meets the following requirements:
- Java Environment: Jenkins requires Java 11 or higher. Install OpenJDK 11 using:
Verify installation withsudo apt update && sudo apt install -y openjdk-11-jdkjava -version. - Debian Version: Use Debian 10 (Buster) or later for compatibility.
- Disk Space: Allocate at least 10GB for Jenkins home directory (
/var/lib/jenkins). - Memory: A minimum of 4GB RAM is recommended; 8GB+ is ideal for production.
Step 1: Add Jenkins Repository and Install
- Download GPG Key: Import Jenkins’ official GPG key to verify package authenticity:
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add - - Add Repository: Create a new source list file for Jenkins and populate it with the stable repository URL:
echo "deb https://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list - Update and Install: Refresh the package index and install Jenkins:
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
- Retrieve Initial Password: The default admin password is stored in a file. Display it using:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword - Access Web Interface: Open a browser and navigate to
http://. Paste the initial password into the “Unlock Jenkins” prompt.:8080 - Install Plugins: Select “Install suggested plugins” to install essential tools (e.g., Pipeline, Git, SSH). Alternatively, manually choose plugins like “Kubernetes” or “Docker” if needed.
- Create Admin User: Set up a permanent admin account with a strong username/password and email. This replaces the default “admin” user for security.
Step 4: Configure Global Settings
- Tool Configuration: Go to “Manage Jenkins” > “Global Tool Configuration”. Add paths for tools like Java (already installed), Git, and Maven. For example:
- Git: Set the path to
/usr/bin/git(default on Debian). - Maven: Click “Add Maven” and specify the installation directory (or enable auto-installation).
- Git: Set the path to
- Environment Variables: Add global variables (e.g.,
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.
- Sample Jenkinsfile: Below is a basic pipeline for a Java application using Maven and Git:
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 } } } - Create Pipeline Job: In Jenkins, click “New Item”, name your job (e.g., “MyApp-CI/CD”), select “Pipeline”, and click OK.
- Configure Pipeline:
- Under “Pipeline”, select “Pipeline script from SCM”.
- Choose “Git” as the SCM and enter your repository URL.
- Specify the branch (e.g.,
main) and the path to theJenkinsfile(leave blank for root).
- Trigger Pipeline: Configure triggers to start builds automatically:
- Webhook: Add a webhook in your Git provider (e.g., GitHub) pointing to
http://. In Jenkins, enable “GitHub hook trigger for GITScm polling”./github-webhook/ - Poll SCM: Set a schedule (e.g.,
H/5 * * * *for every 5 minutes) to check for code changes.
- Webhook: Add a webhook in your Git provider (e.g., GitHub) pointing to
Step 6: Secure Jenkins
Security is critical for protecting your CI/CD environment. Implement these measures:
- Enable Authentication: Go to “Manage Jenkins” > “Configure Global Security” and enable “Logged-in users can do anything”. Then, install the “Role-based Authorization Strategy” plugin to assign granular permissions (e.g., developers can only build, admins can deploy).
- Manage Credentials: Store sensitive data (e.g., SSH keys, API tokens) in Jenkins’ credentials store. Use the “Credentials” section to add credentials and reference them in pipelines with
sshagentorwithCredentials. - Enable HTTPS: Use Nginx or Apache as a reverse proxy with Let’s Encrypt to secure traffic. Configure HTTPS in Jenkins under “Manage Jenkins” > “Configure Global Security”.
- Audit Logs: Install the “Audit Trail” plugin to track user actions (e.g., job modifications, builds) for accountability.
Step 7: Monitor and Optimize
- Monitoring: Use plugins like “Monitoring” (by Jenkins) or integrate with Prometheus/Grafana to track resource usage (CPU, memory) and job performance.
- Distributed Builds: For large projects, add agent nodes (machines with Jenkins installed) to distribute builds. Configure agents in “Manage Jenkins” > “Manage Nodes and Clouds”.
- Performance Tuning: Increase JVM heap size by editing the Jenkins startup script (
/etc/default/jenkins) and adding:Restart Jenkins after making changes.JAVA_OPTS="-Xmx4g -Xms2g" # Adjust values based on your server’s memory
以上就是关于“如何用Debian部署Jenkins CI/CD”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm