17

I am trying to deploy a nodejs app from github to a remote ubuntu server via ssh. Here is my main.yml:

name: Node Github CI

on:
  push:
    branches:
      - master

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
    - name: Node Js
      uses: actions/setup-node@v1
       
    - name: SSH and deploy node app
      uses: appleboy/ssh-action@master        
      with:
        host: ${{ secrets.SERVER_IP }}
        username: ${{ secrets.USERNAME }}
        key: ${{ secrets.SERVER_KEY }}
        script: |
          service myservice stop
          cd leancrm-backend
          git pull git://[email protected]/mycmp/myapp-backend.git master
          npm install
          service myservice start

When I run this, I get this error:

======CMD======
service myservice stop
cd myapp-backend
git pull git://[email protected]/mycmp/myapp-backend.git master
npm install
service myservice start

======END======
err: fatal: Unable to look up [email protected] (port 9418) (Name or service not known)
err: bash: line 3: npm: command not found
==============================================

Screenshot: enter image description here

4 Answers 4

35

Since you are connected to your server I assume you already have the repo there, so you only need to execute git pull.

Also you should add these lines at the beginning of the script:

export NVM_DIR=~/.nvm
source ~/.nvm/nvm.sh

My yml file looks like these at the end:

script: |
  git pull
  export NVM_DIR=~/.nvm
  source ~/.nvm/nvm.sh                
  npm install
  npm run start_server
Sign up to request clarification or add additional context in comments.

3 Comments

This did the trick. I updated to nvm as well and npm didn't work as I removed it from usr local bin folder
I was able to run NPM when I logged in to the server via SSH, but Github kept failing. This did the trick to resolve it. Thanks!
This should be the accepted answer.
12

Reason:

I use nvm for the server node environment, and nvm will not install the node environment in the /usr/local/bin/ directory, so that sudo can't find the corresponding instructions, and finally create a soft connection to solve

sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/node" "/usr/local/bin/node"

sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/npm" "/usr/local/bin/npm" 

sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/pm2" "/usr/local/bin/pm2"

sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/yarn" "/usr/local/bin/yarn"

you can test "sudo npm -v"

4 Comments

I had an issue and this solved it. I couldn't execute any commands like npm/pm2 via github actions. By linking them it now works unlike other suggestions.
Thank you for the answer your answer solved my problem
I'm using Volta and that solved my problem, thanks.
Your answer solved the problem, thanks!!
3

Your first step

name: Node Js
      uses: actions/setup-node@v1

sets up Node.js on the GitHub build runner. Your second step however...

 name: SSH and deploy node app
      uses: appleboy/ssh-action@master        
      with:
        host: ${{ secrets.SERVER_IP }}
        username: ${{ secrets.USERNAME }}
        key: ${{ secrets.SERVER_KEY }}
        script: |
          service myservice stop
          cd leancrm-backend
          git pull git://[email protected]/mycmp/myapp-backend.git master
          npm install
          service myservice start

... SSHs to your server and then runs script instructions there. You're also attempting to check out your source code repo there.

What you probably wanna do is check out your repo on the GitHub build runner...

- name: Checkout repo
  uses: actions/checkout@v2

.. then run npm install there, then scp the output to your server, and finally ssh to that machine and restart your service.

Comments

-1

Check if you did install npm on your remote ubuntu.
npm also needs to be installed on the remote server for the deployment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.