MongoDB服务器没有';t使用gitlab ci在gitlab runner上启动 现在我正处于使用Ruby和Mongo开发应用程序的中间。但我在使用gitlab ci部署应用程序时遇到了问题,因为mongo服务器似乎没有在gitlab runner的测试环境中启动

MongoDB服务器没有';t使用gitlab ci在gitlab runner上启动 现在我正处于使用Ruby和Mongo开发应用程序的中间。但我在使用gitlab ci部署应用程序时遇到了问题,因为mongo服务器似乎没有在gitlab runner的测试环境中启动,ruby,mongodb,sinatra,gitlab,gitlab-ci,Ruby,Mongodb,Sinatra,Gitlab,Gitlab Ci,这是我的gitlab-ci.yml stages : - test - deploy services: - mongo:latest unitTest:API: image: ruby:2.6.10 stage: test cache: paths: - API/vendor/ before_script: - ruby -v - gem install bundler --no-ri --no-rdoc - bundle

这是我的gitlab-ci.yml

stages :
- test
- deploy

services:
  - mongo:latest

unitTest:API:
  image: ruby:2.6.10
  stage: test
  cache: 
    paths:
      - API/vendor/
  before_script:
    - ruby -v
    - gem install bundler --no-ri --no-rdoc
    - bundle install --gemfile=API/Gemfile --path vendor
  script:
    - RAILS_ENV=test rspec API/spec
    - RAILS_ENV=test rubocop API
  artifacts:
    paths:
      - coverage/

deploy_coba:
  stage: deploy
  before_script:
    - cd API
  script: 
    - gem install dpl
    - dpl --provider=heroku --app=exhibisi-cobacoba --api-key=68b12c2b-cfbc-4478-b943-f26552fb907e
  only:
    - coba_coba

staging:
  stage: deploy
  before_script:
    - cd API
  script: 
    - gem install dpl
    - dpl --provider=heroku --app=exhibisi-staging --api-key=68b12c2b-cfbc-4478-b943-f26552fb907e
  only:
    - sit_uat

production:
  stage: deploy
  before_script:
    - cd API
  script:
    - gem install dpl
    - dpl --provider=heroku --app=exhibisi-prod --api-key=68b12c2b-cfbc-4478-b943-f26552fb907e
  only:
    - master

pages:
  stage: deploy
  script:
    - mv coverage/ public/
  artifacts:
    paths:
      - public
    expire_in: 30 days
这是gitlab runner错误的结果

    Running with gitlab-runner 10.6.0-rc1 (0a9d5de9)
