使用 rsync 指令部署前端專案

前言

由於遠端伺服器只有 1G 的 RAM,如果在遠端伺服器進行編譯,資源很快就沒了,因此先在本機端先進行編譯,再將靜態檔案同步至遠端伺服器。

做法

將公開金鑰同步到遠端伺服器。

1
ssh-copy-id -i ~/.ssh/id_rsa.pub root@xxx.xxx.xxx.xxx

新增 deploy.sh 腳本。

1
2
3
4
#!/bin/bash

npm run build
rsync -avr --delete-after dist/* root@xxx.xxx.xxx.xxx:/var/www/my-project

執行同步。

1
sh deploy.sh