如何使用 CircleCI 在后台运行服务器?

use*_*949 3 python django continuous-integration circleci

我在我的 Django 项目中使用 CircleCI。我想python manage.py runserver在后台运行服务器(特别是)以进行某些特定的硒测试。

config.yml的有点像

version: 2
jobs:
  build:
    docker:
      - image: circleci/python:3.6.1-browsers
      - image: selenium/standalone-chrome

    working_directory: ~/myproject

    steps:
      - checkout
      - run:
          name: install dependencies
          command: |
            python3 -m venv venv
            . venv/bin/activate
            pip install -r requirements.txt

      - run:
          name: run unit tests
          command: |
            . venv/bin/activate
            python manage.py test

      - run:
          name: run selenium tests
          command: |
            . venv/bin/activate
            python manage.py migrate
            python manage.py runserver 8000 
            python manage.py run_selenium_tests         
Run Code Online (Sandbox Code Playgroud)

我可以通过在 django 中运行 selenium 测试来使它工作LiveServerTestCase。但是我想独立运行 selenium 测试,为此我需要 runserver 在后台运行。现在 circleci 停止执行python manage.py runserver并最终超时。有什么想法可以这样做吗?

Fel*_*ech 5

您需要将服务器作为后台命令启动。或者,您还可以使用 cURL 等待服务器准备就绪。

根据您发布的配置,您可以执行以下操作:

version: 2
jobs:
  build:
    docker:
      - image: circleci/python:3.6.1-browsers
      - image: selenium/standalone-chrome

    working_directory: ~/myproject

    steps:
      - checkout
      - run:
          name: install dependencies
          command: |
            python3 -m venv venv
            . venv/bin/activate
            pip install -r requirements.txt

      - run:
          name: run unit tests
          command: |
            . venv/bin/activate
            python manage.py test

      - run:
          name: run selenium tests prep
          command: |
            . venv/bin/activate
            python manage.py migrate
      - run:
          name: run server
          command: python manage.py runserver 8000
          background: true
      - run:
          name: run selenium tests
          command: |
            curl --retry-delay 5 --retry 10  --retry-connrefused http://localhost:8000
            python manage.py run_selenium_tests
Run Code Online (Sandbox Code Playgroud)

curl 语句在继续之前等待端口响应。这为您的服务器提供了完全启动的时间。

- Ricardo N Feliciano 开
发布道师,CircleCI