Jese Leos

Bash Scripts to Deploy Applications

$cripts
Bash Scripts For Remote Deployment
Bash Scripts For Remote Deployment

Workflow

  • Pick the branch to run in production server ( master )
  • Create GitHub Token

Get Git

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

Create Application Folder

  • mkdir app-backend && mkdir app-frontend

Clone The Repo

  • Clone Private Repo
     git clone https://{your-token}@github.com/{github-username}/{repo-name}.git
  • Install packages
    npm install
  • Get PM2 Running
PORT=#### NUXT_PUBLIC_API_BASE_URL="{api-url}" pm2 start node --name {app-name} -- .output/server/index.mjs

Backend Strapi Deploy Script

#!/bin/bash

ls
sleep 2
$pwd
sleep 2
ssh -i "$secretKeyPem" $user@$ip << EOF
pwd
sleep 2
cd $path
echo -- git fetch 
git status
git fetch
echo ---- git pull branch-name
git pull origin $branch
echo ---- Strapi running npm ci
npm ci
echo ----- Strapi running npm run build >> NODE_ENV=production npm run build
NODE_ENV=production npm run build
echo --- pm2 reload all
pm2 reload all
wait
echo "--- done from EOF ---"
EOF
echo ----------- done from local ------------

After Locally testing the development branch, commit changes and push changes to master, then run the script locally, which will go to your production server, update the code with the latest updates, then re-install and re-build the app, then trigger PM2 to restart the application without any downtime if all goes well.


Built With

Nuxt.js

© 2019-2023 Khalid Zamer