Sync, Build & Deploy at One Action
Streamlining your deployment process is crucial for efficient coding. It has compounding effects on productivity. It multiplies your focus and reduces the potential for errors. This guide shows you how to sync your code, build, and deploy to a remote server with a single action, eliminating repetitive passphrase entries.
1. Prepare Your SSH Agent
Start by setting up your SSH agent to handle authentication:
eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa
You'll enter your passphrase once here, saving time on subsequent commands.
2. Create Your One-Action Deployment Script
Craft a script (deploy.sh
) that handles everything in one go:
#!/bin/bash
# Ensure SSH agent is running and key is added
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
# Sync code to remote
rsync -ravz --include '.env' . root@12.345.67.89:/path/to/your/app/
# Stop the existing application
ssh root@12.345.67.89 'pm2 delete app_name'
# Start the updated application
ssh root@12.345.67.89 'cd /path/to/your/app && pm2 start my_app.py --name "app_name" --interpreter python3'
# Save the PM2 process list
ssh root@12.345.67.89 'pm2 save'
# Optionally, kill the SSH agent when done
ssh-agent -k
echo "Deployment completed successfully!"
3. Make Your Script Executable
Ensure you can run your script:
chmod +x deploy.sh
4. Deploy with One Action
Now, whenever you need to update your remote application, simply run:
./deploy.sh
This single command will sync your code, restart your application, and handle all SSH interactions without additional prompts.
Efficiency Tip:
Consider creating an alias for even quicker deployment:
echo 'alias deploy="~/path/to/deploy.sh"' >> ~/.bashrc source ~/.bashrc
Now you can simply typedeploy
in your terminal to run the entire process!
By consolidating your deployment process into a single action, you'll save time and reduce the potential for errors, allowing you to focus more on coding and less on repetitive deployment tasks.