【问题标题】:Azure DevOps private agent - how to install node using nvmAzure DevOps 私有代理 - 如何使用 nvm 安装节点
【发布时间】:2019-03-06 04:59:42
【问题描述】:

我正在尝试在 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 -vnpm -v。但是当 npm 任务从管道执行时 - 它失败了

无法找到可执行文件:“npm”。请验证文件路径是否存在,或者该文件是否可以在 PATH 环境变量指定的目录中找到。还要检查文件模式以验证文件是否可执行。

在我的 PATH 中,我有 /usr/local/nvm/versions/node/v11.10.1/binls -l 显示:

lrwxrwxrwx 1 500 500 38 Feb 28 06:00 /usr/local/nvm/versions/node/v11.10.1/bin/npm -> ../lib/node_modules/npm/bin/npm-cli.js

我还为 npm-cli.js 添加了 777(只是为了尝试一下!),但仍然没有运气。

我也发现了这个类似的问题 - https://github.com/Microsoft/azure-pipelines-agent/issues/1862

如何在适用于 Azure DevOps 的 Ubuntu 代理上使用 nvm 正确安装 node 和 npm?

【问题讨论】:

  • 安装npm后有没有重启代理?
  • @4c74356b41 是
  • 作为单独的说明 - 代理也不会自动标记为具有 npm 功能...

标签: ubuntu azure-devops nvm


【解决方案1】:

作为一个临时解决方案,我安装了节点

curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
sudo apt-get install nodejs

而不是 nvm,它工作正常。

【讨论】:

  • 感谢您在此处分享您的解决方案,您可以接受它作为答案,这样它可以帮助遇到相同问题的其他社区成员,我们可以存档此线程,谢谢。
  • @LeoLiu-MSFT 好的,已接受。我以前没有做过,因为我在这里等待一些大师的更好的解决方案:)
【解决方案2】:

为了让它工作,我最终创建了一个代理到 nvm 的 bash 脚本,然后告诉代理将 PATH 变量更新为当前的 NVM。

/usr/local/bin/nvm 创建一个包含以下内容的文件:

#!/usr/bin/env bash
# Store the current path so we can check to see if it changes
OLD_PATH=$PATH

# Normal NVM stuff
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

# Execute NVM and pass all the arguments
nvm $@

# Check to see if the path has changed, and if an azure agent is running the
# command by checking the existence of the $AGENT_VERSION variable.
if [[ "$OLD_PATH" != "$PATH" && ! -z ${AGENT_VERSION+x} ]]; then
    # Grab the current node executable Something like:
    #  /Users/digitaldev/.nvm/versions/node/v10.16.3/bin
    CURRENT_NODE=$(nvm which current)
    # Resolve to directory of the node executable Something like
    # /Users/digitaldev/.nvm/versions/node/v10.16.3
    BIN_DIR=$(dirname "$CURRENT_NODE")
    # Tell Azure Pipeline to update the PATH [1]
    echo "##vso[task.prependpath]$BIN_DIR"
fi

使文件可执行:

chmod +x /usr/local/bin/nvm

现在,当 Azure Pipeline 运行 nvm 时,它会更新路径以查找节点的 nvm 版本。

[1] Logging Commands PrependPath

【讨论】:

    【解决方案3】:

    只是非虚拟机

    我根据nvm.sh 的说明创建了一个这样的模板,并相应地设置了 NVM_DIR 和 PATH。

    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
    

    【讨论】:

    • 我会在缓存部分工作后添加它。这就是我公开 NVM_DIR 变量的原因。
    【解决方案4】:

    Azure DevOps 在其 Linux 和 macOS 构建代理中包含 nvm 已有一段时间了。

    如果你想利用他们所做的工作来创建你自己的代理,这个 repo 有他们使用的所有代码。

    https://github.com/actions/virtual-environments/

    Ubuntu Linux 代理正在使用 Packer (https://packer.io) 来配置 VM。

    https://github.com/actions/virtual-environments/tree/main/images/linux

    我们使用 Azure 托管代理和类似的东西来设置 Node 项目(或 React 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 install
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 2019-08-31
      相关资源
      最近更新 更多