如何连接到 GitLab CI/CD 内的 docker compose 启动的服务?

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

我有这个

GitLab CI/CD

stages:
  - test

variables:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
    DOCKER_HOST: tcp://docker:2375

integration_test:
  image: cypress/base
  stage: test
  services:
    - docker:dind
  before_script:
    - apt-get update && apt-get install -y curl iproute2
    - export RUNNER_IP=$(ip addr show eth0 | grep -Po 'inet \K[\d.]+') && echo $RUNNER_IP
    - curl -fsSL https://get.docker.com | sh
    - docker --version
    - docker login -u TOKEN -p TOKEN registry.gitlab.com
    - docker pull registry.gitlab.com/a.bovi-work/tests-template/service-a:latest
    - docker pull registry.gitlab.com/a.bovi-work/tests-template/service-b:latest
  script:
    - docker compose up -d
    - sleep 10 # Wait for the services to start
    - docker ps
    - curl http://${RUNNER_IP}:8082/hello
    - npm install
    - npx cypress run --spec cypress/integration/serviceb_that_calls_servicea.js --config baseUrl=http://${RUNNER_IP}:8082
    - docker compose down

还有这个

docker-compose.yaml

services:

  service-a:
    image: registry.gitlab.com/a.bovi-work/tests-template/service-a:latest
    ports:
      - 8080:8080

  service-b:
    image: registry.gitlab.com/a.bovi-work/tests-template/service-b:latest
    environment:
      - quarkus.rest-client.service-a.url=http://service-a:8080
    ports:
      - 8082:8082

这个

cypress.config.js

// cypress.config.js
const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    specPattern: "cypress/integration/**/*.js",
    baseUrl: "http://localhost:8082",
    supportFile: false
  }
})

这个测试:

describe('Service B Hello Endpoint', () => {
    it('should fetch data and verify the response', () => {
        cy.request({
            method: 'GET',
            url: '/hello'
        })
            .then((response) => {
                // Assertions
                expect(response.status).to.eq(200);
                expect(response.headers['content-type']).to.include('application/json');
                expect(response.body).to.eq('Hello from Service B, that called Service A too: Hello from Service A');
            });
    });
});

但是发生了这种情况:

$ docker compose up -d
 Network e2e_default  Creating
 Network e2e_default  Created
 Container e2e-service-a-1  Creating
 Container e2e-service-b-1  Creating
 Container e2e-service-b-1  Created
 Container e2e-service-a-1  Created
 Container e2e-service-a-1  Starting
 Container e2e-service-b-1  Starting
 Container e2e-service-b-1  Started
 Container e2e-service-a-1  Started
$ sleep 10
$ docker ps
CONTAINER ID   IMAGE                                                             COMMAND                  CREATED          STATUS          PORTS                                       NAMES
19b9c017e9b3   registry.gitlab.com/a.bovi-work/tests-template/service-a:latest   "java -jar /app/quar…"   15 seconds ago   Up 10 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   e2e-service-a-1
9ee49bdd07b6   registry.gitlab.com/a.bovi-work/tests-template/service-b:latest   "java -jar /app/quar…"   15 seconds ago   Up 10 seconds   0.0.0.0:8082->8082/tcp, :::8082->8082/tcp   e2e-service-b-1
$ curl http://${RUNNER_IP}:8082/hello
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (7) Failed to connect to 172.17.0.4 port 8082: Connection refused

P.S.:我正在尝试使用 CURL 进行测试,因为 cypress 无法连接到

baseUrl=http://${RUNNER_IP}:8082

如何连接到从

docker compose
内部
GitLab CI/CD
启动的服务???

docker docker-compose gitlab cypress gitlab-ci
1个回答
0
投票

我相信您在这里使用了不正确的方法。您不应该使用

dind
创建两个额外的容器。相反,gitlab 已经在管道中提供了一个名为 Services 的东西。

如果您的

gitlab-ci.yml
属于您在
gitlab.com/a.bovi-work
中某处的组,那么您可以设置 gitlab 本身(查看存储库设置)以允许无需登录即可提取图像。

接下来,您可以为您的服务设置环境变量(请参见下面示例的第 9 行)

最后,运行 cypress 时只需使用

http://service-b:8082
而不是
http://${RUNNER_IP}:8082
。可能是
service-a
,也许我只是没能掌握两个容器之间的联系。

无论如何,这尚未经过测试,但应该可行。只需简单地尝试一下,看看什么最适合您。

这是我编写的完整示例:

integration_test:
  image: cypress/base
  stage: test
  services:
    - name: registry.gitlab.com/a.bovi-work/tests-template/service-a:latest
      alias: service-a
    - name: registry.gitlab.com/a.bovi-work/tests-template/service-b:latest
      alias: service-b
      variables:
        quarkus.rest-client.service-a.url: "http://service-a:8080"
  script:
    - npm install
    - npx cypress run --spec cypress/integration/serviceb_that_calls_servicea.js --config baseUrl=http://service-b:8082

参考资料:

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