gpt4 book ai didi

ruby-on-rails - 在 Gitlab CI 上将 Redis 用于 Rails 项目

转载 作者:行者123 更新时间:2023-12-03 06:38:50 24 4
gpt4 key购买 nike

我正在尝试为我的 Rails 项目设置 Gitlab CI,为此我需要 Redis。我已将 redis 添加到配置文件中,但出现错误

Redis::CannotConnectError:
Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)
我正在使用 JWTSession用于基于 token 的身份验证,这依赖于 Redis。这是我的设置。
.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
配置/初始化程序/redis.rb
require 'redis'
$redis = Redis.new(url: 'redis://redis:6379/0')
配置/初始化程序/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_'
}
我似乎无法弄清楚如何让它发挥作用。任何提示将不胜感激。谢谢!
更新
我刚刚试过 connect阻止,但收到此错误:
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

最佳答案

这是我的最终 .gitlab-ci.yml 文件

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

加上 sidekiq 配置
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

我认为问题在于我的 sidekiq 配置中未设置 REDIS_URL env var。

关于ruby-on-rails - 在 Gitlab CI 上将 Redis 用于 Rails 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57398793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com