Skip to content

Instantly share code, notes, and snippets.

@shivanshtalwar0
Created September 8, 2023 11:29
Show Gist options
  • Save shivanshtalwar0/bfbb37a8d04aee8cfb12eade80344bd4 to your computer and use it in GitHub Desktop.
Save shivanshtalwar0/bfbb37a8d04aee8cfb12eade80344bd4 to your computer and use it in GitHub Desktop.
Deploy Nuxt3 application to VPS using github actions

Step 1

Create a directory .github/workflows in root of. your project Create main.yml file in that directory with following content

name: Deploy

# Trigger the workflow on push and
# pull request events on the master branch
on:
  push:
    branches: ["main"]
  pull_request:
    branches: ["main"]

# Authenticate to the the server via ssh
# and run our deployment script
jobs:
  run_pull:
    name: run pull
    runs-on: ubuntu-latest

    steps:
      - name: install ssh keys
        # check this thread to understand why its needed:
        # https://stackoverflow.com/a/70447517
        run: |
          install -m 600 -D /dev/null ~/.ssh/id_rsa
          echo "${{ secrets.PRIVATE_KEY }}" > ~/.ssh/id_rsa
          ssh-keyscan -H ${{ secrets.HOST }} > ~/.ssh/known_hosts
      
      - name: Deploy using ssh
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          key: ${{ secrets.PRIVATE_KEY }}
          port: 22
          script: |
            export NVM_DIR=~/.nvm # needed to reinitialize nvm 
            source ~/.nvm/nvm.sh
            nvm i 18 # intialze node version to use
            cd /root/januscaler-website
            git pull origin main
            npm install --yes
            npm run build
            npm i -g pm2 # make sure it is installed
            pm2 delete website
            pm2 start npm --name "website" -- start
      - name: cleanup
        run: rm -rf ~/.ssh

On the VPS

Setup ssh key

ssh-keygen -f key_path -t ed25519 -C "[email protected]"

copy public key to ~/.ssh/authorized_keys now go to github actions secrets of repository create secrets below

HOST= ip of VPS (108.83.33.44)
USERNAME= username of VPS (root)
PRIVATE_KEY= value of private key generated in previous step (ie copy ~/.ssh/id_ed119 for example)

Create sshconfig

Create sshconfig nano ~/.ssh/config and paste content below

Host github.com
    User shivanshtalwar0
    IdentityFile ~/.ssh/githubKey

change its permission chmod 600 ~/.ssh/config

finaly push the new commit on the project with any commit message so you will be able to see github actions task running and if everything went well your app will be deployed

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