Skip to content

Instantly share code, notes, and snippets.

@ursuleacv
Forked from madsem/cd.yml
Created October 12, 2024 17:51
Show Gist options
  • Save ursuleacv/37cfbecb2311b01f436d10bcc90fa7bc to your computer and use it in GitHub Desktop.
Save ursuleacv/37cfbecb2311b01f436d10bcc90fa7bc to your computer and use it in GitHub Desktop.
GitHub Workflows For: Laravel CI with Mysql 8 & Laravel Vapor Deployment

Laravel Github Actions Workflows:

Laravel CI with Mysql 8 and LOAD DATA LOCAL INFILE enabled

Create .github/workflows/ci.yml

Add required Github secrets.

Add .env.ci to your Laravel project:

APP_ENV=testing
APP_KEY=base64:wevTz42eYncA2j32iQwRWyu5DFb4QVydrl/PmLxxx1s=
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=root
...
...

CI Status Badge:

[![Actions Status](https://github.com/<USER>/<REPO>/workflows/Laravel%20CI/badge.svg)](https://github.com/<USER>/<REPO>/actions)

Laravel Vapor Deployment

Deploy on master branch, when creating a new release, or deleting a release (rollback).

Create .github/workflows/cd.yml

Add required Github secrets.

CI Status Badge:

[![Actions Status](https://github.com/<USER>/<REPO>/workflows/Laravel%20Vapor%20CD/badge.svg)](https://github.com/<USER>/<REPO>/actions)
name: Laravel Vapor CD
on:
release:
types: [ published, deleted ]
branches:
- master
jobs:
deploy_release:
runs-on: ubuntu-20.04
steps:
- id: latest
uses: thebritican/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout Latest Version βœ…
uses: actions/checkout@v2
with:
ref: ${{ steps.latest.outputs.tag_name }}
- name: Setup Composer Parallel Downloads πŸ’₯
run: composer global require hirak/prestissimo
- name: Get Composer Cache Directory πŸ“‚
id: get-composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache Composer βž•
uses: actions/cache@v1
id: composer-cache
with:
path: ${{ steps.get-composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Configure composer for Laravel Nova πŸ”‘
run: composer config "http-basic.nova.laravel.com" "${{ secrets.NOVA_USERNAME }}" "${{ secrets.NOVA_PASSWORD }}"
- name: Install Laravel Vapor-CLI πŸ’­
run: composer require laravel/vapor-cli
- name: Install composer dependencies πŸ€–
run: composer install --no-dev --no-progress --prefer-dist --no-interaction --no-suggest --optimize-autoloader
- name: Deploy Production Code πŸš€
run: |
php vendor/bin/vapor deploy some-environment --commit="${{ github.sha }}"
php vendor/bin/vapor deploy another-environment --commit="${{ github.sha }}"
env:
VAPOR_API_TOKEN: ${{ secrets.VAPOR_API_TOKEN }}
- name: Upload Failure Artifacts 😰
uses: actions/upload-artifact@v2
if: failure()
with:
name: Logs
path: ./storage/logs
- name: Slack Notify Deployment Failure πŸ””
if: failure()
uses: rtCamp/[email protected]
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
SLACK_COLOR: 'danger'
SLACK_TITLE: Laravel Vapor Deployment failed!
SLACK_USERNAME: Github
name: Laravel CI
on: [ pull_request ]
jobs:
laravel:
name: Laravel Testsuite
runs-on: ubuntu-20.04 # has mysql 8 installed by default
steps:
- name: Set Up Mysql 8 βš™οΈ
run: |
echo -e "Enable LOAD DATA LOCAL INFILE in my.cnf\n"
echo -e "SETTING secure-file-priv TO EMPTY STRING\n"
echo -e "[mysqld]\nsecure-file-priv=''" | sudo tee -a /etc/mysql/my.cnf
echo -e "SETTING local_infile TO ON\n"
echo -e "[mysqld]\nlocal_infile='ON'" | sudo tee -a /etc/mysql/my.cnf
echo -e "Start MYSQL service, it is off by default\n"
sudo systemctl enable mysql.service
sudo systemctl start mysql.service
echo -e "Creating Laravel Database\n"
mysql --host 127.0.0.1 -uroot -proot -e 'CREATE DATABASE IF NOT EXISTS laravel;'
echo -e "Check new settings\n"
mysql --host 127.0.0.1 -uroot -proot -e "SELECT @@global.secure_file_priv, @@global.local_infile"
- name: Checkout βœ…
uses: actions/checkout@v2
- name: Setup Composer Parallel Downloads πŸ’₯
run: composer global require hirak/prestissimo
- name: Get Composer Cache Directory πŸ“‚
id: get-composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache Composer βž•
uses: actions/cache@v1
id: composer-cache
with:
path: ${{ steps.get-composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Configure composer for Laravel Nova πŸ”‘
run: composer config "http-basic.nova.laravel.com" "${{ secrets.NOVA_USERNAME }}" "${{ secrets.NOVA_PASSWORD }}"
- name: Install composer dependencies πŸ€–
run: composer install --no-progress --prefer-dist --no-interaction --no-suggest --optimize-autoloader --no-scripts
- name: Clear Config πŸ—‘οΈ
run: |
php artisan config:clear
php artisan cache:clear
- name: Prepare the application πŸ”§
run: |
cp .env.ci .env
php artisan key:generate
- name: Run Migrations πŸ’Ύ
run: php artisan migrate --force -v
- name: Test with phpunit 🧐
run: vendor/bin/phpunit
- name: Run Sensiolabs 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: Slack Notify CI Failure πŸ””
if: failure()
uses: rtCamp/[email protected]
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
SLACK_COLOR: 'danger'
SLACK_TITLE: Continous Integration failed
SLACK_USERNAME: Github
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment