Jenkins

本文档使用的是Jenkins:2.452.2版本,使用的war包

官网

Jenkins Shell脚本

停止脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash

# 获取jenkins的pId
pId=$(ps -ef | grep jenkins | grep -v grep | awk '{ print $2 }')

# 输出pId
echo "---------------- jenkins的pId:$pId ----------------"

# 判断pId是否为空
if [ -z $pId ]; then
echo "---------------- jenkins未启动 ----------------"
else
kill -9 $pId
echo "---------------- 项目停止成功 ----------------"
# 检查项目是否停止成功
check=$(ps -ef | grep jenkins | grep -v grep | awk '{ print $2 }')
if [ -z $check ]; then
echo "---------------- jenkins:$pId已停止 ----------------"
else
echo "---------------- jenkins-pId:$pId停止失败 ----------------"
fi
fi

启动脚本

—httpPort用于指定启动端口

1
2
3
4
5
6
7
8
9
#!/bin/bash

# 开始关闭jenkins
./stop.sh

# jenkins后台启动开始
echo "---------------- jenkins后台启动开始 ----------------"
nohup java -jar jenkins.war --httpPort=8200 > jenkins.log 2>&1 &
echo "---------------- jenkins启动成功 ----------------"

nginx反向代理

需要修改地方

  • upstream
    • server
  • server
    • listen
    • server_name
    • ssl_certificate
    • ssl_certificate_key
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
upstream jenkins {
keepalive 32;
server 127.0.0.1:8200;
}

map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

server {
listen 访问端口 ssl;
server_name www.www.www;

# ssl证书地址
ssl_certificate /usr/local/nginx/crt/www.www.www.pem; # pem文件的路径
ssl_certificate_key /usr/local/nginx/crt/www.www.www.key; # key文件的路径

# ssl验证相关配置
ssl_session_timeout 5m; #缓存有效期
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #加密算法
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #安全链接可选的加密协议
ssl_prefer_server_ciphers on; #使用服务器端的首选算法

ignore_invalid_headers off;

location ~ "^/static/[0-9a-fA-F]{8}\/(.*)$" {
rewrite "^/static/[0-9a-fA-F]{8}\/(.*)" /$1 last;
}

location /userContent {
root /var/lib/jenkins/;
if (!-f $request_filename){
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 10m;
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
}

}

插件下载推荐

  • Blue Ocean:Pipline流水线可视化项目
  • Gitee Plugin
  • Maven Integration:用于创建Maven项目
  • Generic Webhook Trigger:使用Webhook远程触发

Pipline流水线

官方文档

语法

1
2
3
4
5
pipeline:整条流水线
agent:指定执行器
stages:所有阶段
stage:某一阶段,可有多个
steps:阶段内的每一步,可执行命令

使用Pipline部署SpringBoot项目

创建一个Pipline项目

image-20240622142030163

image-20240622142222813

编写Jenkinsfile文件

Jenkinsfile文件放在代码里面

Jenkinsfile文件内容使用的是Pipline语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
pipeline {
agent any

stages {
stage('拉取代码') {
steps {
echo '开始从git仓库拉取代码'
git branch: '分支名称', url: '代码仓库地址'
}
}

stage('maven构建项目') {
steps {
echo '开始使用maven构建项目'
sh 'mvn clean package -DskipTests'
}
}

stage('部署前清理') {
steps {
// 执行脚本时可能报错,使用catchError捕获异常,避免报错后下面步骤不执行了
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh '/xiaofei/xiaofei/clear.sh'
}
}
}

stage('移动jar包') {
steps {
echo '开始移动jar包到构建目录'
sh 'cp xiaofei-admin/target/xiaofei-admin.jar /xiaofei/xiaofei/'
}
}

stage('部署应用') {
steps {
sh 'docker-compose -f /xiaofei/docker-compose.yml up -d xiaofei '
}
}
}

post {
success {
echo 'Pipeline 执行成功,应用部署完成!'
}
failure {
echo 'Pipeline 执行失败,需要进行错误处理!'
}
}
}

Jenkins Linux Shell启动脚本

启动脚本

java -jar 启动Jenkins时,指定jdk的全路径,避免使用systemctl开机启动脚本时找不到Java报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash

# 防止Jenkins部署项目时,使用 nohup 后台启动项目,将进程杀掉
export BUILD_ID=dontKillMe
export JENKINS_NODE_COOKIE=dontKillMe

log_file="jenkins.log"

# 函数:启动Jenkins服务
start_jenkins() {
echo -e "\n---------------- Jenkins后台启动开始 ----------------" | tee -a $log_file
# /usr/local/graalvm-ce-java17-22.3.3/bin/java 指定Java环境的全路径
nohup /usr/local/graalvm-ce-java17-22.3.3/bin/java -jar jenkins.war --httpPort=8200 > $log_file 2>&1 &
sleep 3
}

# 函数:获取Jenkins的进程ID
get_jenkins_pid() {
pgrep -f 'java.*jenkins.war'
}

# 函数:检查Jenkins是否启动成功
check_jenkins() {
local pId=$(get_jenkins_pid)
echo -e "\n---------------- Jenkins的pId:$pId ----------------" | tee -a $log_file
if [ -z "$pId" ]; then
echo -e "\n---------------- Jenkins启动失败 ----------------" | tee -a $log_file
return 1
else
echo -e "\n---------------- Jenkins启动成功 ----------------" | tee -a $log_file
return 0
fi
}

# 主脚本流程
# 开始关闭Jenkins(确保您的stop.sh脚本正确关闭Jenkins)
# ./stop.sh

# 启动Jenkins
start_jenkins

# 检查Jenkins启动状态
check_jenkins
if [ $? -ne 0 ]; then
echo -e "\n---------------- 重试启动Jenkins ----------------" | tee -a $log_file
start_jenkins
check_jenkins
if [ $? -ne 0 ]; then
echo -e "\n---------------- Jenkins启动仍然失败,请检查日志 ----------------" | tee -a $log_file
fi
fi

停止脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash

log_file="jenkins_stop.log"

# 函数:获取Jenkins的进程ID
get_jenkins_pid() {
pgrep -f 'java.*jenkins.war'
}

# 函数:停止Jenkins服务
stop_jenkins() {
local pId=$1
if [[ -z "$pId" || ! "$pId" =~ ^[0-9]+$ ]]; then
echo -e "\n---------------- Jenkins未启动 ----------------" | tee -a $log_file
else
kill -9 "$pId"
echo -e "\n---------------- 项目停止成功 ----------------" | tee -a $log_file
# 检查项目是否停止成功
local check=$(get_jenkins_pid)
if [ -z "$check" ]; then
echo -e "\n---------------- Jenkins:$pId已停止 ----------------" | tee -a $log_file
else
echo -e "\n---------------- Jenkins-pId:$pId停止失败 ----------------" | tee -a $log_file
fi
fi
}

# 主脚本流程
echo -e "\n---------------- Jenkins 开始停止 ---------------- " | tee -a $log_file

pId=$(get_jenkins_pid)

# 输出pId
echo -e "\n---------------- Jenkins的pId:$pId ----------------" | tee -a $log_file

# 停止Jenkins服务
stop_jenkins "$pId"

echo -e "\n---------------- Jenkins 结束停止 ---------------- " | tee -a $log_file