Last active
July 3, 2023 17:24
-
-
Save victor-falcon/2b00263d53fc9d129b9056bf865ce072 to your computer and use it in GitHub Desktop.
Laravel Forge - Zero Downtime Deployment for NPM projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DEPLOY_FOLDER_DATE=$(date '+%Y%m%d%H%M%S') | |
WEBSITE_FOLDER="app.metricswave.com" | |
GIT_REPO="git@github.com:MetricsWave/app.git" | |
METRICS_WAVE_EVENT="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX" | |
echo 'Create new deploy folder…' | |
echo "Folder: /home/forge/${WEBSITE_FOLDER}_deployments/${DEPLOY_FOLDER_DATE}" | |
mkdir -p /home/forge/app.metricswave.com_deployments/$DEPLOY_FOLDER_DATE | |
echo 'Bulding app in deployment folder…' | |
cd /home/forge/$WEBSITE_FOLDER_deployments/$DEPLOY_FOLDER_DATE | |
git clone $GIT_REPO . | |
git pull origin $FORGE_SITE_BRANCH | |
echo "NPM install and build…" | |
npm install | |
npm run build | |
echo 'Replacing current app with deployment folder…' | |
rm -rf /home/forge/$WEBSITE_FOLDER | |
ln -s /home/forge/$WEBSITE_FOLDER_deployments/$DEPLOY_FOLDER_DATE /home/forge/$WEBSITE_FOLDER | |
cd /home/forge/$WEBSITE_FOLDER | |
( flock -w 10 9 || exit 1 | |
echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock | |
# Remove previous deploys | |
cd /home/forge/$WEBSITE_FOLDER_deployments | |
find . -maxdepth 1 -mindepth 1 -type d -printf "%T+ %f\0" | sort -z | head -z -n -1 | cut -z -d' ' -f 2- | xargs -0 rm -rf & | |
# Notify deployment | |
cd /home/forge/$WEBSITE_FOLDER | |
BODY='{"version":"'$(git log --pretty=format:'%h' -n 1)'", "message":"'$(git log --pretty=format:'%s' -n 1)'","author":"'$(git log --pretty=format:'%an' -n 1)'", "service":"Application"}' | |
curl -X POST https://metricswave.com/webhooks/$METRICS_WAVE_EVENT \ | |
-H "Content-Type: application/json" \ | |
-H "Accept: application/json" \ | |
-d "$BODY" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment