由于无法看到Postgres,因此gitlab-ci中的测试失败

时间:2017-10-26 13:12:12

标签: django postgresql docker gitlab-ci cookiecutter-django

在docker中使用gitlab-ci进行的测试失败,因为Postgres服务无法访问。

在我的开发环境中,我使用:$docker-compose -f local.yaml run web py.test

成功运行测试

但是在gitlab中,gitlab-ci.yaml文件中的命令- docker run --env-file=.env_dev $CONTAINER_TEST_IMAGE py.test -p no:sugar失败了:

9bfe10de3baf: Pull complete
a137c036644b: Pull complete
8ad45b31cc3c: Pull complete
Digest: sha256:0897b57e12bd2bd63bdf3d9473fb73a150dc4f20cc3440822136ca511417762b
Status: Downloaded newer image for registry.gitlab.com/myaccount/myapp:gitlab_ci
$ docker run --env-file=.env $CONTAINER_TEST_IMAGE py.test -p no:sugar
Postgres is unavailable - sleeping
Postgres is unavailable - sleeping
Postgres is unavailable - sleeping
Postgres is unavailable - sleeping
Postgres is unavailable - sleeping

基本上,它无法看到Postgres服务。文本Postgres is unavailable - sleeping来自Dockerfile

中的entrypoint.sh文件

以下是一些相关文件:

gitlab-ci.yml

image: docker:latest

services:
  - docker:dind

stages:
  - build
  - test

variables:
  CONTAINER_TEST_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME


before_script:
  - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY

build:
  stage: build
  script:
    - docker build --pull -t $CONTAINER_TEST_IMAGE --file compose/local/django/Dockerfile .
    - docker push $CONTAINER_TEST_IMAGE

pytest:
  stage: test
  script:
    - docker pull $CONTAINER_TEST_IMAGE
    - docker run --env-file=.env_dev $CONTAINER_TEST_IMAGE py.test -p no:sugar
  when: on_success

Dockerfile:

# ... other configs here
ENTRYPOINT ["compose/local/django/entrypoint.sh"]

entrypoint.sh:

# ..... other configs here

export DATABASE_URL=postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@postgres:5432/$POSTGRES_USER

function postgres_ready(){
python << END
import sys
import psycopg2
try:
    conn = psycopg2.connect(dbname="$POSTGRES_USER", user="$POSTGRES_USER", password="$POSTGRES_PASSWORD", host="postgres")
except psycopg2.OperationalError:
    sys.exit(-1)
sys.exit(0)
END
}

until postgres_ready; do
  >&2 echo "Postgres is unavailable - sleeping"
  sleep 1
done

>&2 echo "Postgres is up - continuing..."
exec $cmd

以上设置&amp;配置的灵感来自django-cookie-cutter

0 个答案:

没有答案