使用Redis与Gitlab CI

10

我目前正在使用无服务器框架,并使用共享的Runner设置GitLab CI。

以下是我的gitlab-ci.yml文件:

image: node:latest

services:
  - redis

cache:
  paths:
    - node_modules/
    - java/

stages:
  - build
  - test
  - review
  - staging
  - production

build:
  stage: build
  script:
      - npm install
  artifacts:
    paths:
      - node_modules/

install:java:
  stage: build
  script:
      - apt-get update
      - apt-get install -y default-jre default-jdk openjdk-7-jre openjdk-7-jdk
      - apt-get update
      - sls dynamodb install
  artifacts:
    paths:
      - java/

connect:
  image: redis
  script:
  - redis-cli -h redis PING

unit test:
  stage: test
  script:
    - sls dynamodb start
    - babel-node ./aws/createDB.js
    - npm run unit
  dependencies:
    - build
    - install:java

单元测试工作需要 Redis,但无法连接。当单元测试工作启动时,会抛出以下错误:

创建 Redis 客户端时发生错误:错误:Redis 连接到 127.0.0.1:6379 失败 - 连接 ECONNREFUSED 127.0.0.1:6379

有人可以指出当前配置文件有什么问题吗?谢谢!


以下答案是正确的,但如果您已经考虑到了这一点,那么可能是您没有正确设置redis凭据(例如:您正在传递一个配置对象,而主机密钥位置不正确)。如果找不到凭据,则大多数库默认为127.0.0.1:6379,这也解释了您的错误。 - Overdrivr
2个回答

24

Redis服务的主机地址是redis,而不是127.0.0.1localhost

因此,请确保在所有脚本和配置文件中将Redis服务的主机设置为redis


2
访问服务的文档:https://docs.gitlab.com/ce/ci/docker/using_docker_images.html#accessing-the-services - Pierre

4
为了让人们的生活更加方便,我列出了一个示例.gitlab-ci.yml,以在Gitlab CI中配置Redis。
services:
  - redis:latest

stages:
  - test

test:
 script:
   - echo "hello world!"
 stage: test
 variables:
    REDIS_PORT: 6379
    REDIS_HOST: redis
    REDIS_URL: redis://redis:6379

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