Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Redis on Gitlab CI for a Rails project

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.

.gitlab-ci.yml

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

config/initializers/redis.rb

require 'redis'
$redis = Redis.new(url: 'redis://redis:6379/0')

config/initializers/jwt_sessions.rb

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!

UPDATE

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
like image 927
jedi Avatar asked Oct 21 '25 11:10

jedi


1 Answers

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.

like image 175
jedi Avatar answered Oct 23 '25 00:10

jedi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!