Skip to content

Instantly share code, notes, and snippets.

@kikoseijo
Last active January 12, 2021 16:55
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kikoseijo/205c49fbc66aba03bbb6d3ff456124ef to your computer and use it in GitHub Desktop.
Laravel envoy template for Virtualmin & Webmin servers with cron jobs
@servers(['remote' => 'ks1.xxxxx.com', 'local' => '127.0.0.1'])
@include('vendor/autoload.php')
@setup
# customize this keys...
# USERNAME_HERE, DOMAIN_NAME_HERE, REPO_GROUP, REPO_NAME
$dotenv = Dotenv\Dotenv::create(__DIR__, '.env');
$dotenv->load();
$now = date('Ymd-His');
$branch = "origin/master";
$username = "USERNAME_HERE";
$key_email = "no-reply@DOMAIN_NAME_HERE.com";
$repo_domain = "git.DOMAIN_NAME_HERE.com";
$repo_group = "REPO_GROUP";
$repo_name = "REPO_NAME";
$project_root = "/home/$username/$repo_name";
$domain = $username.'.DOMAIN_NAME_HERE.com';
$slack = env('SLACK_ENDPOINT');
$crons = [
'Laravel Schedule' => [
'* * * * *',
"/usr/bin/php71 $project_root/artisan schedule:run >> /dev/null 2>&1"
]
];
$alias = [
'alias php="/usr/bin/php71"',
'alias composer="php /bin/composer"',
'alias ka="php artisan"',
'alias kdump="composer dump-autoload -o"',
'alias klean="ka clear-compiled && ka migrate:refresh && ka db:seed"',
];
$envs = [
'SLACK_ENDPOINT="https://hooks.slack.com/services/XXX/XX/XXX"',
];
@endsetup
@task('php', ['on' => 'remote'])
su -l {{ $username }}
php -v
@endtask
@story('upgrade_php')
yum install rh-php72 rh-php72-php-mysqlnd rh-php72-php-mbstring rh-php72-php-fpm rh-php72-php-xml rh-php72-php-gd rh-php72-php-soap rh-php72-php-bcmath
@endstory
@story('doall')
build_production
deploy
build_dev
@endstory
@task('build_production', ['on' => 'local'])
yarn prod
git add .
git commit -m "Commit before deploy {{$now}}"
git push -u origin master
@endtask
@task('build_dev', ['on' => 'local'])
yarn dev
@endtask
@task('deploy', ['on' => 'remote'])
su -l {{ $username }}
cd ~/{{ $repo_name }}
php artisan down --message="Upgrading system.." --retry=60
git fetch --all
git reset --hard {{ $branch }}
git pull origin master
@if ($u)
composer update --no-dev --prefer-dist --optimize-autoloader --ignore-platform-reqs
@else
composer install --no-dev --prefer-dist --optimize-autoloader --ignore-platform-reqs
@endif
@if ($m)
php artisan migrate --force
@endif
php artisan config:clear
php artisan config:cache
php artisan view:clear
php artisan view:cache
php artisan up
@endtask
@task('update', ['on' => 'remote'])
su -l {{ $username }}
cd ~/{{ $repo_name }}
php artisan down --message="Upgrading system.." --retry=60
composer update --no-dev --prefer-dist
php artisan config:clear
php artisan config:cache
php artisan view:clear
php artisan view:cache
php artisan up
@endtask
@task('passport', ['on' => 'remote'])
su -l {{ $username }}
cd ~/{{ $repo_name }}
php artisan passport:keys
php artisan passport:install
# php artisan vendor:publish --tag=passport-components
@endtask
@task('composer', ['on' => 'remote'])
su -l {{ $username }}
cd ~/{{ $repo_name }}
php artisan down --message="Upgrading system.." --retry=60
composer install --no-dev --prefer-dist
php artisan up
@endtask
@task('ssh', ['on' => 'remote', 'confirm' => true])
su -l {{ $username }}
[ -d ~/.ssh ] || echo "~/.ssh directory does not exist, lets create"
[ -d ~/.ssh ] || mkdir ~/.ssh
echo "Adding {{ $repo_domain }} domain to known_hosts"
ssh-keyscan {{ $repo_domain }} >> ~/.ssh/known_hosts
cd ~/.ssh
echo "Deleting ssh keys..."
rm -rf id_rsa id_rsa.pub
ssh-keygen -t rsa -C "{{ $repo_domain }}" -b 4096 -N "" -f id_rsa
cat ~/.ssh/id_rsa.pub
@endtask
@story('setup')
download
install
@endstory
@task('download', ['on' => ['remote']])
su -l {{ $username }}
cd ~
echo "Cloning {{ "git@" . $repo_domain .":". $repo_group ."/" . $repo_name . ".git" }}"
git clone {{ "git@" . $repo_domain .":". $repo_group ."/" . $repo_name . ".git" }}
cd ~/{{ $repo_name }}
echo "Done download"
@endtask
@task('install', ['on' => ['remote']])
su -l {{ $username }}
cd ~/{{ $repo_name }}
composer install --no-dev
[ -f .env ] || echo ".env does not exist, using default .env.example"
[ -f .env ] || cp .env.example .env
echo "Generating new KEY for .env"
php artisan key:generate
echo "Linking storage folder"
php artisan storage:link
@endtask
@task('alias', ['on' => ['remote']])
su -l {{ $username }}
cd ~
@foreach ($alias as $alia)
LINE='{{$alia}}'
FILE=.profile
grep -qF "$LINE" "$FILE" || echo "$LINE" >> "$FILE"
@endforeach
alias
echo "Done."
@endtask
@task('add_env', ['on' => ['remote']])
su -l {{ $username }}
cd ~/{{ $repo_name }}
@foreach ($envs as $env)
LINE='{{$env}}'
FILE=.env
grep -qF "$LINE" "$FILE" || echo "$LINE" >> "$FILE"
@endforeach
cat .env
echo "Done."
@endtask
@task('publish', ['on' => ['remote']])
su -l {{ $username }}
cd ~
rm -rf public_html
ln -s ~/{{ $repo_name }}/public ~/public_html
echo "App should be now live."
@endtask
@task('cron_add', ['on' => 'remote'])
su -l {{ $username }}
cd ~/{{ $repo_name }}
echo "# write out current crontab"
@foreach ($crons as $cron_name => $cron_job)
echo "{{$cron_job[1]}}"
croncmd="{{$cron_job[1]}}"
cronjob="{{$cron_job[0]}} $croncmd"
( crontab -l | grep -v -F "$croncmd" ; echo "$cronjob" ) | crontab -
@endforeach
echo "# Done addCrons"
@endtask
@task('cron_remove', ['on' => 'remote'])
su -l {{ $username }}
cd ~/{{ $repo_name }}
echo "# remove crontabs"
@foreach ($crons as $cron_name => $cron_job)
croncmd="{{$cron_job[1]}}"
( crontab -l | grep -v -F "$cron_name" ) | crontab -
( crontab -l | grep -v -F "$croncmd" ) | crontab -
@endforeach
echo "# Done rmCrons"
@endtask
@task('apachelogswatch', ['on' => 'remote'])
su -l {{ $username }}
cd ~/{{ $repo_name }}
tail -f /var/log/virtualmin/{{$domain}}_error_log
@endtask
@task('laralogswatch', ['on' => 'remote'])
su -l {{ $username }}
cd ~/{{ $repo_name }}
tail -f storage/logs/laravel.log
@endtask
@task('catenv', ['on' => 'local'])
cat .env
@endtask
@task('link_klaravel', ['on' => 'local'])
cd vendor/ksoft/
rm -rf klaravel/
ln -s ~/Developer/Laravel/Laravel-Plugins/klaravel/ ./klaravel
ls -la
cd ..
cd ..
@endtask
@task('klean', ['on' => 'local'])
php artisan clear-compiled
php artisan migrate:refresh
php artisan db:seed
@endtask
@task('dump', ['on' => 'local'])
composer dump-autoload
php artisan config:clear
php artisan view:clear
php artisan cache:clear
php artisan clear-compiled
@endtask
@task('up', ['on' => ['remote']])
su -l {{ $username }}
cd ~/{{ $repo_name }}
php artisan up
@endtask
@task('down', ['on' => ['remote']])
su -l {{ $username }}
cd ~/{{ $repo_name }}
php artisan down
@endtask
@story('migrate')
warning
run_migration_seed
@endstory
@task('warning', ['on' => ['local']])
echo "---------------------------------------------"
echo "---------------------------------------------"
echo "--------------- "ATTENTION" -----------------"
echo "---------------------------------------------"
echo "---------------------------------------------"
echo "------------- Are you sure? ---------------"
echo "---------------------------------------------"
echo "---------------------------------------------"
@endtask
@task('run_migration_seed', ['on' => 'remote', 'confirm' => true])
su -l {{ $username }}
cd ~/{{ $repo_name }}
php artisan migrate --force
@if ($seed)
php artisan db:seed --force
@endif
@endtask
@task('m:r', ['on' => 'remote'])
su -l {{ $username }}
cd ~/{{ $repo_name }}
php artisan migrate:rollback --force
@endtask
@task('rights', ['on' => 'remote', 'confirm' => true])
su -l {{ $username }}
cd ~/{{ $repo_name }}
chmod -R 0777 public/upload app/storage
find . -type d -exec chmod 775 {} \;
find . -type f -exec chmod 664 {} \;
@endtask
@task('reload_php_server', ['on' => 'remote'])
/etc/rc.d/init.d/php-fcgi-{{str_replace('.', '-', $domain)}} restart
@endtask
@task('reload_nginx', ['on' => 'remote'])
systemctl restart nginx
@endtask
@task('optimizeInstallation', ['on' => 'remote'])
echo 'start optimizeInstallation'
su -l {{ $username }}
cd ~/{{ $repo_name }}
php artisan clear-compiled
php artisan optimize
@endtask
@task('backupDatabase', ['on' => 'remote'])
echo 'start backupDatabase'
su -l {{ $username }}
cd ~/{{ $repo_name }}
php artisan backup:run
@endtask
@kikoseijo
Copy link
Author

