Laravel Github 操作 CI/CD 、 SQLSTATE[HY000] [2002] 连接被拒绝(SQL:创建表 `migrations`

发布于 2025-01-09 14:41:48 字数 1595 浏览 0 评论 0原文

我想通过我的 yml 文件运行我的单元测试,但它们失败了,因为它们必须在 Mysql 上运行,

我认为缺少一些东西。

这是 yaml 文件:

steps:
    - uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
      with:
        php-version: '7.4|8.0'
    - uses: actions/checkout@v2
    - name: Copy .env
      run: php -r "file_exists('.env') || copy('.env.example', '.env');"
    - name: Install Dependencies
      run: composer install --no-progress --prefer-dist --optimize-autoloader
    - name: Generate key
      run: php artisan key:generate

    - name: Directory Permissions
      run: chmod -R 777 storage bootstrap/cache

    - name: Optimize Project
      run: php artisan optimize:clear

    - name: Execute tests (Unit and Feature tests) via PHPUnit
      env:
        DB_CONNECTION: mysql
        DB_HOST: 127.0.0.1
        DB_PORT: 3306
        DB_DATABASE: database_name
        DB_USERNAME: root
        DB_PASSWORD:
      run: |
        php artisan migrate
        php artisan test

这是 phpunit.xml 文件:

<server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <!-- <server name="DB_CONNECTION" value="sqlite"/> -->
        <!-- <server name="DB_DATABASE" value=":memory:"/> -->
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>

I want to run my unit tests through my yml file, but they fail because they must be run on Mysql

I think something is missing.

here is yaml file:

steps:
    - uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
      with:
        php-version: '7.4|8.0'
    - uses: actions/checkout@v2
    - name: Copy .env
      run: php -r "file_exists('.env') || copy('.env.example', '.env');"
    - name: Install Dependencies
      run: composer install --no-progress --prefer-dist --optimize-autoloader
    - name: Generate key
      run: php artisan key:generate

    - name: Directory Permissions
      run: chmod -R 777 storage bootstrap/cache

    - name: Optimize Project
      run: php artisan optimize:clear

    - name: Execute tests (Unit and Feature tests) via PHPUnit
      env:
        DB_CONNECTION: mysql
        DB_HOST: 127.0.0.1
        DB_PORT: 3306
        DB_DATABASE: database_name
        DB_USERNAME: root
        DB_PASSWORD:
      run: |
        php artisan migrate
        php artisan test

and here is phpunit.xml file:

<server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <!-- <server name="DB_CONNECTION" value="sqlite"/> -->
        <!-- <server name="DB_DATABASE" value=":memory:"/> -->
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后eg是否自 2025-01-16 14:41:48

此示例显示 将 MySQL 添加到 GitHub 操作中的示例,以便数据库可用:

您的 GitHub 操作 yaml 需要 services 部分:

    services:
      # mysql-service Label used to access the service container
      mysql:
        # Docker Hub image (also with version)
        image: mysql:5.7
        env:
          ## Accessing to Github secrets, where you can store your configuration
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: test_db
        ## map the "external" 3306 port with the "internal" 3306
        ports:
          - 3306:3306
        # Set health checks to wait until mysql database has started (it takes some seconds to start)
        options: >-
          --health-cmd="mysqladmin ping"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=3

您可以使用 GH 操作设置 env命令那个需要数据库访问权限可以包括:

      env:
        DB_CONNECTION: mysql
        DB_DATABASE: test_db
        DB_USER: root
        DB_PASSWORD: root

您可能需要查看 Chipper CI,它为您完成了很多测试 Laravel 应用程序的工作!

This example shows an example of adding MySQL into into your GitHub action, so the database is available:

Your GitHub actions yaml needs a services section:

    services:
      # mysql-service Label used to access the service container
      mysql:
        # Docker Hub image (also with version)
        image: mysql:5.7
        env:
          ## Accessing to Github secrets, where you can store your configuration
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: test_db
        ## map the "external" 3306 port with the "internal" 3306
        ports:
          - 3306:3306
        # Set health checks to wait until mysql database has started (it takes some seconds to start)
        options: >-
          --health-cmd="mysqladmin ping"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=3

The env you can set with your GH Actions command that needs database access can include:

      env:
        DB_CONNECTION: mysql
        DB_DATABASE: test_db
        DB_USER: root
        DB_PASSWORD: root

You may want to check out Chipper CI, which does a lot of this work for you for testing Laravel apps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文