Github actions: Deploy Node.js on VPS
- Clone repo to VPS
git clone https://github.com/<user>/<repo>.git
If you have problems with cloning a repository, check out Clone Github Repo Using Personal Access Token
2. Setup workflows, create a file in <repo>/.github/workflows/cd.yml
name: Node.js CDon:
push:
branches: [ master ]jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Deploy to VPS using ssh
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
passphrase: ${{ secrets.PASSPHRASE }}
port: ${{ secrets.PORT }}
script: |
cd /var/www/test
git pull origin master
npm install --only=prod
npm run build --if-present
This workflow triggers when master branch is updated (push or merge).
3. In your VPS, generate ssh-key for github
ssh-keygen -t rsa -b 4096 -C "email@gmail.com"
Add public key to authorized_keys
#Copy to authorized keyscat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
Copy private key, will be needed for setting up Github Secrets.
cat ~/.ssh/id_rsa
For Ubuntu 20, having issue with ssh, refer below:
Error:ssh: handshake failed: ssh: unable to authenticate, attempted methods [none], no supported methods remain
Documentation: https://github.com/appleboy/scp-action#setting-up-a-ssh-key
Resolve Issue: https://github.com/appleboy/ssh-action/issues/16#issuecomment-626372814
4. Setup Github Secret keys, in your repository,
Go to Settings > Secrets > Actions > New repository secret
Create secret one at a time
Name: HOST
Value: <Your VPS Address. e.g. 45.123.23.21>Name: USERNAME
Value: <Username when accessing the VPS. e.g. root>Name: PRIVATE_KEY
Value: <Setup ssh keygen>Name: PASSPHRASE
Value: <Passphrase for the ssh-key>Name: PORT
Value: <Usually its just 22>