kikoseijo commented Nov 18, 2017

Laravel envoy deployment for virtualmin servers

Install Envoy, in Envoy.blade.php add the task you need form above and configure with your settings.
To run commands just envoy run {task_name} from the list. Enjoy and share it!

Initial use case

# Prepare server ssh and generate ssh keys
> envoy run ssh
# Download repository and install application on server
> envoy run setup #(envoy run download + envoy run install)
# Link public_html to Laravel public directory
> envoy run publish

Daily basis deployment

> envoy run deploy

Available tasks

Common Description
deploy Production deploy command
ssh Setup ssh keys on server
setup Download and setup environments
publish Links public_html to Laravel public
cron_add Adds crons from $crons if does not exists
cron_remove Remove all crons from $crons
up php artisan up
down php artisan down
m:r migrate with --force flag
optimizeInstallation optimize Laravel Installation
backupDatabase composer require "spatie/laravel-backup"
xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Server tasks Description
alias Writes alias to .profile
add_env Adds environment variable to .env
apachelogswatch Tails virtualmin server error logs for current server
laralogswatch Tails Laravel logs in storage/logs/laravel.log
catenv Prints all environment variables on server
reload_php_server Reloads php-fcgi process for current server
reload_nginx Reloads nginx server
xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Crontab -e

There are 2 commands that interact directly with the crontab on remote server, you can add jobs to your server and remove them using cron_add and cron_remove tasks.


Thanks to:

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