Skip to content

Instantly share code, notes, and snippets.

@AndrewSavetchuk
Last active October 1, 2024 03:32
Show Gist options
  • Save AndrewSavetchuk/2edfce6713548bacb23aaacf2d183342 to your computer and use it in GitHub Desktop.
Save AndrewSavetchuk/2edfce6713548bacb23aaacf2d183342 to your computer and use it in GitHub Desktop.
GitHub Actions

Laravel PHPUnit and Cypress E2E Tests Pipeline

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]
name: Laravel
jobs:
  laravel:
    name: Laravel (PHP ${{ matrix.php-versions }})
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:latest
        env:
          MYSQL_ALLOW_EMPTY_PASSWORD: false
          MYSQL_DATABASE: laravel
          MYSQL_ROOT_PASSWORD: root
          DB_PORT: 33306
        ports:
          - 33306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
    strategy:
      fail-fast: false
      matrix:
        php-versions: ['8.2']
    steps:
    - name: Checkout
      uses: actions/checkout@v4

    - name: Verify MySQL connection
      run: |
        mysql --host 127.0.0.1 --port 33306 -uroot -proot -e "SHOW DATABASES"
    
    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: ${{ matrix.php-versions }}

    - name: Get Composer cache directory
      id: composer-cache
      run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

    - name: Cache Composer dependencies
      uses: actions/cache@v3
      with:
        path: ${{ steps.composer-cache.outputs.dir }}
        key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
        restore-keys: ${{ runner.os }}-composer-

    - name: Install Composer dependencies
      run: composer install -q --no-ansi --no-interaction --no-progress --prefer-dist --optimize-autoloader

    - name: Prepare the Laravel application
      run: |
        php -r "file_exists('.env') || copy('.env.example', '.env');"
        php artisan key:generate
        php artisan --version
      env:
        DB_CONNECTION: mysql
        DB_DATABASE: laravel
        DB_PORT: 33306
        DB_USER: root
        DB_PASSWORD: root

    - name: Install NPM dependencies
      run: npm install --quiet
      
    - name: Build NPM scripts
      run: npm run build --quiet

    - name: Run the tests
      run: php artisan test
      env:
        DB_CONNECTION: mysql
        DB_DATABASE: laravel
        DB_PORT: 33306
        DB_USER: root
        DB_PASSWORD: root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment