Gitlab CI无法连接到服务

时间:2019-06-06 02:51:59

标签: gitlab gitlab-ci gitlab-ci-runner

我正在尝试对gitlab上的容器运行集成测试。

为使内容尽可能少,这是我的.gitlab-ci.yml

的相关部分
image: ubuntu:latest

coverage:
    stage: test
    dependencies:
        - build
    services:
        - postgres:latest
        - registry.gitlab.com/username/project/image:latest

当我尝试运行该作业时,会收到容器运行状况检查警告。

2019-06-06T02:13:34.508595817Z FATAL: No HOST or PORT found

通常我会以标准docker run -p port:port image:version开始我的映像,但是我不确定这些选项如何转换为gitlab服务。如何定义主机和端口?

2 个答案:

答案 0 :(得分:1)

下面是连接到postgres的示例管道。

除非您显式别名为here

,否则容器将服务别名为容器名称。
services:
- name: postgres:9.4


variables:
  # Configure postgres service (https://hub.docker.com/_/postgres/)
  POSTGRES_DB: $DB_NAME
  POSTGRES_USER: $DB_USER
  POSTGRES_PASSWORD: $DB_PASS

cache:
  paths:
  - node_modules/*

stages:
  - test
  - build


db-test:
  stage: test
  image: ubuntu:latest
  tags:
    - consultancy
    - shared
  script:
    #set server connection env vars
  - export PGPASSWORD=$POSTGRES_PASSWORD
  - apt-get update -y && apt-get install postgresql postgresql-contrib -y
  - psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 'OK' AS status;" #ensure the service is running

node-test:
  stage: test
  image: node:latest
  tags:
  - consultancy
  - shared
  script:
    - npm install # Install Node dependencies
    - npm run test-unit # Execute unit testing suite
#
integration-tests:
  stage: test
  image: node:latest
  tags:
  - consultancy
  - shared
  script:
  - export PGUSER=$POSTGRES_USER
  - export PGHOST=postgres
  - export PGPASSWORD=$POSTGRES_PASSWORD
  - export PGDATABASE=postgres
  - export PGPORT=5432 #set the integration test env vars
  - npm install # Install Node dependencies
  - npm run test-integration # Execute integration testing suite

答案 1 :(得分:1)

在我的情况下,不带EXPOSE的Dockerfile将在cirunner中给出错误的日志味精:

Health check error:
exit code 1

Health check container logs:
2020-05-12T03:42:49.645731131Z No HOST or PORT

由Dockerfile中的公开服务端口修复。