DevOps

20230227-Jenkins+Gogs自动远程部署djang

2023-02-26  本文已影响0人  負笈在线

1.Jenkins安装或确认必要插件

jenkins安装或确认必要插件gitlab、Publish Over SSH。
Dashboard--Manage Jenkins--Plugin Manager


2.Publish Over SSH配置

jenkins配置SSH连接django服务部署的对象服务器
Dashboard--Manage Jenkins--Configure System,找到 Publish over SSH中SSH Server。输入目标服务器IP、登录用户、登录用户密码等,最后Test Configuration


3.Jenkins中创建Job

Dashboard--New Item--输入Item name--选择Freestyle project--点击OK

配置Job

Source Code Management :Configure--General--Source Code Management--Git--输入Repository URL--选择登录gogs的用户Credentials--选择Branch----Save;


      Build Triggers:选择Trigger builds remotely--Authentication Token;选择Build when a change is pushed to Gogs
      Build Environment :选择Send files or execute commands over SSH before the build starts--设置SSH Server、Transfers

注意Transfers Set Source files、Remote directory、Exec command,其中Exec Command中项目执行sh脚本参考如下:

#!/bin/sh
#项目工作目录
workspace="/root/local_cmdb"
#uwsgi日志输出目录
log_path="/root/local_cmdb/local_cmdb/uwsgi.log"
#设置pip source
pip_source="https://pypi.tuna.tsinghua.edu.cn/simple"
#安装项目需要python、python-devel、mysql-devel
yum install -y python39 gcc python39-devel mysql-devel
if [ $? -ne 0 ]; then
    echo " python etc install failed"  `date +%Y%m%d%H%M%S` >> $log_path
fi
#配置Python、pip环境
ln -s /usr/bin/python3.9 /usr/bin/python
ln -s /usr/bin/pip3.9 /usr/bin/pip
#安装项目需要nginx
yum install nginx -y
if [ $? -ne 0 ]; then
    echo " nginx install failed" `date +%Y%m%d%H%M%S` >> $log_path
fi
#设置nginx自启动
systemctl enable nginx
systemctl start nginx
#安装uwsgi以及django依赖包
pip install uwsgi -i $pip_source 
pip install -r $workspace/requirements.txt -i $pip_source
if [ $? -ne 0 ]; then
    echo "uwsgi etc install failed" `date +%Y%m%d%H%M%S` >> $log_path
fi
#python $workspace/manage.py runserver

#nginx config
cp -pf /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak`date +%Y%m%d%H%M%S`
cat /dev/null /etc/nginx/nginx.conf
cp -pf $workspace/config/nginx.conf /etc/nginx/nginx.conf
nginx_procnum = `ps -C nginx --no-header|wc -l`
if [$nginx_procnum -eq 0]
then 
    echo "start nginx" `date +%Y%m%d%H%M%S` >> $log_path
    systemctl start nginx
else
    echo "restart nginx"  `date +%Y%m%d%H%M%S` >> $log_path
    systemctl restart nginx.service
fi
systemctl status nginx.service


###uwsgi config
cp -pf $workspace/config/server_uwsgi.service /etc/systemd/system/server_uwsgi.service
systemctl enable /etc/systemd/system/server_uwsgi.service
systemctl daemon-reload
uwsgi_procnum = `ps -C uwsgi --no-header|wc -l`
if [$uwsgi_procnum -eq 0]
then 
    echo "start uwsgi"  `date +%Y%m%d%H%M%S` >> $log_path
    systemctl start server_uwsgi
else
    echo "restart uwsgi"  `date +%Y%m%d%H%M%S` >> $log_path
    systemctl restart server_uwsgi
fi
systemctl status server_uwsgi.service

4.gogs配置web钩子

选择我的仓库--仓库设置--管理Web钩子--添加Web钩子--选择Gogs--填写推送地址、选择数据格式、密钥文本可免、设置希望触发Web钩子的事件、选择激活--点击添加Web钩子。参考20230129-Jenkins+Gogs自动打包项目

5.测试

开发环境提交代码

下载项目源码

# git clone http://172.26.37.127:3000/luorf/CMDB2.git
# cd CMDB2

修改项目安装脚本文件

# vi local_cmdb.sh

提交修改

# git add .
# git commit -m 'local_cmdb.sh.11'
# git push origin master

gogs自动推送确认:


jenkins脚本执行确认:


远程服务器nginx、uwsgi服务启动正常,django web访问及利用正常

# systemctl status nginx.service
# systemctl status server_uwsgi.service 
# netstat -ant |grep 80
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN     
# netstat -ant |grep 81
tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN 
# curl -I http://172.26.37.161:81/admin

参考URL

https://blog.csdn.net/questions1234/article/details/120291055
https://blog.csdn.net/gbfeng123/article/details/120025234

上一篇下一篇

猜你喜欢

热点阅读