-
-
Save BenSampo/aa5f72584df79f679a7e603ace517c14 to your computer and use it in GitHub Desktop.
# Change to the project directory | |
cd $FORGE_SITE_PATH | |
# Turn on maintenance mode | |
php artisan down || true | |
# Pull the latest changes from the git repository | |
# git reset --hard | |
# git clean -df | |
git pull origin $FORGE_SITE_BRANCH | |
# Install/update composer dependecies | |
composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev | |
# Restart FPM | |
( flock -w 10 9 || exit 1 | |
echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock | |
# Run database migrations | |
php artisan migrate --force | |
# Clear caches | |
php artisan cache:clear | |
# Clear expired password reset tokens | |
php artisan auth:clear-resets | |
# Clear and cache routes | |
php artisan route:cache | |
# Clear and cache config | |
php artisan config:cache | |
# Clear and cache views | |
php artisan view:cache | |
# Install node modules | |
# npm ci | |
# Build assets using Laravel Mix | |
# npm run production --silent | |
# Turn off maintenance mode | |
php artisan up |
@ninety99nine Glad I could help with this issue.. but there is still something not feeling right to your approach. So you are deploying the application from within the application? What if something goes wrong in the script.. That will leave your application down, and no way for you to recover from that..(without terminal-ing into your server). I think you need to find a way to trigger the deployment outside of your application. Maybe some kind of stand-alone webhook you can call that does not rely on the laravel installation..
UPDATE:
Also, I'm not sure how that PHP constant will behave on multi-version PHP installations.. Guess that will conflict at some point.
@npostman, Thank you for that. I understand what you mean and will look more into this. For now, I was running everything within the same application just to understand the basic concepts of simplifying the deployment process, but I do see that this will need to be an external process that is not coupled with the Laravel Application. Thanks for the PHP constant update, that makes sense.
@npostman thanks for sharing the hash_resources.txt approach - works great!
@ninety99nine I am also using same concept but I have a one master application, using that I am deploying other applications on same server. I used Stackoverflow solution for ssh key. Everything is configured properly but now I am getting permission issue for other project directories white trying to git pull
For security purpose I don't want to give permissions to www-data
Did you get such type of permission issues or any suggestion for this error?
Thank you.
Hi @npostman, are you able to assist @accubrain regarding his permission issue?
@ninety99nine @npostman It works if I run following commands:
sudo chown -R $USER:www-data /var/www/example.com
sudo chmod -R 775 /var/www/example.com/storage
Is this safe?
Thank you @npostman for the heads up, yes this was the case. The deploy.sh script could not find the PHP installation. I ran this code locally on my MacBook Pro M1 BigSur (macOS Monterey) and the "php" command was not found though i had installed php.
I had to provide the full path to the php installation. You can find the path by running the "which php" command as @npostman suggested. Here's the path I needed to provide:
After reading responses to a StackOverflow Question, I learned that instead of hard-coding this value, the path could be retrieved dynamically using a PHP constant called PHP_BINARY e.g
In my case, I wanted to implement the one-click deployment functionality similar to Forge to simplify the deployment process. Unfortunately, where i work, I cannot use Forge, and have to implement the convenience of the one-click deployment myself.
Just in case someone out there needs to see how I got this to work for me, you can follow the code down below:
When I click the "Deploy" button on the web application, the application runs a POST Request to a DeploymentController.php that starts the deployment process. Here is a sample of the code inside the DeploymentController.php file
As for the deploy.sh script that is being executed, I have modified it as follows:
Code Above Explained
We are using The Process Component to run the deploy.sh script from the DeploymentController.php file.
The first parameter, which is the "sh" command is used to specify that the file being loaded is a "Shell Script". This helps the Process() method to know how to process the script, since this could be any other kind of file e.g "PHP", "HTML", "PYTHON", e.t.c.
The second parameter, which is the $scriptPath is the fully qualified path to the deployment script that we want to run
The third parameter, which is The base_path() method is used to specify the exact working directory to execute the script commands. In this case the base_path() returns the fully qualified path to the application's root directory so execute the commands from there.
The run() command will run the deploy.sh script and pass the array as variables to be accessed from the deploy.sh script. This allows us to set the configurations here and then give them to the script for processing.
The deploy.sh script references the variables that have been provided via the run() method. This includes the PHP_FPM, PHP_PATH and BRANCH variables
This deployment script requires that we provide the fully qualified php path (PHP_PATH) which can be found by running the "which php" command. I preferred using a PHP Constant called PHP_BINARY which was suggested on the following stackoverflow issue:
Reference #1: Stackoverflow
Reference #2: Php Manual
Conclusion
I'm no expert in this area and I'm sure that they could be improvements, but hope that this helps someone out there if you get stuck like I was.