on docker-auto-scale e11ae361
Using Docker executor with image ruby:2.3.0 ...
Starting service mongo:2.6.10 ...
Pulling docker image mongo:2.6.10 ...
Using docker image sha256:54fb6f9984dde283c9ff55f5aba7d883355793dd7997b0f9f75bb31e89607311 for mongo:2.6.10 ...
Waiting for services to be up and running...
Pulling docker image ruby:2.3.0 ...
Using docker image sha256:7ca70eb2dfea00e9e3eaece33024ad5c06b7473728d559e1a3a574629de95a6a for ruby:2.3.0 ...
Running on runner-e11ae361-project-5550018-concurrent-0 via runner-e11ae361-srm-1521565965-49af3740...
Cloning repository...
Cloning into '/builds/cymon1997/ppl-coba'...
Checking out 61333b01 as US02_menampilkan_daftar_produk...
Skipping Git submodules setup
Checking cache for default...
FATAL: file does not exist                         
Failed to extract cache
$ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
$ gem install bundler --no-ri --no-rdoc
Successfully installed bundler-1.16.1
1 gem installed
$ bundle install --gemfile=API/Gemfile --path vendor
Fetching gem metadata from http://rubygems.org/..........
Fetching rake 12.3.0
Installing rake 12.3.0
Fetching concurrent-ruby 1.0.5
Installing concurrent-ruby 1.0.5
Fetching i18n 0.9.5
Installing i18n 0.9.5
Fetching minitest 5.11.3
Installing minitest 5.11.3
Fetching thread_safe 0.3.6
Installing thread_safe 0.3.6
Fetching tzinfo 1.2.5
Installing tzinfo 1.2.5
Fetching activesupport 5.1.5
Installing activesupport 5.1.5
Fetching activemodel 5.1.5
Installing activemodel 5.1.5
Fetching ast 2.4.0
Installing ast 2.4.0
Fetching bson 4.3.0
Installing bson 4.3.0 with native extensions
Using bundler 1.16.1
Fetching diff-lcs 1.3
Installing diff-lcs 1.3
Fetching docile 1.1.5
Installing docile 1.1.5
Fetching json 2.1.0
Installing json 2.1.0 with native extensions
Fetching mongo 2.5.1
Installing mongo 2.5.1
Fetching mongoid 6.3.0
Installing mongoid 6.3.0
Fetching mustermann 1.0.2
Installing mustermann 1.0.2
Fetching parallel 1.12.1
Installing parallel 1.12.1
Fetching parser 2.5.0.3
Installing parser 2.5.0.3
Fetching powerpack 0.1.1
Installing powerpack 0.1.1
Fetching rack 2.0.4
Installing rack 2.0.4
Fetching rack-protection 2.0.1
Installing rack-protection 2.0.1
Fetching rack-test 0.8.2
Installing rack-test 0.8.2
Fetching racksh 1.0.0
Installing racksh 1.0.0
Fetching rainbow 3.0.0
Installing rainbow 3.0.0
Fetching rspec-support 3.7.1
Installing rspec-support 3.7.1
Fetching rspec-core 3.7.1
Installing rspec-core 3.7.1
Fetching rspec-expectations 3.7.0
Installing rspec-expectations 3.7.0
Fetching rspec-mocks 3.7.0
Installing rspec-mocks 3.7.0
Fetching rspec 3.7.0
Installing rspec 3.7.0
Fetching rspec-json_expectations 2.1.0
Installing rspec-json_expectations 2.1.0
Fetching ruby-progressbar 1.9.0
Installing ruby-progressbar 1.9.0
Fetching unicode-display_width 1.3.0
Installing unicode-display_width 1.3.0
Fetching rubocop 0.53.0
Installing rubocop 0.53.0
Fetching shotgun 0.9.2
Installing shotgun 0.9.2
Fetching simplecov-html 0.10.2
Installing simplecov-html 0.10.2
Fetching simplecov 0.15.1
Installing simplecov 0.15.1
Fetching tilt 2.0.8
Installing tilt 2.0.8
Fetching sinatra 2.0.1
Installing sinatra 2.0.1
Bundle complete! 10 Gemfile dependencies, 39 gems now installed.
Bundled gems are installed into `./vendor`
$ RAILS_ENV=test rspec API/spec
...FFFF.

Failures:

1) ListProductController GET to /products returns status 200 OK
    Failure/Error: expect(last_response).to be_ok
    expected `#<Rack::MockResponse:0x0056395b78af10 @original_headers={"Content-Type"=>"text/html", "Content-Length...bled the <code>show_exceptions</code> setting.</p>\n  </div> <!-- /WRAP -->\n  </body>\n</html>\n"]>.ok?` to return true, got false
    # ./API/spec/controllers/listproduct_controller_spec.rb:9:in `block (3 levels) in <top (required)>'

2) ListProductController GET to /products show a list of product's name and its icon
    Failure/Error:
    Product.each do |product|
        payload.push({ exhibit_name: product.exhibit_name, icon: product.icon })
    end

    Mongo::Error::NoServerAvailable:
    No server is available matching preference: #<Mongo::ServerSelector::Primary:0x47402175180100 tag_sets=[] max_staleness=nil> using server_selection_timeout=30 and local_threshold=0.015
    # ./API/vendor/ruby/2.3.0/gems/mongo-2.5.1/lib/mongo/server_selector/selectable.rb:119:in `select_server'
    # ./API/vendor/ruby/2.3.0/gems/mongo-2.5.1/lib/mongo/collection/view/iterable.rb:41:in `block in each'
    # ./API/vendor/ruby/2.3.0/gems/mongo-2.5.1/lib/mongo/retryable.rb:44:in `read_with_retry'
    # ./API/vendor/ruby/2.3.0/gems/mongo-2.5.1/lib/mongo/collection/view/iterable.rb:40:in `each'
    # ./API/vendor/ruby/2.3.0/gems/mongoid-6.3.0/lib/mongoid/query_cache.rb:222:in `each'
    # ./API/vendor/ruby/2.3.0/gems/mongoid-6.3.0/lib/mongoid/contextual/mongo.rb:132:in `each'
    # ./API/vendor/ruby/2.3.0/gems/mongoid-6.3.0/lib/mongoid/contextual.rb:20:in `each'
    # ./API/vendor/ruby/2.3.0/gems/mongoid-6.3.0/lib/mongoid/findable.rb:15:in `each'
    # ./API/spec/controllers/listproduct_controller_spec.rb:15:in `block (3 levels) in <top (required)>'

