Jenkins自动化部署项目

一. Jenkins自动化部署django项目

  1. 用到的技术栈

    Jenkins + supervisor + nginx + django + gunicornhtml

  2. gunicorn的使用

- 使用gunicorn启动django项目
    gunicorn -w 3 -b 0.0.0.0:8001 projectdir.wsgi:application

  

  3. supervisor的使用 

- 启动supervisor服务端
    python /usr/local/bin/supervisord    # 使用默认配置启动
    python /usr/local/bin/supervisird -c ./xxx.conf    # 使用定制配置启动 

- supervisor项目的配置

[program:projectname]
command=/usr/local/bin/gunicorn -w 3 -b 0.0.0.0:7000 projectdir.wsgi:application
directory=/home/projectdir/
autostart=true
autorestart=true

- supervisor的几个经常使用指令
    supervisorctl start    # 开启配置项目
    supervisorctl restart/reload    # 重启
    supervisorctl update    # 启动新项目,重启修改的项目

  4. nginx的配置

- 普通的配置
    server {
            listen   80;
            server_name  192.168.1.95;	# nginx对外地址

            location / {
                proxy_pass http://0.0.0.0:8001;	# 代理地址
            }

            location /static/ {
                 alias /opt/yl/apps/static/;	# 静态文件
            }
        }

- vue的配置
        server {
                listen       8003;
                server_name  192.168.1.95;
                location / {
                    root /opt/vue_project/dist;
                    index index.html;
                }
            }

  

  

  5. Jenkins的简单使用

- Jenkins配置
    Repository URL(github/gitee远程仓库地址)
    Credentials(帐号密码)
    构建触发器(gitee插件, gitee webhook)
    构建shell(重启supervisor + nginx)
    构建后(发送邮件等)

- 构建时部署django项目的shell
    sudo supervisorctl update
    sudo /etc/init.d/nginx reload

- 构建时部署vue项目的shell
  sudo npm install          # 下载工做区的npm包
  sudo rm -rf ./dist/*       # 删除以前的, 从新进行打包
  sudo npm run build        # 进行打包
  sudo rm -rf /www/web/site/*  # 删除服务器上以前存放dist文件
  sudo cp -rf ./dist/* /www/web/site  # 拷贝新的dist文件到项目