MongoDB服务器未使用gitlab-ci从gitlab运行程序启动

问题描述 投票:0回答:2

现在,我正在使用Ruby和Mongo开发应用程序。但是,当我使用gitlab-ci部署应用程序时,我陷入了困境,似乎mongo服务器没有在gitlab Runner的测试环境下启动。

这是我的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运行程序错误的结果

    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服务器?还是我应该从代码本身运行服务器。还是另一个问题?

ruby mongodb sinatra gitlab gitlab-ci
2个回答
0
投票

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

在这里检查:https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#accessing-the-services

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


0
投票

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

  • 除去冒号(:)之后的所有内容。
  • 将斜杠(/)替换为双下划线(__),并创建主别名。
  • 用单个破折号(-)替换斜杠(/),并创建辅助别名(需要GitLab Runner v1.1.0或更高版本。

因此,您必须使用mongo:27017]而不是localhost

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

© www.soinside.com 2019 - 2024. All rights reserved.