下面按从零开始在 CentOS 上使用 Jenkins的思路,给你一套实用、可操作的步骤(以 CentOS 7/8 为例,Jenkins 长期支持版 LTS)。
Jenkins 需要 Java 8 或 Java 11
# CentOS 7
yum install -y java-11-openjdk-devel
# 查看版本
java -version
# 导入 Jenkins 密钥
sudo wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
# 安装
yum install -y jenkins
# 设置开机自启
systemctl enable jenkins
# 启动
systemctl start jenkins
# 查看状态
systemctl status jenkins
如果启动失败,常见原因:
# 开放 8080 端口
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
echo "Hello Jenkins"
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
}
}
# 修改配置
vi /etc/sysconfig/jenkins
确保:
JENKINS_PORT="8080"
JENKINS_JAVA_CMD="/usr/bin/java"
vi /etc/sysconfig/jenkins
JENKINS_PORT="9090"
systemctl restart jenkins
/var/lib/jenkins/var/log/jenkins/jenkins.log如果你愿意,可以告诉我:
我可以直接给你针对性配置方案或 Jenkinsfile。