環境說明:
CentOS Linux release 7。5。1804 (Core)
Jenkins介紹
:Jenkins是開源CI&CD軟體領導者, 提供超過1000個外掛來支援構建、部署、自動化, 滿足任何專案的需要
1、下載安裝
1.1、下載
開啟
https://jenkins。io/zh/download/
,根據作業系統選擇下載連結,我這邊 選擇的是jenkins2。222。1版本
注意:不同jenkins版本需要匹配的java版本
2。164 (2019-02) and newer: Java 8 or Java 11
2。54 (2017-04) and newer: Java 8
1。612 (2015-05) and newer: Java 7
1.2、安裝
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
2、啟動
2.1、檢視jenkins路徑
[root@VM_0_16_centos data]# rpm -ql jenkins
/etc/init。d/jenkins #伺服器啟動檔案
/etc/logrotate。d/jenkins
/etc/sysconfig/jenkins #配置檔案
/usr/lib/jenkins
/usr/lib/jenkins/jenkins。war #war包檔案目錄
/usr/sbin/rcjenkins
/var/cache/jenkins
/var/lib/jenkins
/var/log/jenkins #日誌檔案路徑
分析:
jenkins預設的埠是8080,可以在配置檔案裡面修改,啟動方式也有兩種,一種是使用系統服務啟動,第二種是使用war包啟動,這裡面推薦使用系統服務啟動。
2.2、啟動方式
war包啟動
nohup java -jar /usr/lib/jenkins/jenkins。war ——httpPort=8080 &
系統服務啟動
systemctl start jenkins
Systemctl status jenkins
注意:啟動會提示找不到java命令,這時候可以修改啟動服務檔案vim /etc/init。d/jenkins,找到java選項,修改為伺服器上面的java路徑(/usr/local/jdk1。8/bin/java),如下圖:
根據伺服器實際情況修改java命令路徑
3、jenkins配置代理
3.1、使用nginx配置https協議,注意域名和埠修改
upstream jenkinss {
keepalive 32; # keepalive connections
server 127。0。0。1:8080; # jenkins ip and port
}
server {
listen 443 ssl; # Listen on port 80 for IPv4 requests
server_name jenkins。work。com;
root /var/run/jenkins/war/;
ssl_certificate cert/2714038__iworkgo。com。pem;
ssl_certificate_key cert/2714038__iworkgo。com。key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1。1 TLSv1。2;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access。log;
error_log /var/log/nginx/error。log;
ignore_invalid_headers off; #pass through headers from Jenkins which are considered invalid by Nginx server。
location ~ “^/static/[0-9a-fA-F]{8}\/(。*)$” {
#rewrite all static files into requests to the root
#E。g /static/12345678/css/something。css will become /css/something。css
rewrite “^/static/[0-9a-fA-F]{8}\/(。*)” /$1 last;
}
location /userContent {
#have nginx handle all the static requests to the userContent folder files
#note : This is the $JENKINS_HOME dir
root /var/lib/jenkins/;
if (!-f $request_filename){
#this file does not exist, might be a directory or a /**view** url
rewrite (。*) /$1 last;
break;
}
sendfile on;
}
location / {
sendfile off;
proxy_pass http://jenkinss;
proxy_redirect default;
proxy_http_version 1。1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_max_temp_file_size 0;
#this is the maximum upload size
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffering off;
proxy_request_buffering off; # Required for HTTP CLI commands in Jenkins > 2。54
proxy_set_header Connection “”; # Clear for keepalive
}
}
注意:
在構建時間長的時候,控制檯會報502 bad gateway,檢視nginx的錯誤日誌,提示:
upstream prematurely closed connection while reading response header from upstream
解決方法:
增加了keepalive_timeout 0的配置
3.2、使用nginx配置http協議,注意域名和埠修改
upstream jenkins {
keepalive 32; # keepalive connections
server 127。0。0。1:8080; # jenkins ip and port
}
server {
listen 80; # Listen on port 80 for IPv4 requests
server_name jenkins。work。com;
#this is the jenkins web root directory (mentioned in the /etc/default/jenkins file)
root /var/run/jenkins/war/;
access_log /var/log/nginx/access。log;
error_log /var/log/nginx/error。log;
ignore_invalid_headers off; #pass through headers from Jenkins which are considered invalid by Nginx server。
location ~ “^/static/[0-9a-fA-F]{8}\/(。*)$” {
#rewrite all static files into requests to the root
#E。g /static/12345678/css/something。css will become /css/something。css
rewrite “^/static/[0-9a-fA-F]{8}\/(。*)” /$1 last;
}
location /userContent {
#have nginx handle all the static requests to the userContent folder files
#note : This is the $JENKINS_HOME dir
root /var/lib/jenkins/;
if (!-f $request_filename){
#this file does not exist, might be a directory or a /**view** url
rewrite (。*) /$1 last;
break;
}
sendfile on;
}
location / {
sendfile off;
proxy_pass http://jenkins;
proxy_redirect default;
proxy_http_version 1。1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_max_temp_file_size 0;
#this is the maximum upload size
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffering off;
proxy_request_buffering off; # Required for HTTP CLI commands in Jenkins > 2。54
proxy_set_header Connection “”; # Clear for keepalive
}
}
4、圖形化安裝外掛過程
瀏覽器輸入伺服器的ip地址+埠號或者域名,彈出getting Started頁面,輸入密碼,初始化密碼檢視(cat /var/lib/jenkins/secrets/initialAdminPassword)
然後就選擇“安裝推薦的外掛”,失敗了可以重新嘗試,多試幾次,安裝失敗也沒有關係,後面下載外掛手動安裝也可以,如下:
安裝外掛完成後,設定使用者名稱:admin 密碼:jenkins,登入後,如下圖: