Skip to content

Instantly share code, notes, and snippets.

@danieltorscho
Last active June 21, 2024 10:04
Show Gist options
  • Save danieltorscho/c104c23e97c840f09fd9a186ec092b28 to your computer and use it in GitHub Desktop.
Save danieltorscho/c104c23e97c840f09fd9a186ec092b28 to your computer and use it in GitHub Desktop.
GitHub Actions 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/REPOSITORY/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.

@ersiny
Copy link

ersiny commented Mar 16, 2023

YOUR_DROPLET_ID -> YOUR_DROPLET_IP?

@danieltorscho
Copy link
Author

YOUR_DROPLET_ID -> YOUR_DROPLET_IP?

Yes, it's Droplet IP. I've updated the gist

@gregtroche
Copy link

This was mostly perfect for me. I had a few small issues:

Thanks for the gist!

@hkwarui
Copy link

hkwarui commented Jun 15, 2023

This really helped.

@allister-grange
Copy link

allister-grange commented Aug 6, 2023

https://github.com/USERNAME/USERNAME/settings/secrets/actions/new: should be https://github.com/USERNAME/REPOSITORY/settings/secrets/actions/new

Love the gist by the way, very helpful

@behzadmoradi
Copy link

I did not add the public key to https://github.com/settings/ssh/new and it works just fine

@clarkdowding
Copy link

Thank. Helped me find a typo in my deploy script.

@BrahyanPro
Copy link

Thanks. Crack

@hanancs
Copy link

hanancs commented Jun 14, 2024

Thanks

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