GitLab CI流水线测试失败:Redis连接拒绝错误排查求助
Hey there, let's get that Redis connection issue sorted out! The problem here is how GitLab CI handles service containers—they don't run on the same localhost as your main job container, so pointing to 127.0.0.1 won't work.
What's Going Wrong?
In your .gitlab-ci.yml, you've added the redis:latest service, but your REDISTOGO_URL variable is still using localhost as the host. In GitLab CI, each service runs as a separate container, and you reference them by their service name (in this case, just redis) instead of localhost.
The Fix
Update your REDISTOGO_URL variable to use the service name as the host:
variables: MONGODB_URI: mongodb://mongo:27017/db_test REDISTOGO_URL: redis://redis:6379 # Changed localhost to redis
Extra Checks to Ensure It Works
- Verify the service host is reachable: Add a quick test in your
before_scriptto confirm Redis is up before running tests:- apt-get update && apt-get install -y redis-tools - redis-cli -h redis ping # Should return "PONG" if connected - Check for hardcoded localhost in your app: Make sure your test environment config (like
config/redis.ymlor similar) isn't hardcoding127.0.0.1—it should use theREDISTOGO_URLenvironment variable instead.
That should resolve the Errno::ECONNREFUSED error and let your tests connect to Redis successfully.
内容的提问来源于stack exchange,提问作者Tony Vincent

