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

本文介绍了使用Jenkins构建的Django+Vue项目的前端、后端及数据库初始化的四条流水线,包括前端的npm打包、静态文件部署到nginx,后端的代码拉取、gunicorn启动,以及数据库的迁移操作。通过这些流水线,实现了项目的自动化部署和更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目简介

飞雪测试平台是我做的一个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'
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值