Skip to content

Instantly share code, notes, and snippets.

@Manzadey
Last active December 29, 2022 21:35
Show Gist options
  • Save Manzadey/1dca0eba682d3db624e8252e39059d20 to your computer and use it in GitHub Desktop.
Save Manzadey/1dca0eba682d3db624e8252e39059d20 to your computer and use it in GitHub Desktop.
Deploy bash script for laravel project

Deploy bash script for laravel project

  1. Add deploy.sh and releases_clear.php to project folder
  2. Set variables in deploy.sh: repo, branch, path_user, path_composer, path_main
  3. Set variables in releases_clear.php: $path_releases
  4. Run bash script in folder project for deploy: bash deploy.sh
  5. Run php script for clear releases folders: php releases_clear.php
#!/bin/bash
echo "START DEPLOY!!!"
# repository
repo=git@github.com:username/repository.git
branch=main
start=$(date +%s)
step=1
# current Date
today=`date +%Y%m%d_%H%M`
# composer path
path_composer=~/bin/composer
# main path
path_main=~/www/test
# path current
path_current=$path_main/current
# path releases
path_releases=$path_main/releases
# path with new release
path_new_release=$path_releases/$today
# path storage
path_storage=$path_current/storage
# path .env
path_env=$path_main/.env
# check releases directory
[ ! -d "$path_releases" ] && mkdir -R $path_releases
# create release folder
mkdir -p $path_new_release
# copy project from github
git clone --depth 1 -b $branch $repo $path_new_release
echo "#$((++step)) - Repository '$repo' has been cloned | $(($(date +%s) - $start)) sec."
# move to path with new release
cd $path_new_release
# check .env file, if not exists - create .env
[ ! -f "$path_env" ] && cp ".env.example" "$path_env"
# create symlink for .env file
ln -nfs $path_env .env
$path_composer install --no-interaction --prefer-dist
echo "#$((++step)) - Composer dependencies have been installed | $(($(date +%s) - $start)) sec."
cd $path_new_release
php artisan config:clear
php artisan view:clear
php artisan migrate --force
php artisan clear-compiled
php artisan optimize
php artisan storage:link
echo "#$((++step)) - Production dependencies have been installed | $(($(date +%s) - $start)) sec."
if [[ -d "$path_current/storage" ]]; then
echo "#$((++step)) - Start copy 'storage' directory from '$path_current' to '$path_new_release'"
start_copy=$(date +%s);
cp -RPT "$path_current/storage" "$path_new_release/storage"
echo "#$((++step)) - End copy 'storage' directory | $(($(date +%s) - $start_copy)) sec."
fi
ln -nfs $path_new_release $path_current
echo "#$((++step)) - Deploy finished! | $(($(date +%s) - $start)) sec."
<?php
$days = 7;
$deleteAll = false;
foreach($argv as $i => $arg) {
if(str_ends_with($arg, '.php')) {
continue;
}
$key = $value = '';
if(strpos($arg, '=') !== false) {
[$key, $value] = explode('=', $arg);
} else {
$key = $arg;
}
if($key === '--days') {
$days = (int) $value;
continue;
}
if($key === '--all') {
$deleteAll = true;
continue;
}
}
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object !== "." && $object !== "..") {
if (is_dir($dir. DIRECTORY_SEPARATOR .$object) && !is_link($dir."/".$object))
rrmdir($dir. DIRECTORY_SEPARATOR .$object);
else
unlink($dir. DIRECTORY_SEPARATOR .$object);
}
}
rmdir($dir);
}
}
$path_home = $_SERVER['HOME'];
$path_releases = "$path_home/www/test/releases";
$now = new DateTime;
if(!is_dir($path_releases)) {
throw new Exception("Folder '$path_releases' not exists!");
}
$i = 1;
foreach(scandir($path_releases) as $dir) {
if($dir === '.' || $dir === '..') {
continue;
}
$dirPath = "$path_releases/$dir";
$dirDate = DateTime::createFromFormat('U', filectime($dirPath));
$interval = $dirDate->diff($now);
if($deleteAll || intval($interval->format('%R%a')) > $days) {
rrmdir($dirPath);
echo "$i. Release deleted: '$dir'";
echo PHP_EOL;
$i++;
}
}
if($i > 1) {
echo PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment