在 CentOS 上使用 Jenkins,一般流程是:安装 → 启动 → 配置 → 创建任务 → 构建。下面按实操步骤给你讲清楚。
# CentOS 7
yum install -y java-1.8.0-openjdk
# CentOS 8
dnf install -y java-11-openjdk
检查:
java -version
wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install -y jenkins
systemctl start jenkins
systemctl enable jenkins
查看状态:
systemctl status jenkins
如果防火墙开着:
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
浏览器访问:
http://服务器IP:8080
cat /var/lib/jenkins/secrets/initialAdminPassword
复制粘贴到页面中。
https://github.com/xxx/xxx.git例如执行 shell:
mvn clean package
或:
sh build.sh
pipeline {
agent any
stages {
stage('拉代码') {
steps {
git 'https://github.com/xxx/xxx.git'
}
}
stage('构建') {
steps {
sh 'mvn clean package'
}
}
stage('部署') {
steps {
sh 'bash deploy.sh'
}
}
}
}
| 路径 | 作用 |
|---|---|
/var/lib/jenkins |
主目录 |
/var/log/jenkins |
日志 |
/etc/sysconfig/jenkins |
配置文件 |
/var/lib/jenkins/secrets |
密码、密钥 |
编辑:
vi /etc/sysconfig/jenkins
修改:
JENKINS_PORT="8080"
重启:
systemctl restart jenkins
chown -R jenkins:jenkins /var/lib/jenkins
在 Jenkins 全局配置中:
jenkins 用户✅ 自动拉代码
✅ 自动构建(Maven / npm / Docker)
✅ 自动部署到服务器
✅ 定时任务(如每天构建)
如果你愿意,可以告诉我:
我可以直接给你 可复制的完整配置示例。