Remi
Remi

Reputation: 5367

Azure pipeline only publish npm package if the version of this npm package is newer than present in npm registry

In the quick start guide of Azure DevOps Services for npm it states in the last Step 6: Publish an npm package the following:

"If you have npmjs.com configured as an upstream and the package name/version exists in the public registry then you will be blocked from publication"

In other words, once a build using an Azure pipeline starts, and you'd like it to build a package, it will only build a package once a package version is used which doesn't exist.

However, trying to do so will result in a warning resulting in an orange/yellow build status.

Is it possible to check whether or not a package version is updated so that the build only tries to build a package if the package version actually contains a new version? Or is there another method that is recommended here?

Upvotes: 2

Views: 3064

Answers (2)

k2snowman69
k2snowman69

Reputation: 1929

There are three steps here:

  1. Get the local package version (new version if it's been bumped)
  2. Get the published package version
  3. Publish only if the new version and the publish version do not match
  - script: |
      PackageName=$(npm list --json --depth=0 | sed -n 2p | cut -d '"' -f4)
      NewPackageVersion=$(npm list --json --depth=0 | sed -n 3p | cut -d '"' -f4)
      PublishedPackageVersion=$(npm show $PackageName version)
      echo "##vso[task.setvariable variable=NewPackageVersion;]$NewPackageVersion"
      echo "##vso[task.setvariable variable=PublishedPackageVersion;]$PublishedPackageVersion"
    displayName: "Extract package versions"

  - task: Npm@1
    inputs:
      command: publish
    displayName: "Publish"
    condition: |
      and(
        ne(variables['NewPackageVersion'], variables['PublishedPackageVersion']), 
        succeeded(), 
        eq(variables['Build.SourceBranch'], 'refs/heads/master')
      )

Upvotes: 7

Andy Li-MSFT
Andy Li-MSFT

Reputation: 30372

The behavior is expected as it doesn't support overriding packages that exist on the public registry.

Is it possible to check whether or not a package version is updated so that the build only tries to build a package if the package version actually contains a new version?

Theoretically speaking it should be possible, you can try writing a script to check that first, if the specific version not existing in public registry then build and publish, otherwise stop the build. However if there are large amount of data to be compared, then it will consume long time for the building... even encounter timeout issues...

Upvotes: 0

Related Questions