Ruby on rails 使用Postgres和Elasticsearch(searchkick gem)的GitLab CI for Rails应用程序
如何为Rails应用程序配置Ruby on rails 使用Postgres和Elasticsearch(searchkick gem)的GitLab CI for Rails应用程序,ruby-on-rails,postgresql,elasticsearch,gitlab-ci,searchkick,Ruby On Rails,Postgresql,elasticsearch,Gitlab Ci,Searchkick,如何为Rails应用程序配置.gitlab ci.yml文件,该应用程序依赖于PosgreSQL和Elasticsearch,通过searchkick gem在我将其推送到gitlab时运行我的测试 我想发布这个问题,因为我花了太长时间才找到答案,不想让别人感受到我的痛苦。下面的示例不仅构建我的应用程序,还运行我的所有规范 安装程序 轨道5+ PostResql9.6 Rspec宝石 Searchkick gem(处理Elasticsearch查询和配置) 配置 使用列出的配置将以下文件添加
.gitlab ci.yml
文件,该应用程序依赖于PosgreSQL和Elasticsearch,通过searchkick gem在我将其推送到gitlab时运行我的测试 我想发布这个问题,因为我花了太长时间才找到答案,不想让别人感受到我的痛苦。下面的示例不仅构建我的应用程序,还运行我的所有规范
安装程序
- 轨道5+
- PostResql9.6
- Rspec宝石
- Searchkick gem(处理Elasticsearch查询和配置)
test:
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
host: postgres
database: test_db
user: runner
password: ""
image: ruby:2.4.1
services:
- postgres:latest
- elasticsearch:latest
variables:
POSTGRES_DB: test_db
POSTGRES_USER: runner
POSTGRES_PASSWORD: ""
ELASTICSEARCH_URL: "http://elasticsearch:9200"
stages:
- test
before_script:
- bundle install --without postgres production --jobs $(nproc) "${FLAGS[@]}"
- cp config/gitlab-ci/gitlab-database.yml config/database.yml
- RAILS_ENV=test bundle exec rails db:create db:schema:load
test:
stage: test
script:
- bundle exec rspec
.gitlab ci.yml
test:
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
host: postgres
database: test_db
user: runner
password: ""
image: ruby:2.4.1
services:
- postgres:latest
- elasticsearch:latest
variables:
POSTGRES_DB: test_db
POSTGRES_USER: runner
POSTGRES_PASSWORD: ""
ELASTICSEARCH_URL: "http://elasticsearch:9200"
stages:
- test
before_script:
- bundle install --without postgres production --jobs $(nproc) "${FLAGS[@]}"
- cp config/gitlab-ci/gitlab-database.yml config/database.yml
- RAILS_ENV=test bundle exec rails db:create db:schema:load
test:
stage: test
script:
- bundle exec rspec
就这样!您现在已配置为在gitlab上为每次推送自动运行规范
进一步解释
让我们从PostgreSQL开始。当我们启动新的runner时,我们正在复制的应用程序将不知道如何正确连接到Postgres。因此,我们创建了一个新的database.yml
文件,它的前缀是gitlab-
,这样它就不会与我们的实际配置冲突,并将该文件复制到runner的config目录中。cp
命令不仅复制文件,而且将替换当前存在的文件
我们通过GitLab连接的项目有数据库:
,用户:
,以及密码:
。我们通过在我们的环境变量中指定这些相同的名称来实现这一点,确保所有内容都正确连接
好的,连接到PostgreSQL在GitLab的网站上有很好的解释和文档记录。那么,我是如何让Elasticsearch工作的,这在任何地方都没有得到很好的解释
在变量
中,神奇再次发生。当ELASTICSEARCH查找http://localhost:9200默认情况下,
。但是,由于我们通过服务使用Elasticsearch,我们需要明确告诉它不要使用默认值,而是使用我们服务的主机名。因此,我们随后替换了http://localhost:9200
与http://elasticsearch:9200
,它确实正确地映射到我们的服务