简体   繁体   中英

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://myuser@github.com/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://myuser@github.com/mycmp/myapp-backend.git master
npm install
service myservice start

======END======
err: fatal: Unable to look up myuser@github.com (port 9418) (Name or service not known)
err: bash: line 3: npm: command not found
==============================================

Screenshot: 在此处输入图像描述

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

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://myuser@github.com/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.

npm: command not found

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"

you can test "sudo npm -v"

https://blog.csdn.net/weixin_40599109/article/details/110825357

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM