Skip to content

Instantly share code, notes, and snippets.

@hofmannsven
Last active May 30, 2024 19:16
Show Gist options
  • Save hofmannsven/7ea03ec596d88ede45650037a25d47f7 to your computer and use it in GitHub Desktop.
Save hofmannsven/7ea03ec596d88ede45650037a25d47f7 to your computer and use it in GitHub Desktop.
Notes on working with GitHub Actions and Laravel Nova.
APP_ENV=ci
APP_KEY=
APP_DEBUG=true
APP_URL=https://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=33306
DB_DATABASE=database_ci
DB_USERNAME=user
DB_PASSWORD=secret
SESSION_DRIVER=array
CACHE_DRIVER=array
QUEUE_DRIVER=sync
MAIL_DRIVER=log
name: Tests
on: [push]
jobs:
tests:
name: Run tests
runs-on: ubuntu-latest
services:
mysql:
image: mysql:5.7
env:
MYSQL_DATABASE: database_ci
MYSQL_USER: user
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secretroot
ports:
- 33306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: actions/checkout@v1
- name: Verify MySQL connection
run: |
mysql --version
sudo apt-get install -y mysql-client
mysql --host 127.0.0.1 --port ${{ job.services.mysql.ports['3306'] }} -uuser -psecret -e "SHOW DATABASES"
- name: Cache composer dependencies
uses: actions/cache@v1
with:
path: vendor
key: composer-${{ hashFiles('composer.lock') }}
- name: Install dependencies
run: |
php --version
composer config "http-basic.nova.laravel.com" "${{ secrets.NOVA_USERNAME }}" "${{ secrets.NOVA_PASSWORD }}"
composer install -n --prefer-dist
- name: Boot Laravel application
run: |
cp .env.github .env
php artisan key:generate
php artisan --version
- name: Migrate database
run: |
mysql --version
php artisan migrate:fresh --seed
- name: Cache yarn dependencies
uses: actions/cache@v1
with:
path: node_modules
key: yarn-${{ hashFiles('yarn.lock') }}
- name: Run yarn
run: |
yarn --version
yarn && yarn dev
- name: Run tests
run: |
./vendor/bin/phpunit --version
./vendor/bin/phpunit
- name: Run security checks
run: |
test -d security-checker || git clone https://github.com/sensiolabs/security-checker.git
cd security-checker
composer install
php security-checker security:check ../composer.lock
- name: Upload artifacts
uses: actions/upload-artifact@master
if: failure()
with:
name: Logs
path: ./storage/logs
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<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_DRIVER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
</php>
</phpunit>
@Jamiewarb
Copy link

Jamiewarb commented May 18, 2022

I've updated this config:

name: ci

on:
  [push]

jobs:
  ci:
    name: PHP ${{ matrix.php }}
    runs-on: ${{ matrix.os }}
    if: "!contains(github.event.head_commit.message, '[ci skip]')"

    strategy:
      matrix:
        os: [ubuntu-latest]
        php: ['8.1'] # add '8.2' sometime soon

    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_DATABASE: database_ci
          MYSQL_USER: user
          MYSQL_PASSWORD: secret
          MYSQL_ROOT_PASSWORD: secretroot
        ports:
          - 3307:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

    steps:
      - name: Checkout 🛎
        uses: actions/checkout@master

      - name: Verify MySQL connection ❤️
        run: |
          mysql --version
          sudo apt-get install -y mysql-client
          mysql --host 127.0.0.1 --port ${{ job.services.mysql.ports['3306'] }} -uuser -psecret -e "SHOW DATABASES"

      - name: Setup the PHP ${{ matrix.php }} environment on ${{ runner.os }} 🏗
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          coverage: xdebug
        env:
          COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Get Composer cache directory path 🛠
        id: composercache
        run: echo "::set-output name=dir::$(composer config cache-files-dir)"

      - name: Cache composer 📦
        uses: actions/cache@v2
        id: php-cache # use this to check for `cache-hit` (`steps.php-cache.outputs.cache-hit != 'true'`)
        with:
          path: ${{ steps.composercache.outputs.dir }}
          key: ${{ runner.os }}-${{ matrix.php }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: ${{ runner.os }}-${{ matrix.php }}-composer-

      - name: Install Composer dependencies 💻
        run: |
          php --version
          composer config "http-basic.nova.laravel.com" "${{ secrets.NOVA_USERNAME }}" "${{ secrets.NOVA_PASSWORD }}"
          composer install --no-interaction --no-progress --prefer-dist --optimize-autoloader --no-suggest

      - name: Boot Laravel application 🥾
        run: |
          cp .github/.env.github .env
          php artisan key:generate
          php artisan --version

      - name: Migrate database 📇
        run: |
          mysql --version
          php artisan migrate:fresh --seed

      - name: Execute the PHP lint script 🧹
        run: |
          ./vendor/bin/phpstan --version
          ./vendor/bin/phpstan analyse

      - name: Run tests 🧪
        run: |
          ./vendor/bin/pest --version
          ./vendor/bin/pest

      - name: Run security checks 👮
        uses: symfonycorp/security-checker-action@v3

      - name: Upload logs on build failure 🪵
        uses: actions/upload-artifact@v3
        if: failure()
        with:
          name: Laravel Logs
          path: ./storage/logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment