npm version in Azure DevOps pipeline - npm

I have a build pipeline in Azure DevOps which releases artifact via npm pack for later publishing into Artifacts Feed.
I would like to set my major and minor version via GIT but patch version to tie with build number. E.g.
1.2.20201212.4
where 1 and 2 is major.minor version manually updated via GIT
and 20201212.4 is a patch and revision number set by build pipeline
Could someone help me to figure out the required npm version command parameters to preserve minor.major version from source code and update only patch and revision part from $(Build.BuildNumber) variable?

In Azure Devops, you could use the Replace Tokens task from the Replace Tokens Extension.
Then you could add the task before the NPM pack task to replace the variable in Package.json -> Version field.
Here are the steps:
Package.Json file:
{
"name": "helloword",
"version": "0.0.#{SetNumber}#",
"description": "Hello World",
"main": "server.js",
...
}
Build Pipeline:
Set Variables in Build Pipeline.
Add the Replace Tokens task.
Note: Based on my test, npm package cannot support xx.x.x.x version format(npm publish) in azure devops. It can support the x.x.x-x.
So you can set buildnumber like this:$(Date:yyyyMMdd)-$(Rev:r).
Result:
Update:
You could try to use the Npm Version command.
npm version 0.0.$(build.buildnumber) --no-git-tag-version
Update2:
You could try to use the following powershell script to get the version field in the Package.json. Then update the patch number.
$filePath = "$(Build.sourcesdirectory)\package.json" #filepath
$Jsonfile= Get-Content $filePath | ConvertFrom-Json
$version = $Jsonfile.version
echo $version
$major,$minor,$build = $version.Split('.')
$build = "$(build.buildnumber)"
$bumpedVersion = $major,$minor,$build -join(".")
echo $bumpedVersion
Write-Host "##vso[task.setvariable variable=version]$bumpedVersion"
In Npm version, you could run the following command:
version $(version) --no-git-tag-version

Related

Pass Azure Devops $(Build.BuildNumber) to npm task

I want to update the package.json version through azure devops pipeline.
This is the task I used. But it throws error.
- task: Npm#1
displayName: "npm version"
command: "custom"
workingDir: src
verbose: false
customCommand: "version $(Build.BuildNumber)"
How to correctly pass the pipeline build number to npm task.
Thanks in advance.
We can use command npm version <newversion> to update the package.json version. for example, current version is v1.0.1, using command npm version 2.0.0 will update its version to v2.0.0. Please note that the version format must be the same, like major.minor.patch.
In addition, yaml pipeline will set the build numer to be formatted like 20210426.5 by default. If your package.json is not formatted like this, this command npm version $(Build.BuildNumber) will cause issues. You could use UpdateBuildNumber to override the automatically generated build number.
BTW, the following yaml pipeline is for your reference supposed that your package.json is formatted like major.minor.patch. Also you could use custom variables to specify the version number.
pool:
vmImage: ubuntu-latest
steps:
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
# Write your PowerShell commands here.
Write-Host "Hello World"
git config --global user.email "you#example.com"
git config --global user.name "Your Name"
echo "##vso[build.updatebuildnumber]2.1.0"
- task: Npm#1
inputs:
command: 'custom'
workingDir: ''
customCommand: 'version $(Build.BuildNumber)'

Error: "migrate dev is not a prisma command." when calling `npx prisma migrate dev --preview-feature`

When I run the following command:
npx prisma migrate dev --preview-feature
I get the following error:
$ npx prisma migrate dev --preview-feature
npx: installed 600 in 26.334s
▸ migrate dev is not a prisma command.
▸ Perhaps you meant generate
▸ Run prisma help for a list of available commands.
Get in touch if you need help: https://slack.prisma.io
To get more detailed output, run $ set -x DEBUG "*"
(node:6096) [DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated
(Use `node --trace-deprecation ...` to show where the warning was created)
However, the prisma migrate dev command should be available in the Prisma CLI. Why doesn't this work?
Okay I figured it out, I accidentally invoked the Prisma 1 CLI which is available as the prisma package on npm.
The way to fix this is to make sure that the Prisma 2 CLI which is available as the #prisma/cli package on npm is installed locally (see docs):
npm install #prisma/cli --save-dev
# or
yarn add #prisma/cli --dev
That way npx will execute the right binary from the local node_modules folder.
From the npx docs:
Executes <command> either from a local node_modules/.bin, or from a central cache, installing any packages needed in order for <command> to run.
By default, npx will check whether <command> exists in $PATH, or in the local project binaries, and execute that. If <command> is not found, it will be installed prior to execution.
Unless a --package option is specified, npx will try to guess the name of the binary to invoke depending on the specifier provided. All package specifiers understood by npm may be used with npx, including git specifiers, remote tarballs, local directories, or scoped packages.
If for some reason the local binary is not picked up, you can try to add the prisma script to your package.json:
{
"scripts": {
"prisma": "prisma"
}
}

You cannot publish over the previously published versions

I've updated my package using npm version minor to go from 0.4.0 to 0.5.0, and both package.json and package-lock.json reflect this. However when I run the npm publish command it says:
You cannot publish over the previously published versions: 0.4.0
Is there another place I need to update the semver in order to publish?
This helped me:
Open Command Prompt and do the following steps.
npm version <new_Version_No>
npm publish
In your package.json, there might exist a publish script command with content of npm publish ..., remove or rename the publish command in your scripts of package.json if there is one.
Take the following code for example, this scripts.publish command will again be triggered by npm publish --access public, running recursively.
"scripts": {
"publish": "npm publish --access public" // this was being triggered by running `npm publish`
},
take a look at your package.json.
Is the version actually set to 0.5.0?
If not consider setting it manually there. NPM is telling you that you already have a version 0.4.0 and it cannot publish it again. So it seems to think that it's still on 0.4.0.
npm version [patch|minor|major|<version_no>] should be done to bump up the version and then
npm publish for public visibility add --access public
This should do it.
It happens when there is already npm module with same version.
We need to increment the npm module version and publish it again.
For some reason I was getting this error when I was trying to increase my version from 0.0.0 to 0.0.1. However, updating from 0.0.0 to 0.1.0 worked just fine.
You can try the following:
Update npm version to latest.
cd into the parent folder (of the folder containing package.json i.e. cd into A if your files are A/B/package.json) and then run the command npm publish B.
Doing both fixed the issue for me.

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?
There are three steps here:
Get the local package version (new version if it's been bumped)
Get the published package version
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')
)
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...

How to create tgz file with version using npm pack?

I'm trying to create npm package (tgz) without publishing however it's not clear how to specify the version of the module.
When running npm pack it creates filename with version 0.0.0, does anyone has example to share?
npm pack reads the version from your package.json file. You can set the version with the npm version command:
npm version 1.2.3
If you don't yet have a package.json file, create it with npm init.
above answer is valid,
just adding some code and a small tip:
{
"name": "my-cool-package",
"version": "1.0.0",
(...rest of you package.json details)
}
npm pack command in any terminal will give you my-cool-package-1.0.0.tgz.
This will have all contents from you package.
Extra info:
In case you want to ignore any file eg node_modules add those into .npmignore in the same folder and they will be ignored.