Skip to content

Instantly share code, notes, and snippets.

@alies-dev
Last active July 5, 2024 09:07
Show Gist options
  • Save alies-dev/4d40a4facee0271ce88bf5d0282e97ea to your computer and use it in GitHub Desktop.
Save alies-dev/4d40a4facee0271ce88bf5d0282e97ea to your computer and use it in GitHub Desktop.
Laravel GitHub workflows (starting pack)

Context

These assets created for the First hour of a new Laravel project article.

Instructions

Add these files to the .github/workflows directory of your project, commit and push them.

main.yml example uses PostgreSQL DB as an exammple. You can read more about other services:

name: Run tests
on: [push]
jobs:
php-tests:
name: Run PHP tests
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_DB: laravel
POSTGRES_USER: root
POSTGRES_PASSWORD: secret
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432
steps:
- uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv
coverage: none
- name: Install Composer dependencies
uses: ramsey/composer-install@v3
- name: Prepare Laravel Application
run: |
cp .env.example .env
php artisan key:generate
# It speeds up (in theory) the following steps and sort of tests routes:
# this command may fail in case of route definition issues (e.g., not existing controller).
# Please it tests whether routes are cacheable (what is essential for production)
- name: Cache Laravel routes
run: php artisan route:cache
- name: Run tests
run: php artisan test --parallel
env:
DB_HOST: 127.0.0.1
DB_PORT: 5432
DB_DATABASE: laravel
DB_USERNAME: root
DB_PASSWORD: secret
- name: Run composer security audit
run: composer audit
deploy:
name: Deploy
runs-on: ubuntu-latest
# jobs that must complete successfully before this job will run
needs: [ php-tests ]
# Deploy if the push is tagged by a git tag (release)
if: ${{ startsWith(github.ref, 'refs/tags/') && github.event_name == 'push' }}
concurrency: deploy-${{ github.ref }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
#todo add next steps, e.g. ping Laravel Forge deployment URL
name: PHP code style
on:
workflow_dispatch:
push:
branches-ignore:
- 'dependabot/*'
paths:
- '**.php'
- 'composer*'
- 'pint.json'
jobs:
php-style:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: "laravel-pint"
uses: aglipanci/laravel-pint-action@2.0.0
with:
verboseMode: true
name: PHP quality check
on:
workflow_dispatch:
push:
branches-ignore:
- 'dependabot/*'
paths:
- '**.php'
- 'composer*'
- 'phpstan*'
jobs:
static-analysis:
name: Static Analysis
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
coverage: none
- name: Install Composer dependencies
uses: ramsey/composer-install@v3
- name: PHPStan
run: ./vendor/bin/phpstan --error-format=github
- name: Analyze composer dependencies
run: ./vendor/bin/composer-dependency-analyser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment