官方文档
添加软件源
为了安装最新稳定版的 Jenkins ,这里添加官方的软件源进行安装
# 下载密钥
wget -O /usr/share/keyrings/jenkins-keyring.asc https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
# 添加软件源
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" https://pkg.jenkins.io/debian-stable binary/ | tee /etc/apt/sources.list.d/jenkins.list > /dev/null
安装
自从 2.346.1 版本开始 Jenkins 支持 Java17 版本的运行环境,因此这里我也安装了 openjdk17 作为 Jenkins 的运行环境
apt update && apt install fontconfig openjdk-17-jdk -y && apt install jenkins -y
配置
因为我的 Jenkins 版本使用的是 Java17 版本的运行环境,但我的代码编译使用的是 Java8 版本的运行环境,因此为了保证 Jenkins 的正常运行,我在 Jenkins 的 service 服务文件中指定了 JAVA_HOME 变量的值为 openjdk17 的安装目录
# service 服务文件默认位置
vim /lib/systemd/system/jenkins.service
# 找到 JAVA_HOME 行进行修改
Environment="JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64"
# 重新加载服务文件并重启 Jenkins
systemctl daemon-reload && systemctl restart jenkins.service
反向代理
不配置反向代理的情况下可以直接通过 Jenkins 的监听端口进行访问,如 http://localhost:8080。
通过使用 nginx 进行反向代理可以实现静态资源与后端分离提高运行性能,所以这里我在 Jenkins 所运行的服务器又安装了 nginx ,在线安装 nginx 的方式可参考文章:
https://blog.csdn.net/weixin_70520101/article/details/151075856?spm=1011.2415.3001.10575&sharefrom=mp_manage_linkhttps://blog.csdn.net/weixin_70520101/article/details/151075856?spm=1011.2415.3001.10575&sharefrom=mp_manage_linknginx 配置内容如下,server_name 我配置了内网域名,根据自己的实际情况进行调整,如果不配置域名可直接使用 IP 进行访问
upstream jenkins {
keepalive 32; # keepalive connections
server 127.0.0.1:8080; # jenkins ip and port
}
# Required for Jenkins websocket agents
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name jenkins.example.com;
# this is the jenkins web root directory
# (mentioned in the output of "systemctl cat jenkins")
root /var/cache/jenkins/war/;
access_log /var/log/nginx/jenkins.access.log;
error_log /var/log/nginx/jenkins.error.log;
# pass through headers from Jenkins that Nginx considers invalid
ignore_invalid_headers off;
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 userContent folder
# 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;
# Required for Jenkins websocket agents
proxy_set_header Connection $connection_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $http_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 100m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_request_buffering off; # Required for HTTP CLI commands
}
}
配置完成后检测配置是否正确并加载配置文件使其生效
nginx -t && nginx -s reload
之后就可通过配置的域名或服务器 IP 进行访问 Jenkins 控制台