Docker 未在 Django 应用程序上提交工作服

Coveralls is not being submitted on a Django app with Docker

我正在使用 Docker 开发 Django 项目。我已经配置了 Travis-Ci 并且我想将测试覆盖率提交给 coveralls。但是,它没有按预期工作。任何帮助将不胜感激。

这是我遇到的错误

Submitting coverage to coveralls.io...
No source for /mwibutsa/mwibutsa/settings.py
No source for /mwibutsa/mwibutsa/urls.py
No source for /mwibutsa/user/admin.py
No source for /mwibutsa/user/migrations/0001_initial.py
No source for /mwibutsa/user/models.py
No source for /mwibutsa/user/tests/test_user_api.py
No source for /mwibutsa/user/tests/test_user_model.py
Could not submit coverage: 422 Client Error: Unprocessable Entity for url: https://coveralls.io/api/v1/jobs
Traceback (most recent call last):
  File "/home/travis/virtualenv/python3.7.1/lib/python3.7/site-packages/coveralls/api.py", line 177, in wear
    response.raise_for_status()
  File "/home/travis/virtualenv/python3.7.1/lib/python3.7/site-packages/requests/models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 422 Client Error: Unprocessable Entity for url: https://coveralls.io/api/v1/jobs
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/home/travis/virtualenv/python3.7.1/lib/python3.7/site-packages/coveralls/cli.py", line 77, in main
    result = coverallz.wear()
  File "/home/travis/virtualenv/python3.7.1/lib/python3.7/site-packages/coveralls/api.py", line 180, in wear
    raise CoverallsException('Could not submit coverage: {}'.format(e))
coveralls.exception.CoverallsException: Could not submit coverage: 422 Client Error: Unprocessable Entity for url: https://coveralls.io/api/v1/jobs

**Here is my Travis.yml file**

语言:python python: - “3.7” 服务:docker before_script: pip 安装 docker-compose 脚本: - docker-compose 运行 web sh -c "coverage run manage.py test && flake8 && coverage report" after_success: - 工作服

language: python
python:
  - "3.7"
services: docker
before_script: pip install docker-compose
script:
  - docker-compose run web sh -c "coverage run manage.py test && flake8 && coverage report"
after_success:
  - coveralls

我的Docker文件

FROM python:3.7-alpine
LABEL description="Mwibutsa Floribert"

ENV PYTHONUNBUFFERED 1

RUN mkdir /mwibutsa
WORKDIR /mwibutsa

COPY requirements.txt /mwibutsa/
RUN apk add --update --no-cache postgresql-client jpeg-dev
RUN apk add --update --no-cache --virtual  .tmp-build-deps gcc libc-dev linux-headers postgresql-dev musl-dev zlib zlib-dev

RUN pip install --upgrade pip
RUN pip install -r requirements.txt
RUN apk del .tmp-build-deps


COPY . /mwibutsa/

我的docker-compose.yml

version: '3.7'
services:
  web:
    build: .
    command: >
      sh -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
    environment:
      - DB_HOST=db
      - DB_NAME=postgres
      - DB_PASSWORD=password
      - DB_USER=postgres
      - DB_PORT=5432
    volumes:
      - .:/mwibutsa
    ports:
        - "8000:8000"
    depends_on:
      - db
  db:
    image: postgres:12-alpine
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_PASSWORD=password
      - POSTGRES_USER=postgres
      - POSTGRES_PORT=5432

要了解为什么未提交覆盖范围,您必须了解 docker 容器的运行方式。

创建容器是为了模拟一个独立的单元。这意味着全局上下文中 运行 的命令与容器上下文中 运行 的命令不同。

在您的情况下,您正在 运行 测试并在容器的上下文中生成覆盖率报告,然后尝试从全局上下文向工作服提交报告。

由于文件在容器中,coveralls 命令找不到报告,因此不会提交任何内容。

您可以参考这里提供的答案来解决这个问题:

或者查看 travis 提供的关于如何使用 docker 从 travis 提交工作服的文档: https://docs.travis-ci.com/user/coveralls/#using-coveralls-with-docker-builds

您必须 运行 容器内的工作服才能将覆盖生成的数据文件发送到 coveralls.io。您必须在 after_success 命令中再次 运行 覆盖范围,以便在工作服 运行s 时容器中存在 .coverage 数据文件。您还必须将工作服回购令牌作为您在 travis https://docs.travis-ci.com/user/environment-variables#defining-variables-in-repository-settings.

中设置的环境变量传递

.travis.yml

language: python
python:
  - "3.7"

services: docker

before_script: pip install docker-compose

script:
  - docker-compose run web sh -c "coverage run manage.py test && flake8 && coverage report"
after_success:
  - docker-compose run web sh -c "coverage run manage.py test && TRAVIS_JOB_ID=$TRAVIS_JOB_ID TRAVIS_BRANCH=$TRAVIS_BRANCH COVERALLS_REPO_TOKEN=$COVERALLS_REPO_TOKEN coveralls"

您需要确保将您的 git 回购文件复制到工作服的容器中,以便准确报告分支并使徽章正常工作。您可能还需要在容器中安装 git。

Dockerfile:10

RUN apk add --update --no-cache postgresql-client jpeg-dev git