Only npm publish when version changes in tfs build
Asked Answered
E

2

6

I have created a private npm package which is published to Azure Artifacts. To publish my npm package I have a npm publish step in my tfs build definition. I am using tfs version 16.131.28507.4.

This all works as intented. However, if a build is triggered where the npm package version is unchanged the npm publish will fail with a 403 forbidden. This error causes the rest of my build to fail. The npm package is part of a larger project, so it will not be uncommon for builds to occur where the npm package is unchanged.

Is it possible to prevent the tfs build step which performs the npm publish from running if the current version within my packages feed is the same as the version specified within the package json?

I aware of the "Custom Conditions" within the build step, but am unsure how to link the Azure Artifacts current published version with the version in my package.json. I am also aware of the "Continue on Error" option within the build definition, but this creates a partially succeeded build which I would like to avoid in this case.

Eirena answered 11/11, 2019 at 15:45 Comment(0)
B
1

If a build is triggered where the npm package version is unchanged the npm publish will fail with a 403 forbidden.

That's an expected behavior. Since your package name/version not changed. A package with that name was already published before, so you'd need to use a different name in your package.json file and then npm publish again.

In your case, the simplest solution one is updating your minimum version of npm package even nothing changed. Another as you have pointed out, using "Continue on Error" option within the build definition, but this creates a partially succeeded build.

We do not have any build-in settings/configurations with Azure Artifacts or npm task could judge if the current version within your packages feed is the same as the version specified within the package json.

And for customer conditions, we also do not provide such an expression that controls when this task should run. For this, you could take a look at this official link--Conditions.

Another workaround should be separating your build pipeline, one for build something, npm publish package. One for build something, refer your npm package in azure package feed. If you want each time npm version changed auto trigger second build. You could use Trigger Build Task to chain the build.

Bodoni answered 12/11, 2019 at 3:7 Comment(1)
Thank you for your answer, I think a seperate build pipeline makes the most sense for my situation.Eirena
S
2

This is the solution that we currently use in Azure Pipelines when we publish our package to Azure Artifacts.

First, compare the currently building package version with all published versions, and set variable for other tasks to use:

- bash:
        PACKAGE_VERSION="$(node -p -e "require('./package.json').version")"
        PACKAGE_NAME="$(node -p -e "require('./package.json').name")"

        FOUND_VERSION=$(npm view $PACKAGE_NAME versions | grep \'$PACKAGE_VERSION\')
        
        if $FOUND_VERSION
        then
            IS_NEW_VERSION=true
        fi

        echo "##vso[task.setvariable variable=IS_NEW_VERSION]$IS_NEW_VERSION"

Note that we use npm view <package-name> versions, not just version. This is to make sure that we get all published versions, not just the latest one.

Then do what you need to do in order to publish a new version, build it etc.

Then, publish only if $IS_NEW_VERSION is true:

- bash: |
        npm publish
  displayName: 'Publish NPM package'
  condition: and(not(failed()), eq(variables.IS_NEW_VERSION, 'true'))

Perhaps somewhat brute-force and the bash scripts can certainly be slimmed down or improved but they do the job.

Seaman answered 2/12, 2021 at 3:58 Comment(0)
B
1

If a build is triggered where the npm package version is unchanged the npm publish will fail with a 403 forbidden.

That's an expected behavior. Since your package name/version not changed. A package with that name was already published before, so you'd need to use a different name in your package.json file and then npm publish again.

In your case, the simplest solution one is updating your minimum version of npm package even nothing changed. Another as you have pointed out, using "Continue on Error" option within the build definition, but this creates a partially succeeded build.

We do not have any build-in settings/configurations with Azure Artifacts or npm task could judge if the current version within your packages feed is the same as the version specified within the package json.

And for customer conditions, we also do not provide such an expression that controls when this task should run. For this, you could take a look at this official link--Conditions.

Another workaround should be separating your build pipeline, one for build something, npm publish package. One for build something, refer your npm package in azure package feed. If you want each time npm version changed auto trigger second build. You could use Trigger Build Task to chain the build.

Bodoni answered 12/11, 2019 at 3:7 Comment(1)
Thank you for your answer, I think a seperate build pipeline makes the most sense for my situation.Eirena

© 2022 - 2024 — McMap. All rights reserved.