CircleCI Swift 与 Postgres 连接问题

CircleCI Swift with Postgres connection issues

我正在与我的 repo 一起使用 circleCI 和 postgres 为 swift 构建一个测试应用程序,但是当涉及到测试时,我似乎无法掌握如何连接两个图像测试阶段。

我是运行宁

circleci local execute --job build

应该同时构建 swift 和 postgres 图像。我给了他们我在应用程序中提供的相同环境变量。但是,我在尝试 运行 时遇到此错误。根据我的经验,当我的 api 无法通过网络连接到数据库容器时,尝试设置两个 docker 容器时出现此错误。

Test Case 'AppTests.RemoveUserTest' started at 2019-04-09 19:46:15.380
Fatal error: 'try!' expression unexpectedly raised an error: NIO.ChannelError.connectFailed(NIO.NIOConnectionError(host: "db", port: 5432, dnsAError: Optional(NIO.SocketAddressError.unknown(host: "db", port: 5432)), dnsAAAAError: Optional(NIO.SocketAddressError.unknown(host: "db", port: 5432)), connectionErrors: [])): file /home/buildnode/jenkins/workspace/oss-swift-4.2-package-linux-ubuntu-16_04/swift/stdlib/public/core/ErrorType.swift, line 184

我知道它说它因为 try 语句而失败,但该 try 语句失败是因为它请求来自 Postgres 的操作,而 Postgres 不存在。有任何想法吗?

我目前 config.yml 的 circleci

version: 2

jobs:
  build:
    docker:
      - image: swift:4.2
        environment:
          POSTGRES_USER: test
          POSTGRES_PASSWORD: test
          POSTGRES_DB: test
          DB_HOSTNAME: db
          PORT: 5432
      - image: postgres:11.2-alpine
        environment:
          POSTGRES_USER: test
          POSTGRES_PASSWORD: test
          POSTGRES_DB: test
    steps:
      - checkout
      - run: apt-get update -qq
      - run: apt-get install -yq libssl-dev pkg-config wget
      - run: apt-get install -y postgresql-client || true
      - run:
          name: install dockerize
          command: wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
          environment:
            DOCKERIZE_VERSION: v0.3.0
      - run:
          name: Wait for db
          command: dockerize -wait tcp://localhost:5432 -timeout 1m
      - run: 
          name: Compile code
          command: swift build
      - run: 
          name: Run unit tests
          command: swift test

  release:
    docker:
      - image: swift:4.2
    steps:
      - checkout
      - run: 
          name: Compile code with optimizations
          command: swift build -c release
  push-to-docker-hub:
    docker:
      - image: docker:latest
    steps:
      - checkout
      - setup_remote_docker
      - run:
          name: Install dependencies
          command: |
            apk add --update --no-cache curl jq python py-pip
      - run:
          name: Build Docker Image
          command: |
            docker build -t api .
            docker tag api <>/repo:latest
            docker tag api <>/repo:$CIRCLE_SHA1
            docker login -u $DOCKER_USER -p $DOCKER_PASS
            docker push <>/repo:latest
            docker push <>/repo:$CIRCLE_SHA1
#      - persist_to_workspace:
#          root: ./
#          paths:
#            - k8s-*.yml

workflows:
  version: 2
  tests:
    jobs:
      - build
      - push-to-docker-hub:
          requires:
            - build
          context: dockerhub
          filters:
            branches:
              only: master
      #- linux-release

您将数据库的主机名设置为 db,但没有在任何地方定义它。您需要命名您的 Docker 容器以匹配 DB_HOSTNAME 环境变量,例如 https://github.com/vapor/postgresql/blob/master/circle.yml#L8