CircleCI忘记了机器执行程序上的节点版本

时间:2019-03-06 16:15:39

标签: node.js circleci nvm

我使用10.15.1将节点版本设置为nvm,在接下来的run步骤中,它将返回到6.1.0。我已经尝试过多种变体,包括:https://www.cloudesire.com/how-to-upgrade-node-on-circleci-machine-executor/

我缺少明显的东西吗?我只需要每个run步骤来记住我在第一个步骤中设置的节点版本,因此在这种情况下它们都将使用10.15.1

这是我的工作流程中的工作:

dev:
  environment:
    BASH_ENV: run/env/test/.env
  machine:
    image: circleci/classic:latest
  steps:
  - checkout
  - run:
      name: Install node@10
      command: |
        set +e

        curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
        export NVM_DIR="/opt/circleci/.nvm"
        [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

        nvm install 10
        nvm alias default 10

        rm -rf ~./node-gyp

        node -v # prints 10.15.1 as expected

  - run:
      name: Install yarn and rsync
      command: |
        node -v # prints 6.1.0

        export NVM_DIR="/opt/circleci/.nvm"
        [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

        node -v # prints 6.1.0

        curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
        echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
        sudo apt-get update && sudo apt-get install yarn rsync

  - run:
      name: Install node modules
      command: |
        node -v # prints 6.1.0

        yarn install # this is what is failing because of the unexpected node version

  - run:
      name: Deploy to Dev Server
      command: |
        if [ "${CIRCLE_BRANCH}" == "master" ]; then rsync -arhvz --exclude .git/ -e "ssh -o StrictHostKeyChecking=no" --progress \
        ./ ubuntu@xxx.xxx.xxx.xxx:/var/www/xxx/xxx/; fi
        if [ "${CIRCLE_BRANCH}" == "master" ]; then ssh -o StrictHostKeyChecking=no ubuntu@xxx.xxx.xxx.xxx 'cd /var/www/xxx/xxx && pm2 restart all --update-env'; fi

4 个答案:

答案 0 :(得分:1)

您在这里做的太多了。您需要做的就是运行nvm install v10nvm执行器中已经安装了machine

答案 1 :(得分:1)

@FelicianoTech是不正确的,每个构建步骤都在全新的环境中运行,并且忘记了您的nvm设置。您必须在每个构建步骤中设置NVM_DIR并提供nvm设置脚本。烦人,我知道。

答案 2 :(得分:1)

尽管FelicianoTech是正确的,但在计算机执行程序中您已预安装了nvm,它无法记住下一个运行命令的实际状态。为了使nvm记住其状态,您必须将其存储在$BASH_ENV中。经过长时间的搜索,我终于找到了一个answer in the CircleCI Disussion Board

machine: 
      image: circleci/classic:latest
steps:
      - checkout
      - run: 
          command: |
            echo 'export NVM_DIR="/opt/circleci/.nvm"' >> $BASH_ENV
            echo ' [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> $BASH_ENV
      - run: nvm install 10
      - run: node -v
      - run: npm -v
      - run: npm i -g npm@6.9.0
      - run: npm -v
      - run: nvm alias default 10
      - run: nvm use 10
      - run: node -v
      - run: npm -v

然后,直到那时,我也取得了成功,即使最后运行的命令- run: node -v仍能正确记住节点版本。

CircleCI上的输出

enter image description here

(在讨论板上的phil-lgr信用)

答案 3 :(得分:0)

通过将已安装的节点添加到$ PATH来解决

示例:

    - run:
        name: 'Install Project Node'
        command: |
          set +x
          source ~/.bashrc

          nvm install 12
          NODE_DIR=$(dirname $(which node))
          echo "export PATH=$NODE_DIR:\$PATH" >> $BASH_ENV