我家里有一台 Linux 服务器,其中有一个 GitHub Actions 运行器,代码如下:
name: Build and Deploy UI - DEV
on:
workflow_dispatch:
push:
branches: [ "dev" ]
paths:
- 'UI/**'
pull_request:
branches: [ "dev" ]
paths:
- 'UI/**'
jobs:
build_and_deploy:
if: github.ref == 'refs/heads/dev'
runs-on: self-hosted
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install Dependencies
run: |
nvm list
node -v
npm -v
它给了我这个错误
/github/actions-runner/project/_work/_temp/dc7e7354-9aa9-4676-8b50-883e4f150b34.sh:
line 2: nvm: command not found Error: Process completed with exit code 127.
我尝试使用以 root 和非 root 身份安装的
nvm
和 node
(运行 GitHub Actions 的用户),但错误消息中没有任何变化。
GitHub Actions 可以使用服务器中安装的 Node.js 吗?我不想每次提交代码时都设置 Node.js 和 nvm 以节省部署时间。
问题是已安装的 Node 和 NVM 在新终端中不可用,默认情况下,非 root 用户的 shell (/bin/sh) 是可用的。您可以获取 NVM 来解决此问题。
就我而言,为了解决这个问题,我执行了以下操作。
将非 root 用户的默认 shell 更改为 bash (/bin/bash)
sudo chsh -s /bin/bash non-root
添加了.profile(实际上是从/root路径复制的代码)
if [ "$BASH" ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi
mesg n 2> /dev/null || true
添加了 .bashrc 文件
export HISTCONTROL=ignoreboth:erasedups
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completi>
在动作运行器中添加了 BASH_ENV:~/.profile
name: Build and Deploy UI - Prod
on:
workflow_dispatch:
push:
branches: [ "main" ]
paths:
- 'UI/**'
pull_request:
branches: [ "main" ]
paths:
- 'UI/**'
env:
BASH_ENV: ~/.profile
jobs:
build_and_deploy:
if: github.ref == 'refs/heads/main'
runs-on: self-hosted
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install Dependencies
run: |
cd ./path-to-project-root
npm ci