| 备注 | 修改日期 | 修改人 |
| 修改标题 | 2025-11-23 00:28:44[当前版本] | 文艺范儿 |
| 格式调整 | 2025-11-22 19:07:29 | 文艺范儿 |
| 创建版本 | 2025-11-22 19:05:54 | 文艺范儿 |
a.创建项目
b.配置连接的gitlab项目地址
注意:这里我已经在jenkins服务器生成密钥对,并在gitlab页面上把jenkins的公钥添加了的,添加完成之后还需要在jenkins服务器中执行一次clone项目的命令,输入一次yes。不然上面页面会报无法连接仓库的错误。
c.点保存后,立即构建进行测试。
可以在jenkins服务器项目目录下查看是否拉取游戏代码成功。
[root@201-jenkins ~]# ll /var/lib/jenkins/workspace/game_job/
总用量 48
-rw-r--r-- 1 root root 28032 11月 19 01:04 bgm.mp3
drwxr-xr-x 2 root root 23 11月 19 01:04 css
drwxr-xr-x 2 root root 23 11月 19 01:04 images
-rw-r--r-- 1 root root 8956 11月 19 01:04 index.html
drwxr-xr-x 2 root root 213 11月 19 01:04 js
drwxr-xr-x 2 root root 4096 11月 19 01:04 roms
-rw-r--r-- 1 root root 811 11月 19 01:04 shuoming.html
1.##在web服务器上安装nginx并配置
[root@204-web vhost]# cd /home/deploy/nginx/conf/vhost/
[root@204-web vhost]# cat game.conf
server {
listen 80;
server_name game.wyasw.com;
location / {
root /home/deploy/nginx/html/game/ ;
index index.html index.htm index.php;
}
}
2.##jenkins和web服务器做免密钥
[root@201-jenkins ~]# ssh-copy-id -i .ssh/id_rsa.pub 10.0.0.204
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"
The authenticity of host '10.0.0.204 (10.0.0.204)' can't be established.
ECDSA key fingerprint is SHA256:Jr3K8c9P67/4Nvy8jn7FFmFrNBVcKW5dVzpUNW2aVNo.
ECDSA key fingerprint is MD5:fe:26:f2:56:60:1b:3c:52:80:28:26:c1:4d:ec:e0:a3.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.0.0.204's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '10.0.0.204'"
and check to make sure that only the key(s) you wanted were added.
[root@201-jenkins ~]# ssh '10.0.0.204'
Last login: Mon Nov 17 00:23:33 2025 from 10.0.0.1
[root@204-web ~]# exit
登出
Connection to 10.0.0.204 closed.
shell命令:
DATE=$(date +%Y-%m-%d-%H-%M-%S)
CODE_DIR="./"
WEB_DIR="/home/deploy/nginx/html/"
WEB_IP="10.0.0.204"
get_code_tar(){
cd $CODE_DIR && tar zcf /opt/web-$DATE.tar.gz ./*
}
scp_code_web(){
scp /opt/web-$DATE.tar.gz $WEB_IP:$WEB_DIR
}
code_tarxf(){
ssh $WEB_IP "cd $WEB_DIR && mkdir web-$DATE && tar xf web-$DATE.tar.gz -C web-$DATE"
}
ln_html(){
ssh $WEB_IP "cd $WEB_DIR && rm -rf game && ln -s web-$DATE game"
}
main(){
get_code_tar;
scp_code_web;
code_tarxf;
ln_html;
}
main
浏览器访问web服务器显示成功
到gitlab页面:
配置
完成后点击:添加Webhooks
测试:修改代码推送到gitlab仓库的游戏项目中,会自动触发Jenkins拉取代码并将拉取到的修改后的代码推送到WEB服务器。