MongoDB server doesn't start at gitlab runner using gitlab-ci

妖精的绣舞 提交于 2020-05-28 07:43:18

问题


right now I'm in the middle at developing an application using Ruby and Mongo. But I got stuck when I deploy the application using gitlab-ci where it seems that mongo server doesn't start at test environment on gitlab runner.

Here are my 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

and this are the result of the gitlab runner error

    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 

and this are my mongoid.yml file :

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

Can anybody tell me what I'm missing. I search everywhere about gitlab-ci and databases but only mysql and postgre sql that showed up about this. Did I miss to run the mongo server on test? or Should I run the server from the code itself. Or it is another problem?


回答1:


Your MongoDB most probably runs on GitlabCI, but it is executed in the separate Docker container. It must be accessed from the tests using mongo:27017, not localhost:27017.

Check here: https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#accessing-the-services

So you should fix your configuration (mongoid.yml) appropriately.




回答2:


The problem is with your connection string for the MongoDB database. Since you have used the mongo service, and no alias is put, so Gitlab takes "mongo" as its hostname. According to Gitlab: The default aliases for the service’s hostname are created from its image name following these rules:

  • Everything after the colon (:) is stripped.
  • Slash (/) is replaced with double underscores (__) and the primary alias is created.
  • Slash (/) is replaced with a single dash (-) and the secondary alias is created (requires GitLab Runner v1.1.0 or higher).

So, instead of localhost, you have to use mongo:27017

Your connection string will be like: mongodb://mongo:27017/${databaseName}



来源:https://stackoverflow.com/questions/49391372/mongodb-server-doesnt-start-at-gitlab-runner-using-gitlab-ci

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!