CircleCI Swift存在Postgres连接问题

时间:2019-04-09 19:52:59

标签: swift postgresql docker circleci circleci-2.0

我正在与repo合作,使用circleCI和postgres构建一个用于Swift的测试应用程序,但是在测试方面,我似乎无法掌握在测试阶段如何连接两个图像。

我在跑步

circleci local execute --job build

应该同时构建swift和postgres图像。我给他们两个我在应用程序中给出的相同的env变量。但是,尝试运行它时出现此错误。以我的经验,当我尝试设置两个docker容器进行撰写时,当我的api无法通过网络连接到db容器时,就会出现此错误。

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不在那里。有任何想法吗?

我当前的circleci config.yml

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

1 个答案:

答案 0 :(得分:1)

您要将数据库的主机名设置为db,但未在任何地方定义该主机名。您需要命名Docker容器以匹配https://github.com/vapor/postgresql/blob/master/circle.yml#L8

这样的DB_HOSTNAME环境变量