Jenkins是一个开源的支持自动化构建、部署等任务的平台。基本上可以说是持续集成(CI)、持续发布(CD)不可或缺的工具。
Jenkins 官网
准备工作
本篇环境信息
工具/环境 |
版本 |
Linux Server |
CentOS7 |
Jenkins |
2.222.4 |
JDK |
1.8.0_212 |
Nginx |
1.15.1 |
1 2 3 4
| [root@caoxl ~]# java -version openjdk version "1.8.0_212" OpenJDK Runtime Environment (build 1.8.0_212-b04) OpenJDK 64-Bit Server VM (build 25.212-b04, mixed mode)
|
1 2 3 4
| [root@caoxl ~]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.15.1 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) configure arguments:
|
1 2
| [root@caoxl ~]# cat /etc/sysconfig/jenkins | grep "JENKINS_PORT" JENKINS_PORT="8080"
|
安装Jenkins
Yum安装
1 2 3 4 5
| #添加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
|
1
| sudo yum install -y jenkins
|
开放端口
Jenkins站点的默认监听端口是8080
1 2
| sudo firewall-cmd --add-port=8080/tcp --permanent sudo firewall-cmd --reload
|
配置Java可选路径
因为Jenkins默认的java可选路径不包含我们部署的jdk路径,所以这里要配置一下,不然Jenkins服务会启动失败
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #修改jenkins启动脚本 sudo vi /etc/init.d/jenkins
#修改candidates增加java可选路径:/usr/java/jdk1.8.0_181/bin/java candidates=" /etc/alternatives/java /usr/lib/jvm/java-1.8.0/bin/java /usr/lib/jvm/jre-1.8.0/bin/java /usr/lib/jvm/java-1.7.0/bin/java /usr/lib/jvm/jre-1.7.0/bin/java /usr/lib/jvm/java-11.0/bin/java /usr/lib/jvm/jre-11.0/bin/java /usr/lib/jvm/java-11-openjdk-amd64 /usr/bin/java "
|
启动Jenkins并设置Jenkins开机启动
1 2 3 4 5 6 7 8 9
| #重载服务(由于前面修改了Jenkins启动脚本) sudo systemctl daemon-reload
#启动Jenkins服务 sudo systemctl start jenkins
#将Jenkins服务设置为开机启动 #由于Jenkins不是Native Service,所以需要用chkconfig命令而不是systemctl命令 sudo /sbin/chkconfig jenkins on
|
浏览器输入 http://<ip address>:8080
访问Jenkins
修改Jenkins端口
1 2 3 4 5
| vim /etc/sysconfig/jenkins JENKINS_PORT="8080" // 修改成自己需要的端口即可
// 重新启动jenkins systemctl restart jenkins
|
配置Nginx反向代理Jenkins
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #新增Jenkins专用Nginx配置文件 sudo vi /etc/nginx/conf.d/jenkins.conf
#输入以下内容并保存 server { listen 80;#监听80端口 server_name jenkins.caoxl.com;#监听的域名 access_log /var/log/nginx/jenkins.access.log main; error_log /var/log/nginx/jenkins.error.log error;
location / { #转发或处理 proxy_pass http://127.0.0.1:8080; } error_page 500 502 503 504 /50x.html;#错误页 location = /50x.html { root /usr/share/nginx/html; } }
|
报-bash: nginx: command not found
, 解决办法
1 2 3
| [root@caoxl sbin]# vi /etc/profile export PATH=$PATH:/usr/local/nginx/sbin // Nginx可执行文件路径 [root@caoxl sbin]# source /etc/profile
|
解决Jenkins插件下载慢的问题
修改插件下载链接
1 2 3 4 5 6 7 8 9
| vim /var/lib/jenkins/hudson.model.UpdateCenter.xml
# 修改 https://updates.jenkins.io/update-center.json # 为 http://mirror.xmission.com/jenkins/updates/update-center.json
# 重新启动jenkins systemctl restart jenkins
|
修改default.json
cd 到updates
目录,找到default.json
文件
1 2 3 4 5 6 7
| [root@caoxl updates]# pwd /var/lib/jenkins/updates [root@caoxl updates]# ll total 2.2M -rw-r--r-- 1 jenkins jenkins 1.9M Jun 2 14:22 default.json -rw-r--r-- 1 jenkins jenkins 5.3K Jun 2 14:22 hudson.tasks.Maven.MavenInstaller -rw-r--r-- 1 jenkins jenkins 274K Jun 2 14:53 hudson.tools.JDKInstaller
|
使用vi编辑文件,如下,替换所有插件下载的url
1
| :1,$s/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g
|
替换连接测试url
1
| :1,$s/http:\/\/www.google.com/https:\/\/www.baidu.com/g
|
进入vi先输入:
然后再粘贴上边的:
后边的命令,注意不要写两个冒号!
参考