0

我有一个 laravel 应用程序和一个工作的 Postgis 容器。我正在尝试使用 github 工作流 beta 为我的项目设置 CI。我的工作流程在尝试访问数据库时失败。对于本地环境,我正在使用我的容器名称设置我的数据库主机canie-db。但是在我的 github 工作流程中,我无法通过容器名称找到我的数据库主机。当它尝试运行我的测试时出现此错误:

Doctrine\DBAL\Driver\PDOException: SQLSTATE[08006] [7] 无法将主机名“canie-db”转换为地址:名称解析临时失败

这是我的.github/workflows/ci.yml

name: CanieCICD

on:
  push:
    branches:
      - master
      - stage
      - ci/cd-patch
  pull_request:
    branches:
      - stage
      - ci/cd-patch

jobs:
  build:

    runs-on: ubuntu-18.04

    services:
      canie-db:
        image: mdillon/postgis:10
        ports:
        - 5432:5432
        env:
          POSTGRES_DB: canie-db
          POSTGRES_USER: iabs_user
          POSTGRES_PASSWORD: p@ssw0rd

    steps:

    - uses: actions/checkout@v1

    - name: Use Node.js 12.x
      uses: actions/setup-node@v1
      with:
        node-version: 12.x

    - name: Install composer dependencies
      run: composer install --prefer-dist

    - name: Run PHPUnit tests # CI fails here!
      run: vendor/bin/phpunit --no-coverage

    - name: Install npm dependencies
      run: npm install

    - name: Run Mix
      run: npm run production

我的Laravel 后端数据库配置文件config/database.php'default' => env('DB_CONNECTION', 'pgsql'),

'pgsql' => [
    'driver' => 'pgsql',
    'url' => env('DATABASE_URL'),
    'host' => env('DB_HOST', 'canie-db'),
    'port' => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'canie-db'),
    'username' => env('DB_USERNAME', 'iabs_user'),
    'password' => env('DB_PASSWORD', 'p@ssw0rd'),
    'charset' => 'utf8',
    'prefix' => '',
    'prefix_indexes' => true,
    'schema' => 'public',
    'sslmode' => 'prefer',
],

我也尝试添加docker-compose up -d canie-db到我的工作流程中,但它也不起作用

4

1 回答 1

0

我认为答案在评论和发布的示例中阐明了https://github.com/actions/example-services/blob/master/.github/workflows/postgres-service.yml

为什么它不起作用是您的工作流程/步骤在 VM(ubuntu-18.04)中运行,并且 VM 不知道canie-db的 docker 容器主机名

一种解决方法是使用 localhost 因为端口是暴露的

'host' => env('DB_HOST', 'localhost'),

或者,就像您在示例中发现的那样,或者可以选择在容器中运行工作流和 postgres。这样主机canie-db是已知的。

container:
  image:  node:10.16-jessie
于 2020-08-29T14:35:41.643 回答