我试图在Ubuntu上为Azure DevOps设置一个私有构建代理。我需要在构建中使用npm任务。
我尝试使用nvm安装最新的节点,安装成功:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
nvm install 11.10.1我可以检查node -v和npm -v。但是,当npm任务从管道执行时,它将失败
无法找到可执行文件:“npm”。请验证文件路径是否存在,或者在path环境变量指定的目录中找到该文件。还检查文件模式以验证文件是可执行的。
在我的路径中,我看到了/usr/local/nvm/versions/node/v11.10.1/bin和ls -l显示:
lrwxrwxrwx 1 500 500 38 2月28日06:00 /usr/local/nvm/version/node/v11.10.1/bin/npm./lib/node_ -> /npm/bin/npm-cli.js
我还添加了777 (只是为了尝试一下!)对于npm-cli.js,仍然没有运气。
我还发现了类似的问题-- https://github.com/Microsoft/azure-pipelines-agent/issues/1862
如何在Azure DevOps的Ubuntu代理上正确安装节点和nvm?
发布于 2019-03-06 23:03:48
作为临时解决方案,我使用
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
sudo apt-get install nodejs而不是nvm,它工作得很好。
发布于 2020-10-22 06:08:31
只是nvm
我根据来自nvm.sh的指令创建这样的模板,并相应地设置NVM_DIR和路径。
steps:
- bash: |
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.36.0/install.sh | bash
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm install
nvm use
echo "##vso[task.setvariable variable=NVM_DIR;]$NVM_DIR"
echo "##vso[task.setvariable variable=PATH;]$PATH"
displayName: "Install Node.js"我还添加了一个.nvmrc,如nvm.sh中所记录的那样,以便指定要使用的节点的版本。
然后在我的管道中使用它,如下所示
steps:
- template: templates/install-node-js.yml
- bash: |
node --version具有缓存和节点依赖关系
这是我的模板,用于安装节点,并对package-lock.json中的包进行缓存。
steps:
- task: Cache@2
inputs:
key: 'nvm | "$(Agent.OS)" | .nvmrc'
path: $(Pipeline.Workspace)/.nvm
displayName: "Cache NVM"
- task: Cache@2
inputs:
key: 'npm | "$(Agent.OS)" | package-lock.json'
path: $(Build.SourcesDirectory)/node_modules
displayName: "Cache node dependencies"
- bash: |
set -e
if [ $(System.Debug) ]
then
set -x
fi
if [ ! -s $NVM_DIR/nvm.sh ]
then
mkdir -p $NVM_DIR
curl -so- https://raw.githubusercontent.com/nvm-sh/nvm/v0.36.0/install.sh | bash
fi
. $NVM_DIR/nvm.sh || true
nvm install
nvm use
npm ci
echo "##vso[task.setvariable variable=PATH;]$PATH"
env:
NVM_DIR: $(Pipeline.Workspace)/.nvm
displayName: Install Node and dependencies发布于 2021-12-22 16:00:13
Azure DevOps已经在他们的Linux和macOS构建代理中包含了nvm。
如果您想利用他们所做的工作来创建您自己的代理,那么这个回购程序就拥有他们使用的所有代码。
https://github.com/actions/virtual-environments/
Ubuntu代理正在使用Packer (https://packer.io)来提供VM。
https://github.com/actions/virtual-environments/tree/main/images/linux
我们使用Azure托管的代理和类似的东西来设置Node项目(或响应Native/Cordova/Ionic移动构建),最重要的部分是任何时候您想使用nvm或它安装的包(项目或全局范围),您需要使用. ${NVM_DIR}/nvm.sh。
steps:
- script: |
# Setup node using nvm
# NODE_VERSION=12 # or whatever your preferred version is
# We are using .nvmrc instead of specific version
npm config delete prefix # avoid a warning
. ${NVM_DIR}/nvm.sh # This is the most important bit to utilize nvm
nvm install # uses .nvmrc implicitly
nvm use
nvm alias default $(cat .nvmrc)
# nvm alias can understand eg. lts/erbium better than
# nvm_version_path which can't resolve the lts/* codenames
VERSION_PATH="$(nvm_version_path $(cat $(nvm_alias_path)/$(cat $(nvm_alias_path)/default)))"
which nvm
# This will inject the node version we told nvm was default to the $PATH
echo "##vso[task.prependPath]$VERSION_PATH"
# Optionally upgrade npm here or using `nvm install-latest-npm`
#npm install -g npm # upgrades to latest that supports lockfileVersion@2
npm installhttps://stackoverflow.com/questions/55015866
复制相似问题