Mongodb gitlab ci如何在服务运行后执行Mongo rs.initiate()
我想对我的GoLang项目进行集成测试,并使用mongodb事务。让它工作。我需要使mongodb服务运行副本集 这是我的.gitlab-ci.ymlMongodb gitlab ci如何在服务运行后执行Mongo rs.initiate(),mongodb,gitlab-ci,Mongodb,Gitlab Ci,我想对我的GoLang项目进行集成测试,并使用mongodb事务。让它工作。我需要使mongodb服务运行副本集 这是我的.gitlab-ci.yml image: golang:1.13 services: - name: mongo:4.2 alias: mongo-svc command: ["mongod", "--logpath=/dev/null", "--bind_ip_all", "--replSet=rs0"] unit_tests: stage:
image: golang:1.13
services:
- name: mongo:4.2
alias: mongo-svc
command: ["mongod", "--logpath=/dev/null", "--bind_ip_all", "--replSet=rs0"]
unit_tests:
stage: test
script:
- mongo-svc exec mongo --eval "rs.initiate()"
- make test
我犯了一个错误
$ mongo-svc mongo --eval "rs.initiate()"
/bin/bash: line 95: mongo-svc: command not found
ERROR: Job failed: exit code 1
我已经在使用另一个命令,但仍然找不到如何操作。您的.gitlab-ci.yml创建了一个mongo服务(主机名别名为“mongo svc”),然后您的脚本运行一个命令“mongo svc” 游戏中有两个容器:mongo和golang。您希望在哪个容器中运行
mongo rs.initiate()
(我知道GitLab,但不知道mongo,但我希望这能有所帮助。)
[在阅读了您下面的评论和Kalyn Valentin在
以下.gitlab ci.yml
有效:
image: golang:1.13
services:
- name: mongo:4.2
alias: mongo-svc
command: ["mongod", "--logpath=/dev/null", "--bind_ip_all", "--replSet=rs0"]
unit_tests:
stage: test
script:
- apt-get install -y gnupg
- wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | apt-key add -
- echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main" | tee /etc/apt/sources.list.d/mongodb-org-4.2.list
- apt-get update
- apt-get install -y mongodb-org
- mongo --host mongo-svc --eval "rs.initiate()"
说明:
-我安装Mongo(根据安装说明)--首先,我必须确定golang映像基于Debian,这是我在单元测试
作业的脚本
部分中使用的cat/etc/os版本
。
-然后在golang映像上启动mongo
shell,将其指向mongo svc主机,然后运行rs.initiate()
以下是CI作业日志,显示成功:
Running with gitlab-runner 12.9.0 (4c96e5ad)
on docker-auto-scale 72989761
Preparing the "docker+machine" executor
Using Docker executor with image golang:1.13 ...
Starting service mongo:4.2 ...
Pulling docker image mongo:4.2 ...
Using docker image sha256:c5e5843d9f5fd70b3c39d20c1c06478d067550c2dea667da1e4f1d777576a1f5 for mongo:4.2 ...
Waiting for services to be up and running...
Pulling docker image golang:1.13 ...
Using docker image sha256:a80eb3fa71389ad9ecc1f615c274e8f76690fab74d03f5946c7443350308642c for golang:1.13 ...
Preparing environment
00:03
Running on runner-72989761-project-17974064-concurrent-0 via runner-72989761-srm-1586454360-c1c2ce67...
Getting source from Git repository
00:02
$ eval "$CI_PRE_CLONE_SCRIPT"
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/atsaloli/mongo-test/.git/
Created fresh repository.
From https://gitlab.com/atsaloli/mongo-test
* [new ref] refs/pipelines/134529622 -> refs/pipelines/134529622
* [new branch] master -> origin/master
Checking out abe63386 as master...
Skipping Git submodules setup
Restoring cache
Downloading artifacts
00:01
Running before_script and script
$ apt-get install -y gnupg
Reading package lists...
Building dependency tree...
Reading state information...
gnupg is already the newest version (2.2.12-1+deb10u1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
$ wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | apt-key add -
Warning: apt-key output should not be parsed (stdout is not a terminal)
OK
$ echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main" | tee /etc/apt/sources.list.d/mongodb-org-4.2.list
deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main
$ apt-get update
Get:1 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]
Get:2 http://deb.debian.org/debian buster InRelease [122 kB]
Get:3 http://deb.debian.org/debian buster-updates InRelease [49.3 kB]
Ign:4 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 InRelease
Get:5 http://security.debian.org/debian-security buster/updates/main amd64 Packages [187 kB]
Get:6 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 Release [1488 B]
Get:7 http://deb.debian.org/debian buster/main amd64 Packages [7907 kB]
Get:8 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 Release.gpg [801 B]
Get:9 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2/main amd64 Packages [4083 B]
Get:10 http://deb.debian.org/debian buster-updates/main amd64 Packages [7380 B]
Fetched 8344 kB in 2s (3626 kB/s)
Reading package lists...
$ apt-get install -y mongodb-org
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
The following NEW packages will be installed:
mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell
mongodb-org-tools
0 upgraded, 5 newly installed, 0 to remove and 1 not upgraded.
Need to get 97.7 MB of archives.
After this operation, 296 MB of additional disk space will be used.
Get:1 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2/main amd64 mongodb-org-shell amd64 4.2.5 [12.1 MB]
Get:2 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2/main amd64 mongodb-org-server amd64 4.2.5 [18.5 MB]
Get:3 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2/main amd64 mongodb-org-mongos amd64 4.2.5 [10.2 MB]
Get:4 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2/main amd64 mongodb-org-tools amd64 4.2.5 [57.0 MB]
Get:5 http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2/main amd64 mongodb-org amd64 4.2.5 [3532 B]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 97.7 MB in 2s (40.4 MB/s)
Selecting previously unselected package mongodb-org-shell.
(Reading database ... 15401 files and directories currently installed.)
Preparing to unpack .../mongodb-org-shell_4.2.5_amd64.deb ...
Unpacking mongodb-org-shell (4.2.5) ...
Selecting previously unselected package mongodb-org-server.
Preparing to unpack .../mongodb-org-server_4.2.5_amd64.deb ...
Unpacking mongodb-org-server (4.2.5) ...
Selecting previously unselected package mongodb-org-mongos.
Preparing to unpack .../mongodb-org-mongos_4.2.5_amd64.deb ...
Unpacking mongodb-org-mongos (4.2.5) ...
Selecting previously unselected package mongodb-org-tools.
Preparing to unpack .../mongodb-org-tools_4.2.5_amd64.deb ...
Unpacking mongodb-org-tools (4.2.5) ...
Selecting previously unselected package mongodb-org.
Preparing to unpack .../mongodb-org_4.2.5_amd64.deb ...
Unpacking mongodb-org (4.2.5) ...
Setting up mongodb-org-server (4.2.5) ...
Adding system user `mongodb' (UID 101) ...
Adding new user `mongodb' (UID 101) with group `nogroup' ...
Not creating home directory `/home/mongodb'.
Adding group `mongodb' (GID 102) ...
Done.
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb
Done.
Setting up mongodb-org-shell (4.2.5) ...
Setting up mongodb-org-tools (4.2.5) ...
Setting up mongodb-org-mongos (4.2.5) ...
Setting up mongodb-org (4.2.5) ...
$ mongo --host mongo-svc --eval "rs.initiate()"
MongoDB shell version v4.2.5
connecting to: mongodb://mongo-svc:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("3f798624-0569-423d-a8e1-03e84f9fee7f") }
MongoDB server version: 4.2.5
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "cf8a49d94069:27017",
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1586455071, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1586455071, 1)
}
Running after_script
00:02
Saving cache
Uploading artifacts for successful job
00:02
Job succeeded
我想在mongo容器内运行
mongo rs.initiate()