栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Jenkins搭建前后端分离项目流水线实战

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Jenkins搭建前后端分离项目流水线实战

文章目录
  • 项目简介
  • 流水线建设
    • 前端流水线pipline script
    • nginx配置流水线pipline script
    • 后端流水线pipline script
    • 后端数据库初始化流水线pipline script

项目简介

飞雪测试平台是我做的一个Django+Vue前后端分离demo项目。主要用于日志管理,主要是兴趣驱动,体验一下全栈研发。顺便记录一下流水线搭建的一些关键代码。

前端地址:https://github.com/tdx1997tdx/my_tapd_frontend
后端地址:https://github.com/tdx1997tdx/my_tapd_backend
demo地址:http://139.198.36.243

流水线建设

对于这个项目建立了4条流水线

前端流水线pipline script
pipeline {
    agent any

    stages {
        stage('git clone') {
            steps {
                git credentialsId: 'f4209992-d0c8-4f10-bcea-c5edaa0846f1', url: 'https://github.com/tdx1997tdx/my_tapd_frontend.git'
            }
        }
        
        stage('npm build') {
            steps {
                sh 'npm install'
                sh 'npm run build'
            }
        }
        
        stage('copy files to nginx') {
            steps {
                sh 'rm -rf /usr/local/nginx/html/*'
                sh 'cp -r ./dist/* /usr/local/nginx/html/'
            }
        }
    }
}

主要流程就是npm打包出来的产物转移到nginx上

nginx配置流水线pipline script
pipeline {
    agent any

    stages {
        stage('change_conf') {
            steps {
                writeFile encoding: 'utf-8', file: '/usr/local/nginx/conf/nginx.conf', text: 
'''
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  139.198.36.243;
        
        location / {
            root   html;
            index  index.html index.htm;
        }
        
        location /api/ {
            proxy_pass   http://127.0.0.1:8080;
        }
    }
}
'''
            }
        }
        stage('restart nginx') {
            steps {
                sh 'systemctl restart nginx.service'
            }
        }
    }
}

对于静态文件直接获取,对于后端接口,也就是api开头的,转发到8080端口

后端流水线pipline script
pipeline {
    agent any

    stages {
        stage('git pull') {
            steps {
                git credentialsId: 'f4209992-d0c8-4f10-bcea-c5edaa0846f1', url: 'https://github.com/tdx1997tdx/my_tapd_backend.git'
            }
        }
        stage('start') {
            steps {
                sh 'killall gunicorn || true'
                sh 'JENKINS_NODE_COOKIE=dontKillMe nohup /usr/local/python3/bin/gunicorn my_tapd_backend.wsgi -w 4 -b 0.0.0.0:8080 &'
            }
        }
    }
}

后端数据库初始化流水线pipline script
pipeline {
    agent any

    stages {
        stage('git pull') {
            steps {
                git credentialsId: 'f4209992-d0c8-4f10-bcea-c5edaa0846f1', url: 'https://github.com/tdx1997tdx/my_tapd_backend.git'
            }
        }
        
        stage('migrate') {
            steps {
                sh 'pip3 install -r requirements.txt'
                sh 'python3 manage.py makemigrations'
                sh 'python3 manage.py migrate'
            }
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/866598.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号