I am trying to set up Gitlab CI for my Rails project and for this I need Redis. I have added redis to the config file but I am getting an error
Redis::CannotConnectError:
Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)
I am using JWTSession
for token-based authentication and this relies on Redis. This is my setup.
image: ruby:2.5.1
services:
- postgres:10.1
- redis:latest
variables:
BUNDLE_PATH: vendor/bundle
DISABLE_SPRING: 1
DB_HOST: postgres
REDIS_URL: redis
before_script:
- apt-get update -qq && apt-get install nodejs -y
- bundle check || bundle install --jobs $(nproc)
- cp config/database.yml.ci config/database.yml
- bundle exec rails db:create RAILS_ENV=test
- bundle exec rails db:schema:load RAILS_ENV=test
stages:
- test
Tests:
stage: test
script:
- bundle exec rspec spec/
only:
- merge_requests
require 'redis'
$redis = Redis.new(url: 'redis://redis:6379/0')
JWTSessions.encryption_key = Rails.application.credentials.secret_jwt_encryption_key
JWTSessions.token_store = :redis, {
redis_url: 'redis://localhost:6379',
token_prefix: 'jwt_'
}
I can't seem to figure out how to get this to work. Any hint will be appreciated. Thank you!
I've just tried the connect
block, but got this error:
connect:
# Connect to PostgreSQL database as user postgres, without password
image: redis
script:
- redis-cli -h redis PING
Redis::CannotConnectError:
Error connecting to Redis on localhost:6379 (Errno::EADDRNOTAVAIL)
--- Caused by: ---
# Errno::EADDRNOTAVAIL:
# Cannot assign requested address - connect(2) for [::1]:6379
This is my final .gitlab-ci.yml file
image: "ruby:2.4.0"
services:
- postgres:latest
- redis:latest
- name: docker.elastic.co/elasticsearch/elasticsearch:6.5.4
alias: elasticsearch
variables:
POSTGRES_DB: lauber_backend_test
POSTGRES_USER: runner
POSTGRES_PASSWORD: ""
REDIS_HOST: redis
REDIS_URL: redis://redis:6379
ELASTICSEARCH_URL: "http://elasticsearch:9200"
before_script:
- apt-get update -qq && apt-get install -y -qq nodejs libpq-dev cmake libicu-dev
- echo "$RAILS_MASTER_KEY" -> config/master.key
- RAILS_ENV=test bundle install --jobs $(nproc) "${FLAGS[@]}"
- cp config/database.yml.example config/database.yml
- RAILS_ENV=test rails db:setup
rspec:
script:
- bundle exec rspec
Plus sidekiq config
redis = { db: 4, url: ENV['REDIS_URL'] }
Sidekiq.configure_client do |config|
config.redis = redis
end
Sidekiq.configure_server do |config|
config.redis = redis
end
I think the problem was with REDIS_URL env var not set in my sidekiq config.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With