Skip to content

Instantly share code, notes, and snippets.

@troccoli
Created June 3, 2022 17:06
Show Gist options
  • Save troccoli/c919d766157f2f1167490165cdb87d16 to your computer and use it in GitHub Desktop.
Save troccoli/c919d766157f2f1167490165cdb87d16 to your computer and use it in GitHub Desktop.
Cypress, Laravel Sail, GitHub Action
# .github/workflows/ci.yml
name: CI
on: push
env:
node-modules-cache-name: cache-node-modules-5
composer-packages-cache-name: cache-composer-packages-5
build-artifacts-cache-name: cache-build-artifacts-5
jobs:
build-matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v3
- id: set-matrix
run: echo "::set-output name=matrix::{\"php-versions\":[ '8.1' ]}"
install-dependencies:
needs: build-matrix
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJSON(needs.build-matrix.outputs.matrix) }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
- name: Cache node modules
id: cache-node-modules
uses: actions/cache@v3
with:
path: |
~/.cache/Cypress
./node_modules
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-build-${{ env.node-modules-cache-name }}-${{ hashFiles('**/package-lock.json') }}
- if: steps.cache-composer-packages.outputs.cache-hit != 'true'
run: npm install
- name: Install Cypress
run: npm i cypress
- name: Verify Cypress
uses: cypress-io/github-action@v4
with:
runTests: false
- name: Cache composer packages
id: cache-composer-packages
uses: actions/cache@v3
with:
path: ./vendor
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-build-${{ env.composer-packages-cache-name }}-${{ hashFiles('**/composer.lock') }}
- if: steps.cache-composer-packages.outputs.cache-hit != 'true'
run: composer install --no-interaction --ignore-platform-reqs --optimize-autoloader
- name: Cache build artifacts
id: cache-build-artifacts
uses: actions/cache@v3
with:
path: |
./public/css
./public/js
./public/mix-manifest.json
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-build-${{ env.build-artifacts-cache-name }}
- run: npm run prod
unit-tests:
needs: [ build-matrix, install-dependencies ]
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJSON(needs.build-matrix.outputs.matrix) }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
- name: Debug
run: |
php --version
composer --version
node --version
npm --version
- name: Setup Environment
run: |
cp .env.ci .env
touch ./storage/logs/laravel.log
touch ./database/database.sqlite
- name: Restore node modules
id: cache-node-modules
uses: actions/cache@v3
with:
path: ./node_modules
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-build-${{ env.node-modules-cache-name }}-${{ hashFiles('**/package-lock.json') }}
- name: Restore composer packages
id: cache-composer-packages
uses: actions/cache@v3
with:
path: ./vendor
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-build-${{ env.composer-packages-cache-name }}-${{ hashFiles('**/composer.lock') }}
- name: Restore build artifacts
id: cache-build-artifacts
uses: actions/cache@v3
with:
path: |
./public/css
./public/js
./public/mix-manifest.json
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-build-${{ env.build-artifacts-cache-name }}
- name: Migrate Database
run: php artisan migrate --force
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Dump Autoloader
run: composer dump-autoload
- name: Run Laravel Server
run: php artisan serve &
- name: Execute all tests via PHPUnit
id: phpunit
run: vendor/bin/phpunit --testsuite Unit
feature-tests:
needs: [ build-matrix, install-dependencies ]
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJSON(needs.build-matrix.outputs.matrix) }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
- name: Debug
run: |
php --version
composer --version
node --version
npm --version
- name: Setup Environment
run: |
cp .env.ci .env
touch ./storage/logs/laravel.log
touch ./database/database.sqlite
- name: Restore node modules
id: cache-node-modules
uses: actions/cache@v3
with:
path: ./node_modules
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-build-${{ env.node-modules-cache-name }}-${{ hashFiles('**/package-lock.json') }}
- name: Restore composer packages
id: cache-composer-packages
uses: actions/cache@v3
with:
path: ./vendor
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-build-${{ env.composer-packages-cache-name }}-${{ hashFiles('**/composer.lock') }}
- name: Restore build artifacts
id: cache-build-artifacts
uses: actions/cache@v3
with:
path: |
./public/css
./public/js
./public/mix-manifest.json
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-build-${{ env.build-artifacts-cache-name }}
- name: Migrate Database
run: php artisan migrate --force
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Dump Autoloader
run: composer dump-autoload
- name: Run Laravel Server
run: php artisan serve &
- name: Execute all tests via PHPUnit
id: phpunit
run: vendor/bin/phpunit --testsuite Feature
cypress-tests:
needs: [ build-matrix, install-dependencies ]
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJSON(needs.build-matrix.outputs.matrix) }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
- name: Debug
run: |
php --version
composer --version
node --version
npm --version
- name: Setup Environment
run: |
cp .env.cypress .env
touch ./storage/logs/laravel.log
touch ./database/database.sqlite
- name: Restore node modules
id: cache-node-modules
uses: actions/cache@v3
with:
path: |
~/.cache/Cypress
./node_modules
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-build-${{ env.node-modules-cache-name }}-${{ hashFiles('**/package-lock.json') }}
- name: Restore composer packages
id: cache-composer-packages
uses: actions/cache@v3
with:
path: ./vendor
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-build-${{ env.composer-packages-cache-name }}-${{ hashFiles('**/composer.lock') }}
- name: Restore build artifacts
id: cache-build-artifacts
uses: actions/cache@v3
with:
path: |
./public/css
./public/js
./public/mix-manifest.json
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-build-${{ env.build-artifacts-cache-name }}
- name: Migrate Database
run: php artisan migrate --force
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Dump Autoloader
run: composer dump-autoload
- name: Run Laravel Server
run: php artisan serve &
- name: Run Cypress Tests
id: cypress-tests
uses: cypress-io/github-action@v4
with:
install: false
wait-on: 'http://127.0.0.1:8000'
config: baseUrl=http://127.0.0.1:8000
config-file: ./cypress.config.js
record: true
project: ./
env:
# pass the Dashboard record key as an environment variable
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
# pass GitHub token to allow accurately detecting a build vs a re-run build
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# pass the project ID from the secrets through environment variable
CYPRESS_PROJECT_ID: ${{ secrets.CYPRESS_PROJECT_ID }}
- name: Upload screenshots
uses: actions/upload-artifact@v3
if: failure()
with:
name: ${{ github.job }}-screenshots
path: cypress/screenshots
- name: Upload videos
uses: actions/upload-artifact@v3
if: failure()
with:
name: ${{ github.job }}-videos
path: cypress/videos
const {defineConfig} = require("cypress");
module.exports = defineConfig({
e2e: {
baseUrl: 'http://localhost',
chromeWebSecurity: false,
defaultCommandTimeout: 5000,
retries: {
runMode: 2,
openMode: 2
},
setupNodeEvents(on, config) {
on('task', {
log(message) {
console.log(message)
return null
},
})
},
},
});
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.1
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.1/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mariadb
mariadb:
image: 'mariadb:10'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmariadb:/var/lib/mysql'
networks:
- sail
healthcheck:
test: [ "CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}" ]
retries: 3
timeout: 5s
cypress:
image: 'cypress/included:10.0.1'
profiles:
- 'on-demand-only'
volumes:
- '.:/e2e'
- '/tmp/.X11-unix:/tmp/.X11-unix'
working_dir: '/e2e'
environment:
- CYPRESS_baseUrl=http://laravel.test
- CYPRESS_VIDEO=false
- DISPLAY=host.docker.internal:0
networks:
- sail
depends_on:
- laravel.test
entrypoint: cypress
networks:
sail:
driver: bridge
volumes:
sailmariadb:
driver: local
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment