Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github actions err: bash: line 3: npm: command not found

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

like image 811
rahulserver Avatar asked Jul 12 '20 15:07

rahulserver


3 Answers

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
like image 187
cbeltrangomez Avatar answered Nov 15 '22 09:11

cbeltrangomez


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"

like image 5
sanglt Avatar answered Nov 15 '22 10:11

sanglt


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.

like image 3
Max Avatar answered Nov 15 '22 09:11

Max