bash ssh scp in script - ghdrako/doc_snipets GitHub Wiki

 #!/bin/bash
 echo "Starting deployment process..."
 # Define deployment server and directory
 DEPLOY_SERVER="[email protected]"
 DEPLOY_DIR="/var/www/myapp"
 # Archive the application files
 tar -czf myapp.tar.gz app.js package.json node_modules
 # Transfer files to the deployment server
 scp myapp.tar.gz $DEPLOY_SERVER:/tmp
 # Check if scp was successful
 if [ $? -ne 0 ](/ghdrako/doc_snipets/wiki/-$?--ne-0-); then
   echo "Deployment failed: File transfer error."
   exit 1
 fi
 # Connect to the server and deploy
 ssh $DEPLOY_SERVER << 'ENDSSH'
 tar -xzf /tmp/myapp.tar.gz -C $DEPLOY_DIR
 rm /tmp/myapp.tar.gz
 cd $DEPLOY_DIR
 npm install --production
 pm2 restart all
 ENDSSH
 echo "Deployment completed successfully."