Github Action
-
On Linux server
Run:
cd .ssh ssh-keygen -t ed25519 -a 200 -C "[email protected]" cat id_ed25519.pub >> authorized_keys
Now, copy the content of .ssh/ed25519 file into GitHub Action secret variable. (Repo > Settings > Secret & Variables > Actions > New Repository Secret)
The copied content of .ssh/ed25519 file has to start from:
-----BEGIN PRIVATE KEY----- ...... key ...... -----END PRIVATE KEY-----
Suggestion name : REMOTE_PRIVATE_KEY
Create 2 more keys & value for;
- REMOTE_HOST (example: 172.0.0.1)
- REMOTE_USER (example: root)
So, you will have 3 secrets.
Now, create a folder & YAML file in the repo.
File: .github/workflows/ssh-remote-execution.yml
name: Remote SSH Command on: push: branches: - master jobs: execute-remote-script: runs-on: ubuntu-latest steps: - name: Execute script on remote server uses: appleboy/ssh-action@master with: host: ${{ secrets.REMOTE_HOST }} username: ${{ secrets.REMOTE_USER }} key: ${{ secrets.REMOTE_PRIVATE_KEY }} script: | cd /var/www/app_location/www/source git reset --hard git pull npm i npm run build pm2 restart app_name
Then, commit to branch
master
.