如何在gitlab管道中从Python连接到Redis?

时间:2019-06-25 14:50:03

标签: python redis gitlab

按照以下说明进行操作:https://docs.gitlab.com/ce/ci/services/redis.html 我已添加

services:
  - redis:latest

到我的.gitlab-ci.yml文件,然后更改为redis connect call到:

redis.StrictRedis(host='redis', port=6379, db=0)

我得到的错误是:

/usr/local/lib/python2.7/site-packages/redis/client.py:772: in execute_command
    connection = pool.get_connection(command_name, **options)
/usr/local/lib/python2.7/site-packages/redis/connection.py:994: in get_connection
    connection.connect()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = Connection<host=redis,port=6379,db=0>

    def connect(self):
        "Connects to the Redis server if not already connected"
        if self._sock:
            return
        try:
            sock = self._connect()
        except socket.timeout:
            raise TimeoutError("Timeout connecting to server")
        except socket.error:
            e = sys.exc_info()[1]
>           raise ConnectionError(self._error_message(e))
E           ConnectionError: Error -2 connecting to redis:6379. Name or service not known.

1 个答案:

答案 0 :(得分:0)

事实证明,将服务声明放在每个任务中一样容易,例如:

py27:
  stage: run
  image: python:2.7
  script:
    - mkdir -p build/coverage
    - pip install -r requirements.txt
    - pytest --color=no --verbose --ignore=almanac/migrations --cov=almanac --cov-config=.coveragerc tests
    - cp .coverage build/coverage/py27.coverage
  services:                                     # <===== here ========
    - redis:latest
  artifacts:
    paths:
      - build/coverage/*.coverage