Skip to content

Instantly share code, notes, and snippets.

@ghostwriter
Last active May 23, 2020 12:20
Show Gist options
  • Select an option

  • Save ghostwriter/2c8539659727ab89162eb85c98b115a9 to your computer and use it in GitHub Desktop.

Select an option

Save ghostwriter/2c8539659727ab89162eb85c98b115a9 to your computer and use it in GitHub Desktop.
Deploy a Laravel app for free with GitHub Actions
name: CD
on:
push:
branches: [ production ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
token: ${{ secrets.PUSH_TOKEN }}
- name: Set up Node
uses: actions/setup-node@v1
with:
node-version: '12.x'
- run: npm install
- run: npm run production
- name: Commit built assets
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git checkout -B deploy
git add -f public/
git commit -m "Build front-end assets"
git push -f origin deploy
- name: Deploy to production
uses: appleboy/ssh-action@master
with:
username: YOUR USERNAME GOES HERE
host: YOUR SERVER'S HOSTNAME GOES HERE
password: ${{ secrets.SSH_PASSWORD }}
script: 'cd /var/www/html && ./server_deploy.sh'
name: CI
on: [ push, pull_request ]
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: composer install
- name: Run tests
run: vendor/bin/phpunit
#!/bin/sh
set -e
vendor/bin/phpunit
npm run prod
git add .
(git commit -m "Build frontend assets for deployment to production") || true
(git push) || true
git checkout production
git merge master
git push origin production
git checkout master
#!/bin/bash
set -e
echo "Deploying application ..."
# Enter maintanance mode
php artisan down
# Update codebase
git fetch origin deploy
git reset --hard origin/deploy
# Install dependencies based on lock file
composer install --no-interaction --prefer-dist --optimize-autoloader
# Migrate database
php artisan migrate --force
# Clear cache
php artisan optimize
# Reload PHP to update opcache
echo "" | sudo -S service php7.4-fpm reload
# Exit maintenance mode
php artisan up
echo "🚀 Application deployed!"
@ghostwriter

Copy link
Copy Markdown
Author
  • Some public/ assets are in .gitignore, but are built on GitHub
  • master branch is used for development
  • production is pushed and deployed
  • deploy is created by the Action, by taking production and adding a commit with the built assets

So the code flows like this: master --> production --> deploy.

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