优化 Debian 上的 Jenkins 可以从 系统层、JVM 层、Jenkins 配置、插件与任务、架构扩展 几个层面来做。下面给你一套比较完整、可落地的优化方案。
编辑 /etc/security/limits.conf:
jenkins soft nofile 65536
jenkins hard nofile 65536
jenkins soft nproc 4096
jenkins hard nproc 4096
/etc/sysctl.conf:
vm.swappiness = 10
net.core.somaxconn = 1024
fs.file-max = 2097152
sysctl -p
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
Debian 下编辑:
sudo nano /etc/default/jenkins
示例(8G 内存服务器):
JAVA_OPTS="-Xms2g -Xmx4g \
-XX:+UseG1GC \
-XX:MaxGCPauseMillis=200 \
-XX:+ParallelRefProcEnabled \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:HeapDumpPath=/var/lib/jenkins/heapdump.hprof"
| 内存 | GC |
|---|---|
| < 4G | Parallel GC |
| ≥ 4G | G1 GC(推荐) |
Manage Jenkins → Nodes
Master → 设置 # of executors = 0 或 1
✅ 主节点只做调度,不跑构建
Manage Jenkins → Plugins → Installed
常见“吃资源”插件:
✅ 推荐:
❌ 避免:
pipeline {
agent none
stages {
stage('Build') {
agent any
steps {
sh 'make'
}
}
}
}
cleanWs()
或:
options {
skipDefaultCheckout true
}
-Dmaven.repo.local=/cache/.m2npm ci --cache .npmparallel {
stage('Unit Test') {}
stage('Build') {}
}
pipeline {
agent {
docker {
image 'maven:3.9'
}
}
}
✅ 避免每次构建安装环境
sudo apt install jenkins-cleanup
或手动:
rm -rf /var/lib/jenkins/workspace/*
rm -rf /var/lib/jenkins/builds/*
/etc/logrotate.d/jenkins:
/var/log/jenkins/*.log {
daily
rotate 7
compress
missingok
}
/var/lib/jenkins 放在 SSD| 场景 | 建议 |
|---|---|
| 10+ 项目 | Agent 分离 |
| 50+ 构建 | Master 只调度 |
| 100+ 构建 | 引入 Kubernetes |
| 多团队 | Folder + RBAC |
✅ Master 不跑任务
✅ JVM 内存合理
✅ 使用 G1 GC
✅ 插件精简
✅ Pipeline 并行
✅ Docker Agent
✅ 磁盘 SSD
✅ Nginx 反向代理
JAVA_OPTS你可以直接贴: