" name="sm-site-verification"/>
侧边栏壁纸
博主头像
PySuper博主等级

千里之行,始于足下

  • 累计撰写 203 篇文章
  • 累计创建 14 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

GitLab CI/CD 部署Django项目

PySuper
2021-11-05 / 0 评论 / 0 点赞 / 20 阅读 / 6221 字
温馨提示:
所有牛逼的人都有一段苦逼的岁月。 但是你只要像SB一样去坚持,终将牛逼!!! ✊✊✊

nginx

  • 重写一段nginx的配置
  • 在打镜像的时候,ADD到新的镜像中去
  • 这里要注意指定端口号
server {
    listen 3456;
    charset utf-8;
    client_max_body_size 75M;

    location /static {
        alias /home/project/static/;
    }

    location / {
        uwsgi_pass 0.0.0.0:8000;
        include /etc/nginx/uwsgi_params;
    }
}

uwsgi

  • Django的uwsgi配置文件
  • 在Django项目文件中,一起复制到镜像中
  • 注意端口暴露方式,这会影响访问结果
[uwsgi]

chdir = /home/project
module = test_2.wsgi:application
wsgi-file = /home/project/uwsgi.ini
pyenv = /usr/local/python3/bin/python3
log-format = [%(ctime)] %(method) %(status) %(uri)
;daemonize = /home/project/logs/uwsgi.log
thread = 8
uid = nginx
gid = nginx
vacuum = true
master = true
processes = 8
max-requests = 5000
socket = 0.0.0.0:8000

run.sh

  • run一个容器时候执行的脚本
  • 在放入镜像中后,需要修改sh文件的执行权限
  • 复制文件的时候,注意文件所在位置
echo "服务启动中..."
nginx
/usr/local/python3/bin/uwsgi --ini /home/project/uwsgi.ini
echo "服务启动成功..."
sleep 3600h  # 只是设置了等待时间

docker

  • 如果需要修改Python版本,直接修改所有Python-3.8.10
  • 也可以先生成一个基础镜像,再一点点丰富它
FROM centos:7
MAINTAINER zhengxingtao

RUN yum -y update && \
    yum -y groupinstall "Development tools" && \
    yum -y install wget zlib-devel bzip2-devel openssl-devel ncurses-devel && \
    yum -y install sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel && \
    yum -y install xz-devel libffi-devel epel-release gcc python3-devel libsndfile && \
    wget -P  /usr/bin https://www.python.org/ftp/python/3.8.10/Python-3.8.10.tgz && \
    cd /usr/bin && \
    tar -zxvf /usr/bin/Python-3.8.10.tgz && \
    cd /usr/bin/Python-3.8.10 && \
    ./configure --prefix=/usr/local/python3 && make && make install && \
    rm -rf /usr/bin/pip3 && rm -rf /usr/bin/python3 && \
    ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 && \
    ln -s /usr/local/python3/bin/python3 /usr/bin/python3 &&\
    pip3 install --upgrade pip && \
    pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple && \
    pip3 config set install.trusted-host mirrors.aliyun.com && \
    /usr/local/python3/bin/python3.8 -m pip install --upgrade pip && \
    pip3 install django pymysql uwsgi pyfiglet && \
    cp /usr/local/python3/bin/uwsgi /usr/sbin/ && \
    yum install -y nginx && \
    mkdir /home/project/
ADD ./test_2/ /home/project/
ADD ./test_2/site.conf /etc/nginx/conf.d/
RUN nginx -c /etc/nginx/nginx.conf && \
    nginx -s reload && \
    chmod +x /home/project/run.sh && \
    cd /home/project/ &&  \
    chmod 777 /home/project/run.sh
ENTRYPOINT ["/bin/bash", "-c", "/home/project/run.sh"]

yaml

  • Docker登录的设置Docker的配置文件
  • 部署的时候,需要先配置免密码登录
variables:
  DOCKER_REPO_URL: "192.168.189.111"
  DOCKER_REPO_USERNAME: zheng
  DOCKER_REPO_PASSWORD: zheng
  DOCKER_PROJECT_NAME: gitlab
  DOCKER_IMAGE_NAME: project:v4
  TEST_SERVER_IP: "192.168.189.141"

before_script:
  - export PYTHONPATH=$PYTHONPATH:/usr/bin/python3

after_script:
  # 运行完成之后删除,避免影响下次Pipline
  - rm -rf /home/gitlab-runner/builds/RZDb_hKv/0/zheng

stages:
  - test
  - build
  - deploy

test:
  stage: test
  image: 192.168.189.111/epgn/python:3.8
  script:
    # 测试tests.py文件
    # 这里对于不同的项目需要执行不同的测试文件
    - /usr/local/bin/python -m pip install --upgrade pip && pip3 install django && pip3 install pymysql
    - cd test_2/user && /usr/local/bin/python tests.py
  tags:
    - Demo

build:
  # build的时候应该分成两部分:
  # 先测试能否正常打成镜像
  # 再把项目打入到镜像里面去
  # 最好把这里的每个阶段分开执行
  stage: build
  script:
    # 构建和上传镜像
    - docker build -f Dockerfile -t $DOCKER_REPO_URL/$DOCKER_PROJECT_NAME/$DOCKER_IMAGE_NAME .
    - docker login $DOCKER_REPO_URL -u $DOCKER_REPO_USERNAME -p $DOCKER_REPO_PASSWORD
    - docker push $DOCKER_REPO_URL/$DOCKER_PROJECT_NAME/$DOCKER_IMAGE_NAME
    - docker rmi $DOCKER_REPO_URL/$DOCKER_PROJECT_NAME/$DOCKER_IMAGE_NAME
  tags:
    - Shell

deploy:
  stage: deploy
  script:
    # 以容器启动项目
    - ssh $TEST_SERVER_IP "figlet django start && docker login $DOCKER_REPO_URL -u$DOCKER_REPO_USERNAME -p$DOCKER_REPO_PASSWORD && docker pull $DOCKER_REPO_URL/$DOCKER_PROJECT_NAME/$DOCKER_IMAGE_NAME && docker run --name=project --hostname=project -p 12321:3456 -dit $DOCKER_REPO_URL/$DOCKER_PROJECT_NAME/$DOCKER_IMAGE_NAME && docker ps | grep project && figlet welcome to kubernetes && echo '项目链接:http://192.168.189.141:12321/'"
  tags:
    - Shell

0

评论区