3) ListProductController GET to /:id returns status 200 OK
    Failure/Error: expect(last_response).to be_ok
    expected `#<Rack::MockResponse:0x0056395c8565b0 @original_headers={"Content-Type"=>"text/html", "Content-Length...bled the <code>show_exceptions</code> setting.</p>\n  </div> <!-- /WRAP -->\n  </body>\n</html>\n"]>.ok?` to return true, got false
    # ./API/spec/controllers/listproduct_controller_spec.rb:31:in `block (3 levels) in <top (required)>'

4) ListProductController GET to /:id displays the product's profil
    Failure/Error: product = Product.find_by(exhibit_id: "EXH1")

    Mongo::Error::NoServerAvailable:
    No server is available matching preference: #<Mongo::ServerSelector::Primary:0x69881936872840 tag_sets=[] max_staleness=nil> using server_selection_timeout=30 and local_threshold=0.015
    # ./API/vendor/ruby/2.3.0/gems/mongo-2.5.1/lib/mongo/server_selector/selectable.rb:119:in `select_server'
    # ./API/vendor/ruby/2.3.0/gems/mongo-2.5.1/lib/mongo/collection/view/iterable.rb:41:in `block in each'
    # ./API/vendor/ruby/2.3.0/gems/mongo-2.5.1/lib/mongo/retryable.rb:44:in `read_with_retry'
    # ./API/vendor/ruby/2.3.0/gems/mongo-2.5.1/lib/mongo/collection/view/iterable.rb:40:in `each'
    # ./API/vendor/ruby/2.3.0/gems/mongoid-6.3.0/lib/mongoid/query_cache.rb:222:in `each'
    # ./API/vendor/ruby/2.3.0/gems/mongoid-6.3.0/lib/mongoid/contextual/mongo.rb:278:in `first'
    # ./API/vendor/ruby/2.3.0/gems/mongoid-6.3.0/lib/mongoid/contextual/mongo.rb:278:in `find_first'
    # ./API/vendor/ruby/2.3.0/gems/mongoid-6.3.0/lib/mongoid/contextual.rb:20:in `find_first'
    # ./API/vendor/ruby/2.3.0/gems/mongoid-6.3.0/lib/mongoid/findable.rb:114:in `find_by'
    # ./API/spec/controllers/listproduct_controller_spec.rb:35:in `block (3 levels) in <top (required)>'

Finished in 7 minutes 2 seconds (files took 1.31 seconds to load)
8 examples, 4 failures

Failed examples:

rspec ./API/spec/controllers/listproduct_controller_spec.rb:7 # ListProductController GET to /products returns status 200 OK
rspec ./API/spec/controllers/listproduct_controller_spec.rb:12 # ListProductController GET to /products show a list of product's name and its icon
rspec ./API/spec/controllers/listproduct_controller_spec.rb:29 # ListProductController GET to /:id returns status 200 OK
rspec ./API/spec/controllers/listproduct_controller_spec.rb:33 # ListProductController GET to /:id displays the product's profil

Coverage report generated for RSpec to /builds/cymon1997/ppl-coba/coverage. 44 / 56 LOC (78.57%) covered.
ERROR: Job failed: exit code 
这是我的mongoid.yml文件:

development:
  options:
    raise_not_found_error: false
  clients:
    default:
      database: mongoid_dev
      hosts:
        - localhost:27017
test:
  options:
    raise_not_found_error: false
  clients:
    default:
      database: mongoid_dev
      hosts:
        - localhost:27017

谁能告诉我我错过了什么。我到处搜索gitlab ci和数据库,但只有mysql和postgre sql显示了这一点。我是否错过了在测试中运行mongo服务器?或者我应该从代码本身运行服务器。或者这是另一个问题?

您的MongoDB很可能在GitlabCI上运行,但它是在单独的Docker容器中执行的。必须使用
mongo:27017
而不是
localhost:27017
从测试中访问它

请点击此处:


因此,您应该适当地修复配置(mongoid.yml)。

问题在于MongoDB数据库的连接字符串。 因为您使用了mongo服务,并且没有放置别名,所以Gitlab将“mongo”作为其主机名。 据Gitlab称: 服务主机名的默认别名是按照以下规则从其映像名创建的:

  • 冒号(:)之后的所有内容都被剥离
  • 斜杠(/)替换为双下划线(_u),并创建主别名
  • 斜杠(/)替换为单破折号(-),并创建辅助别名(需要GitLab Runner v1.1.0或更高版本)
因此,您必须使用mongo:27017来代替localhost

您的连接字符串如下所示:mongodb://mongo:27017/${databaseName}