2

I'm setting up a build pipeline in azure devops. There's an assistant task called YarnInstaller@3 that allows me to install a specific version of Yarn. However, the only available versions listed are 1.x, with the latest being 1.22.

There are migration instructions at https://yarnpkg.com/getting-started/install for migrating from v1 to v2+, which I've done locally. Since only yarn@1.x is available, it seems like I'll have to migrate every time I run a build, e.g.

  • Install yarn 1
  • Set the version to berry
  • yarn set version latest
  • Then run yarn commands as usual

This seems unnecessary, especially since I've already committed the .yarnrc.yml and the .yarn sub-directories. Am I wrong about that? Is there another way to install yarn@2+ in my pipeline?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
rakitin
  • 1,943
  • 6
  • 25
  • 51

2 Answers2

7

You can use the following command lines in your pipeline to update the version of Yarn to 2.x.

yarn set version berry
yarn set version 2.x

For more details, you can see:

Below is the sample pipeline I tested on my side, and it can work as expected. You can reference it and add the related steps in your build pipeline.

  • azure-pipelines.yml
  steps:
  . . .

  - task: Bash@3
    displayName: 'Yarn version before update'
    inputs:
      targetType: inline
      script: |
        echo "Yarn version before update:"
        yarn --version
  
  - task: Bash@3
    displayName: 'Update Yarn version to 2.x'
    inputs:
      targetType: inline
      script: |
        yarn set version berry
        yarn set version 2.x

  - task: Bash@3
    displayName: 'Yarn version after update'
    inputs:
      targetType: inline
      script: |
        echo "Yarn version after update:"
        yarn --version
  • Result. enter image description here

    enter image description here

Bright Ran-MSFT
  • 5,190
  • 1
  • 5
  • 12
3

The best way to do this is using Node Corepack, as Yarn docs suggests. This enables you to run modern versions of Yarn (2+) without the need of having Yarn Classic (1) installed.

First, add packageManager to your package.json, for example:

  "packageManager": "yarn@3.6.0"

(this should work both for all modern versions of Yarn: 2, 3, 4, and possibly future versions)

Once you do this, you'll no longer need to keep Yarn binary in your repo. You can safely remove yarnPath from yarnrc.yml and you can remove the contents of .yarn/releases folder.

Then (assuming you have Node 14+ installed), you may need to enable Corepack by running:

corepack enable

If everything was successful, running

yarn -v

in your project should now return correct version number.

Note that the first time you use a binary specified in packageManager in package.json Node Corepack will transparently download it in the background, so you need internet access and the first command may take a couple of seconds to complete. After that, you'll be able to use the binary without internet access and without any delays.

Now that you've verified your setup works correctly on your local machine, it's time to do the same on Azure Pipelines. Here's the full setup, from nothing to your first Yarn-powered command:

# …

stages:
  - stage: runTests
    displayName: 'Run tests'
    jobs:
      - job: runLint
        displayName: 'ESLint'
        steps:
            # Cache .yarn/cache for faster installs
          - task: Cache@2
            inputs:
              key: 'yarn | $(Agent.OS) | yarn.lock'
              restoreKeys: |
                yarn | $(Agent.OS)
                yarn
              path: .yarn/cache
            displayName: Cache .yarn/cache

            # Install Node.js - all versions from version 14 ship with Corepack
          - task: NodeTool@0
            inputs:
              versionSpec: '18.x'
            displayName: 'Install Node.js'

            # Enable Corepack
          - script: corepack enable
            displayName: 'Enable Corepack'

            # Install dependencies
          - script: yarn --immutable
            displayName: 'Install dependencies'
Wojciech Maj
  • 982
  • 6
  • 21