作者每次部署更新都需要进行下列操作:
于是作者写了一个脚本,可以自动执行ssh以及登陆后的固定操作:
#!/bin/bash
ssh -i "webapp.pem" ubuntu@example.com << EOF
cd /var/www/webapp/
echo "Switching to www-data user"
sudo -Hu www-data bash
echo "Pulling Latest Changes"
git pull
echo "Activating Virtual Environment"
source venv/bin/activate
echo "Installing any new libraries"
pip install -r requirements.txt
echo "Migrating Database"
python manage.py migrate
echo "Returning back to Ubuntu user"
exit
echo "Restarting Supervisor and Nginx"
sudo service supervisor restart
sudo service nginx restart
echo "Deployment Finished"
EOF
上述代码的关键就是在两个EOF之间,你可以ssh登陆后写自己需要的操作。
《How to Automate Manual Steps after SSH》2019-03,amitness
原文:https://www.cnblogs.com/CheeseZH/p/12943721.html