Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gregtroche/f1ac625d864dc61d9b7d5c6dad497ae9 to your computer and use it in GitHub Desktop.
Save gregtroche/f1ac625d864dc61d9b7d5c6dad497ae9 to your computer and use it in GitHub Desktop.
GitHub Action deploy nodejs to DigitalOcean Droplet

Github deployment

Requirements:

  • DigitalOcean Droplet (Ubuntu 20.04+) should be created
  • Github repository

Prepare DO Droplet Server:

  • ssh root@DROPLET_IP

  • sudo vi /etc/ssh/sshd_config

  • change PasswordAuthentication from no to yes

  • sudo systemctl restart ssh

  • adduser deployer

  • usermod -aG sudo deployer

  • exit

  • ssh deployer@DROPLET_IP

  • ssh-keygen

  • ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts

  • cat ~/.ssh/id_rsa.pub and copy the output

  • go to https://github.com/settings/ssh/new and paste your id_rsa.pub value

  • cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

  • cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys2

  • chmod 700 ~/.ssh/authorized_keys && chmod 640 ~/.ssh/authorized_keys2

  • go to and create a new secrets in repository settings page: https://github.com/USERNAME/USERNAME/settings/secrets/actions/new:

  • secret name: HOST, secret value: YOUR_DROPLET_IP

  • secret name: USERNAME, secret value: deployer

  • copy value from cat ~/.ssh/id_rsa and paste it as a new secret name: KEY, secret value: PASTE_YOUR_VALUE_HERE

Github repository

Create a new file in your repository .github/workflows/on-main-push-deploy-do.yml and add following:

name: Deploy to DigitalOcean Droplet
'on':
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Deploy to DigitalOcean Droplet
        uses: appleboy/ssh-action@master
        with:
          host: ${{secrets.HOST}}
          username: ${{secrets.USERNAME}}
          key: ${{secrets.KEY}}
          script: |
            export NVM_DIR=~/.nvm
            source ~/.nvm/nvm.sh
            rm -rf test
            mkdir test
            cd test
            git clone git@github.com:username/projectname
            echo 'Deployment to digital ocean finished'

Commit and push to the remote repository and go check out the ACTIONS tab for the jobs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment