环境: CentOS 7.6
方式一(不推荐使用yum安装
)
- 获取安装源
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
- 安装Jenkins
yum install jenkins
- 修改配置
vim /etc/sysconfig/jenkins
修改如下:
JENKINS_PORT="8080" #Jenkins服务访问端口
JENKINS_USER="root" #用户为root
- 修改目录权限
chown -R root:root /var/lib/jenkins
chown -R root:root /var/cache/jenkins
chown -R root:root /var/log/jenkins
- 启动Jenkins
service jenkins start
ps -ef | grep jenkins
错误信息:
Starting Jenkins bash: /usr/bin/java: No such file or directory #未找到java文件
解决方法,创建java文件软连接:
ln -s /usr/local/java/jdk1.8.0_171/bin/java /usr/bin/java
- 重新启动Jenkins
service jenkins restart
- 访问Jenkins控制台
浏览器输入http://ip:8080,提示输入密码,执行如下命令查看:
#查看初始密码
cat /var/lib/jenkins/secrets/initialAdminPassword
提示实例已离线,解决办法:
访问: http://ip:8080/pluginManager/advanced
修改升级站点为:
https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
或者
http://mirror.xmission.com/jenkins/updates/current/update-center.json
#镜像仓库仓库网站:
http://mirrors.jenkins-ci.org/status.html
#重启Jenkins
service jenkins restart
方式二(推荐使用docker安装
)
- 拉取镜像
docker pull jenkins/jenkins
- 添加用户
useradd jenkins
- 挂载目录
mkdir -p /home/jenkins #新增jenkins目录
vim /home/jenkins/log.properties #编辑日志配置文件
添加如下配置:
handlers=java.util.logging.ConsoleHandler
jenkins.level=FINEST
java.util.logging.ConsoleHandler.level=FINEST
- 赋予权限
chmod -R 777 /home/jenkins #给目录赋权
chown -R jenkins:jenkins /home/jenkins #赋予用户权限
chown -R 1000:1000 /home/jenkins
#查看权限是否设置成功
ls -nd /home/jenkins
如下显示证明赋权成功:
drwxrwxrwx 2 1000 1000 4096 Aug 8 10:40 /home/jenkins
- 启动容器
docker run -d --name jenkins \
-p 8080:8080 -p 50000:50000 \
--privileged=true \
--restart=always \
-v /etc/timezone:/etc/timezone \
-v /etc/localtime:/etc/localtime \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(which docker):/usr/bin/docker \
-v /home/jenkins:/var/jenkins_home \
-v /usr/bin/mv:/usr/bin/mv \
-v /usr/java/jdk-11.0.4/bin/java:/usr/local/jdk/bin/java \
-v /usr/local/maven:/usr/local/maven \
-v /usr/java/jdk-11.0.4:/usr/local/jdk \
-v /usr/lib64/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7 \
-u 0 jenkins/jenkins \
--prefix=/jenkins
- 访问jenkins控制台
http://ip:8080/jenkins,提示输入密码,因将jenkins容器挂载至 /home/jenkins,直接执行如下命令:
cat /home/jenkins/secrets/initialAdminPassword
若提示该实例似乎已离线,参考yum安装方式该问题处理办法即可.