iT邦幫忙

0

Github action bash: npm: command not found when on ssh

前言

我想用GitHub action去做CI/CD,主要做的事情很簡單,只有gut push和npm install,但卻不盡人意,在action讀取到執行npm install的時候,會出現npm command not found,我想原因是出在我的VPS設定上面,我們就一起來看下面解Bug的過程吧。

環境:Ubuntu 18.04.4 LTS (GNU/Linux 5.3.0-1026-gcp x86_64)

參考:

https://github.com/animetosho/Nyuu/issues/14

  • step1. 查詢環就變數which npm就會拿到npm的絕對路
$ which npm

which指令会在环境变量$PATH设置的目录里查找符合条件的文件。所以echo $PATH也可以從~/.bashrc環境變數中拿到一樣的絕對路徑

  • step2. 新錯誤訊息
/usr/bin/env: ‘node’: No such file or directory

原因是因為在OS上Debian呼叫"nodejs"替代"node",而Ubuntu相反,所以我們在VPS下指令做軟連結讓nodejs=node

  • step3
$ ln -s /usr/bin/nodejs /usr/bin/node

得到同樣錯誤訊息:

/usr/bin/env: ‘node’: No such file or directory
  • step4. 在VPS上重新安裝node
$ sudo apt-get install nodejs-legacy

跑出錯誤訊息:

Reading package lists... Done
Building dependency tree
Reading state information... Done
Package nodejs-legacy is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
However the following packages replace it:
  nodejs

E: Package 'nodejs-legacy' has no installation candidate

提示我們把nodejs-legacy改成nodejs

  • step4. 我們重新下指令
sudo apt-get install nodejs

完成

以下是我們修改好的yml檔案:

name: CI

on: [push]

jobs:
  build:

    runs-on: self-hosted
    strategy:
      matrix:
        node-version: [10.15.3]
    steps:
    - uses: actions/checkout@v2
    - name: decrypt
      run: |
        gpg --quiet --batch --yes --decrypt --passphrase="$PASSPHRASE" \
        --output $HOME/secrets/key google_compute_engine.gpg
      env:
        PASSPHRASE: ${{ secrets.PASSPHRASE }}
    - name: chmod
      run: chmod 600 $HOME/secrets/key
    - name: ssh
      run: ssh -o StrictHostKeyChecking=no -i $HOME/secrets/key zen_master@35.187.152.88 "cd postmark-side-project/;git pull;sudo /home/zen_master/.nvm/versions/node/v10.15.3/bin/npm install"

尚未有邦友留言

立即登入留言