GitLab CI - 使用Mysql服务

时间:2018-04-08 14:20:00

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

我尝试在带有symfony的web项目中使用GitLab CI。

要执行某些测试,我需要有一个数据库并从应用程序访问它,但它不起作用。看来mysql服务无法访问......实际上,我只是在gitlab-ci.yml文件的顶部声明服务并设置变量来配置它。是否有可能将服务链接到我自己的应用服务以使用它?

我的gitlab-ci.yml:

image: docker:latest

services:
- docker:dind
- name: mysql:5.7.21
  alias: db

stages:
- build
- test
- release

variables:
  MYSQL_ROOT_PASSWORD: collection-manager
  MYSQL_USER: collection-manager
  MYSQL_PASSWORD: collection-manager
  MYSQL_DATABASE: collection-manager
  CONTAINER_IMAGE: registry.gitlab.com/$CI_PROJECT_PATH
  CONTAINER_TEST_IMAGE: registry.gitlab.com/$CI_PROJECT_PATH:$CI_COMMIT_REF_NAME
  CONTAINER_RELEASE_IMAGE: registry.gitlab.com/$CI_PROJECT_PATH:latest
  DOCKER_DRIVER: overlay2

before_script:
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN registry.gitlab.com

build:
  stage: build
  script:
    - docker pull $CONTAINER_RELEASE_IMAGE || true
    - docker build --cache-from $CONTAINER_RELEASE_IMAGE -t $CONTAINER_TEST_IMAGE .
    - docker push $CONTAINER_TEST_IMAGE

cs-test:
  stage: test
  script:
    - docker pull $CONTAINER_TEST_IMAGE
    - docker run $CONTAINER_TEST_IMAGE bash -c "composer install --no-progress --no-suggest --optimize-autoloader --no-interaction; ./vendor/bin/php-cs-fixer fix --diff --dry-run -v"
    - docker run $CONTAINER_TEST_IMAGE bash -c "composer install --no-progress --no-suggest --optimize-autoloader --no-interaction; ./bin/console lint:yaml app"
    - docker run $CONTAINER_TEST_IMAGE bash -c "composer install --no-progress --no-suggest --optimize-autoloader --no-interaction; ./bin/console lint:twig app/Resources"
    - docker run $CONTAINER_TEST_IMAGE bash -c "composer install --no-progress --no-suggest --optimize-autoloader --no-interaction; composer validate --strict"

release-image:
  stage: release
  script:
    - docker pull $CONTAINER_TEST_IMAGE
    - docker tag $CONTAINER_TEST_IMAGE $CONTAINER_RELEASE_IMAGE
    - docker push $CONTAINER_RELEASE_IMAGE
  only:
    - master

在工作cs-test中,我得到这样的回报:

docker run $CONTAINER_TEST_IMAGE bash -c "composer install --no-progress --no-suggest --optimize-autoloader --no-interaction; ./vendor/bin/php-cs-fixer fix --diff --dry-run -v"

Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Package operations: 8 installs, 0 updates, 0 removals
  - Installing doctrine/data-fixtures (v1.3.0): Downloading (100%)
  - Installing doctrine/doctrine-fixtures-bundle (v2.4.1): Downloading (100%)
  - Installing symfony/polyfill-php72 (v1.7.0): Downloading (100%)
  - Installing php-cs-fixer/diff (v1.3.0): Downloading (100%)
  - Installing composer/semver (1.4.2): Downloading (100%)
  - Installing friendsofphp/php-cs-fixer (v2.11.1): Downloading (100%)
  - Installing sensio/generator-bundle (v3.1.7): Downloading (100%)
  - Installing symfony/phpunit-bridge (v3.4.8): Downloading (100%)
Generating optimized autoload files
ocramius/package-versions:  Generating version class...
ocramius/package-versions: ...done generating version class
> Incenteev\ParameterHandler\ScriptHandler::buildParameters
Creating the "app/config/parameters.yml" file
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache

In AbstractMySQLDriver.php line 112:

  An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddr  
  esses: getaddrinfo failed: Name or service not known                         


In PDOConnection.php line 50:

  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name o  
  r service not known                                                          


In PDOConnection.php line 46:

  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name o  
  r service not known                                                          


In PDOConnection.php line 46:

  PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or s  
  ervice not known                                                             


Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the symfony-scripts event terminated with an exception


  [RuntimeException]                                                               
  An error occurred when executing the "'cache:clear --no-warmup'" command:        




  In AbstractMySQLDriver.php line 112:                                             

    An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddr    
    esses: getaddrinfo failed: Name or service not known                           


  In PDOConnection.php line 50:                                                    

    SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name o    
    r service not known                                                            


  In PDOConnection.php line 46:                                                    

    SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known                                                            


  In PDOConnection.php line 46:                                                    

    PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known

我不明白为什么......应用程序有一个默认连接:

- host: db
- db name: collection-manager
- db user: collection-manager
- db pass: collection-manager

2 个答案:

答案 0 :(得分:0)

在我使用MySQL进行集成测试的项目中,我具有以下env变量:

MYSQL_USER: 'your_user'
MYSQL_PASSWORD: 'your_pass'
MYSQL_HOST: 'mysql__mysql-server'
MYSQL_PORT: 3306
MYSQL_DATABASE_NAME: 'testdb'
MYSQL_DATABASE: 'testdb'
MYSQL_ROOT_HOST: '%'
MYSQL_ROOT_PASSWORD: 'your_rootpw'

我记得我花了很多时间弄清主机名,我发现您在配置中也缺少它。

答案 1 :(得分:0)

将网络模式设置为主机(在macOS下将不起作用)并添加自定义的主机到IP的映射:

services:
  - docker:dind
  - mysql:5.7
script:
  - export # just for debugging
  - docker build --network=host --add-host mysql:${MYSQL_PORT_3306_TCP_ADDR} .