1

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

这是我得到的错误

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 install docker-compose script:-docker-compose run 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

我的 Dockerfile

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

4

2 回答 2

2

要了解为什么没有提交覆盖率,您必须了解 docker 容器的运行方式。

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

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

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

您可以参考此处提供的答案来解决此问题: Coveralls: Error- No source for in my application using Docker container

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

于 2019-11-12T09:10:53.160 回答
2

您必须在容器内运行工作服,以便它可以将coverage 生成的数据文件发送到coveralls.io。您必须在命令中再次运行 coverage,after_success以便在运行工作服时容器中存在 .coverage 数据文件。您还必须将工作服 repo 令牌作为您在 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 repo 文件复制到工作服的容器中,以准确报告分支并让徽章工作。您可能还需要在容器中安装 git。

Dockerfile:10

RUN apk add --update --no-cache postgresql-client jpeg-dev git
于 2019-11-15T08:13:08.603 回答