Astrotomic / opendor.me

https://opendor.me
Other
75 stars 29 forks source link

Deployer script #44

Open Gummibeer opened 3 years ago

Gummibeer commented 3 years ago

Right now I'm using simple SSH deployment - because I have multiple hosts and want to maintain the deployment script next to the code I want to use https://deployer.org instead.

If you don't know deployer yet - @lorisleiva has a great course about deployer with Laravel https://lorisleiva.com/deploy-your-laravel-app-from-scratch

The current deployment script is:

$FORGE_PHP artisan backup:run
rm -f bootstrap/cache/config.php
$FORGE_PHP artisan optimize:clear

git pull origin main
$FORGE_COMPOSER update --no-interaction --prefer-dist --optimize-autoloader --no-dev

( flock -w 10 9 || exit 1
    echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock

yarn install
yarn production
$FORGE_PHP artisan blade-fontawesome:sync-pro
$FORGE_PHP artisan icons:cache
$FORGE_PHP artisan nova:publish
$FORGE_PHP artisan horizon:publish

$FORGE_PHP artisan config:cache
$FORGE_PHP artisan event:cache
$FORGE_PHP artisan route:cache
$FORGE_PHP artisan view:cache
$FORGE_PHP artisan migrate --force
$FORGE_PHP artisan permission:cache-reset
$FORGE_PHP artisan permission:ensure
$FORGE_PHP artisan schedule-monitor:sync
lorisleiva commented 3 years ago

I haven't tested it and it can be cleaned up slightly using recipes but here's the equivalent as a Deployer config file. Just put that in the root of your app as deploy.yaml, tweak the hosts user, IP and deploy path and you should be sorted.

I've taken the liberty to push the php-fpm:reload at the very end of your deployment but feel free to put it back in the middle.

May I ask why you need to run nova:publish and horizon:publish during your deployment? I've recently removed them from the Deployer core since they're more local commands IMO but I could fix this if there's a good use case for it. :)

import:
  - recipe/laravel.php
  - contrib/php-fpm.php
  - contrib/yarn.php

config:
  application: 'opendor.me'
  repository: 'git@github.com:Astrotomic/opendor.me.git'
  php_fpm_version: '8.0'

hosts:
  prod_1:
    remote_user: forge
    hostname: '139.59.161.100'
    deploy_path: '~/opendor.me'
  prod_2:
    remote_user: forge
    hostname: '139.59.161.101'
    deploy_path: '~/opendor.me'

tasks:
  deploy:
    - artisan:backup:up
    - cache:config:delete
    - artisan:optimize:clear
    - deploy:prepare
    - deploy:vendors
    - yarn:install
    - yarn:run:prod
    - artisan:blade-fontawesome:sync-pro
    - artisan:icons:cache
    - artisan:nova:publish
    - artisan:horizon:publish
    - artisan:config:cache
    - artisan:event:cache
    - artisan:route:cache
    - artisan:view:cache
    - artisan:migrate
    - artisan:permission:cache-reset
    - artisan:permission:ensure
    - artisan:schedule-monitor:sync
    - deploy:publish
    - php-fpm:reload
  artisan:backup:up:
    script: 'cd {{release_or_current_path}} && {{bin/php}} artisan backup:run'
  cache:config:delete:
    script: 'cd {{release_or_current_path}} && rm -f bootstrap/cache/config.php'
  artisan:blade-fontawesome:sync-pro:
    script: 'cd {{release_or_current_path}} && {{bin/php}} artisan blade-fontawesome:sync-pro'
  artisan:icons:cache:
    script: 'cd {{release_or_current_path}} && {{bin/php}} artisan icons:cache'
  artisan:nova:publish:
    script: 'cd {{release_or_current_path}} && {{bin/php}} artisan nova:publish'
  artisan:horizon:publish:
    script: 'cd {{release_or_current_path}} && {{bin/php}} artisan horizon:publish'
  artisan:permission:cache-reset:
    script: 'cd {{release_or_current_path}} && {{bin/php}} artisan permission:cache-reset'
  artisan:permission:ensure:
    script: 'cd {{release_or_current_path}} && {{bin/php}} artisan permission:ensure'
  artisan:schedule-monitor:sync:
    script: 'cd {{release_or_current_path}} && {{bin/php}} artisan schedule-monitor:sync'
  yarn:run:prod:
    script: 'cd {{release_or_current_path}} && {{bin/yarn}} run production'

after:
  deploy:failed: deploy:unlock
adevade commented 3 years ago

Yeah, I would put the horizon:publish on the update hook of composer instead of in production, as suggested in the docs: https://laravel.com/docs/8.x/horizon#upgrading-horizon

{
  "scripts": {
    "post-update-cmd": [
      "@php artisan horizon:publish --ansi"
    ]
  }
}
Gummibeer commented 3 years ago

As I run composer update on production as well this isn't important.^^ But is applied. I simply don't want/will manage the composer.lock file and it's never ending merge conflicts.