microsoft / azure-pipelines-tasks

Tasks for Azure Pipelines
https://aka.ms/tfbuild
MIT License
3.49k stars 2.61k forks source link

Add engines & nvmrc support to UseNode #16991

Open jamiehaywood opened 2 years ago

jamiehaywood commented 2 years ago

Note

Issues in this repo are for tracking bugs, feature requests and questions for the tasks in this repo

For a list:
https://github.com/Microsoft/azure-pipelines-tasks/tree/master/Tasks

If you have an issue or request for the Azure Pipelines service, use developer community instead:

https://developercommunity.visualstudio.com/spaces/21/index.html )

Required Information

Entering this information will route you directly to the right team and expedite traction.

Question, Bug, or Feature?
Type: Feature

Enter Task Name: UseNode

list here (V# not needed):
https://github.com/microsoft/azure-pipelines-tasks/tree/master/Tasks/UseNodeV1

Issue Description

It would be great to add optional inputs to UseNode that would allow for engines & nvmrc detection, i.e:

- task: UseNode@1
  inputs:
    detectVersionFromEngines: true
    detectVersionFromNvmrc: true

Currently we do some convoluted detection by doing the following:

- bash: |
    if test -f ".nvmrc"; then
      NODE_VERSION=$(cat .nvmrc)
      echo "##vso[task.setvariable variable=NODE_VERSION]$NODE_VERSION"
    fi  
  displayName: 'Set node version from .nvmrc (if exists)'

- bash: |
    if jq -e -r '.engines | has("node")' package.json > /dev/null; then
      ENGINES_NODE_VERSION=$(jq -e -r '.engines.node' package.json)
      echo "##vso[task.setvariable variable=ENGINES_NODE_VERSION]$ENGINES_NODE_VERSION"
    fi  
  displayName: 'Set node version from engines property (if exists)'

- task: UseNode@1
  displayName: Use Node.js
  inputs:
    version: ${{ coalesce(parameters.nodeVersion, '$(ENGINES_NODE_VERSION)', '$(NVMRC_NODE_VERSION)') }}
jamiehaywood commented 1 year ago

@DenisRumyantsev is there any movement on this issue?

DenisRumyantsev commented 1 year ago

@jamiehaywood thank you for suggesting this feature, we will check if it is possible to implement it

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 180 days with no activity. Remove the stale label or comment on the issue otherwise this will be closed in 5 days

jamiehaywood commented 1 year ago

bump - not stale

Jogai commented 1 year ago

You have my vote

github-actions[bot] commented 9 months ago

This issue is stale because it has been open for 180 days with no activity. Remove the stale label or comment on the issue otherwise this will be closed in 5 days

jamiehaywood commented 9 months ago

bump - not stale

SchulteMarkus commented 5 months ago

(Not so) fun fact: NodeTool@0 (which is probably the predecessor of UseNode@1) supports using .nvmrc

Varorbc commented 3 months ago

@DenisRumyantsev is there any movement on this issue?

austinjones commented 2 months ago

bump. stuck on NodeTool@0 with no upgrade path.

JoepKockelkorn commented 1 month ago

I had issues installing Node on custom Windows agents using NodeTool@0 so I had to switch to UseNode@1 and read the version from the .nvmrc file myself.

Normally I would have written a bash or powershell script as that is 10x easier, but unfortunately they were not installed on the agents. Therefore I had to write a batch script:

    - script: |
        @echo off
        setlocal enabledelayedexpansion
        for /f "delims=v" %%i in ('type .nvmrc') do (
            set NODE_VERSION=%%i
        )
        echo ##vso[task.setvariable variable=nodeVersion]!NODE_VERSION!
        endlocal
      displayName: "Read Node version from .nvmrc"

    - task: UseNode@1
      displayName: 'Install Node'
      inputs:
        version: $(nodeVersion)

For clarity: this uses the double percent notation (%%i) because under the hood, the script is written to a batch file and then executed on the agent. The syntax of a for loop is different between batch files and direct cmd input.

SchulteMarkus commented 1 month ago

We only use UseNode@1 with the major Node.js version, like this:

    - task: UseNode@1
      displayName: 'Install Node'
      inputs:
        version: 20

This at least means that a Node.js pre-installed on the build agent is always used, so the installation in the pipeline is stable. However, you have to take care manually that the Node.js of the build agents and the one in the project do not diverge.

https://stackoverflow.com/questions/75583337/cache-nodejs-tool-in-devops/78875309#78875309