github flask常用workflow

  1. workflow是干啥的
    在代码开发中,作为build pipeline的手段,可以帮助校验检查代码合规性,防止bug上线
  2. 首先在项目根目录中新建文件
.github/workflows/build.yml
  1. build.yml内容
name: Build //检验名称
on: [push] // 在进行Push操作时进行校验
jobs:
  python-formatting: //	自定义名称为python样式校验的job
    runs-on: ubuntu-latest //定义running env
    steps:
      - name: Checkout //基础配置
        uses: actions/checkout@v3
      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: 3.9
      - name: Install flake8 // 使用flake8进行代码样式校验
        run: pip install flake8
      - name: Run flake8 // 在env中运行flake8命令进行校验
        run: flake8 api/
  run-unit-tests:
    needs: python-formatting
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: 3.9
      - name: Install flask //从这里开始下载所有pytest需要的安装包
        run: pip install flask
      - name: Install requests
        run: pip install requests
      - name: Install pytest //进行pytest
        run: pip install pytest
      - name: Run Unit Tests
        run: pytest api/*.py
  deploy-to-vercel: //后续deploy的action
    needs: run-unit-tests
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Deploy to Vercel
        uses: amondnet/vercel-action@v20
        with:
          vercel-token: ${{ secrets.VERCEL_TOKEN }}
          vercel-org-id: ${{ secrets.VERCEL_ORG_ID }}
          vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }}
          vercel-args: '--prod'
   
  1. 在本机上的操作
source .venv/bin/activate //进入虚拟环境
 pip install flake8 // 下载flake8插件
 flake8 api/*.py //校验api文件内的所有.py文件格式是否合规(基于flask框架,主代码放在api的.py中)
 pip install black // black是一个可以自动美化代码格式的插件
 black api/*.py //美化所有api中的.py代码
 pip freeze > requirements.txt //生成requirements.txt, 里面包含所有需要的安装包
 pip install pytest //进行pytest
 pytest app_test.py //执行pytest,检查app_test.py中的单元测试函数输出结果是否全部通过

你可能感兴趣的:(github,flask,python)