如何在CircleCI中以后台方式运行服务器?

4

我正在使用CircleCI处理我的Django项目。我想要在后台运行一个服务器(特别是python manage.py runserver)来进行一些特定的Selenium测试。

我的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         

我可以通过在django的LiveServerTestCase中运行selenium测试来使其工作。但是我想独立运行selenium测试,因此需要在后台运行runserver。目前,circleci在python manage.py runserver处停止执行,并最终超时。有什么建议吗?

3个回答

7
你需要将服务器作为后台命令启动。可选地,你也可以使用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

curl语句在继续执行之前等待端口响应。这给您的服务器充分启动的时间。

- Ricardo N Feliciano
Developer Evangelist,CircleCI


1
当我尝试这个时,我遇到了这个错误 curl: option --retry-connrefused: is unknown。但是当我移除了 curl 后就可以工作了。谢谢! - user3148949
我必须在“curl”之前使用“sleep 5”,否则它会出现“curl:(7)Failed to connect to localhost port 8000: Connection refused”的错误。 - udo
嗨Richard,你能否建议另一种尝试服务器直到连接成功的方法?我正在使用CircleCI,curl也失败了。 - TD1
1
尝试使用Dockerize:https://discuss.circleci.com/t/prevent-race-conditions-by-waiting-for-services-with-dockerize/11215?u=felicianotech - FelicianoTech
如果在 docker 容器中,--retry-connrefused 看起来被忽略了,可以尝试添加 "-4" 选项,使 curl 使用 IPv4。请参阅此处:https://github.com/curl/curl/issues/5080 - sveri

1
我不使用CircleCI,但问题在于manage.py runserver是阻塞的,即它不能在后台运行。根据CircleCI文档,您可以将进程设为守护进程:

https://circleci.com/docs/1.0/background-process/

关于之后是否能够连接到端口(即CI是否允许绑定端口),我不确定。

0
成功使用gunicorn运行。这是config.yml文件。
  # run tests!
  - run:
      name: run unittests
      command: |
        . venv/bin/activate
        python manage.py test
        python manage.py migrate

  - run:
      name: run server in background
      command: |
        . venv/bin/activate
        gunicorn myproject.wsgi:application --bind=127.0.0.1:8000 --pid=gunicorn.txt --daemon
      background: true

  - run:
      name: run selenium test
      command: |
        . venv/bin/activate
        python manage.py run_selenium_tests
        kill -9 `cat gunicorn.txt`

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接