Update package.json version using Azure Dev Ops Build pipeline - npm

I am trying to dynamically update a package.json as part of a CI build for publishing to an npm repository.
I am following a simple scheme of yyyymm.dd.rev to create the version number.
I have followed npm version in Azure Dev Ops pipeline.
My powershell script to update variables:
variables:
major: 1
minor: 0
patch: 0
rev: 0
packageVersion: '$(major)$(minor).$(patch).$(rev)'
steps:
- task: PowerShell#2
displayName: Set SemVer values
inputs:
targetType: 'inline'
script: |
$year = Get-Date -Format "yyyy"
$month = Get-Date -Format "MM"
$day = Get-Date -Format "dd"
$rev = Get-Date -Format "HHmm"
Write-Host "##vso[task.setvariable variable=major]$year"
Write-Host "##vso[task.setvariable variable=minor]$month"
Write-Host "##vso[task.setvariable variable=patch]$day"
Write-Host "##vso[task.setvariable variable=rev]$rev"
With the variable set - I am calling the npm version command to update the package.json
- task: Npm#1
displayName: Update Version of my package
inputs:
command: 'custom'
workingDir: 'libs/my-package'
customCommand: 'version $(packageVersion) --no-git-tag-version --loglevel verbose'
I keep getting an npm failure. This runs fine if I execute it locally - just on the build server do I see failure:
/usr/local/bin/npm --version
6.14.15
/usr/local/bin/npm config list
; cli configs
metrics-registry = "https://registry.npmjs.org/"
scope = ""
user-agent = "npm/6.14.15 node/v14.18.1 linux x64"
; environment configs
userconfig = "/home/vsts/work/1/npm/17197.npmrc"
; node bin location = /usr/local/bin/node
; cwd = /home/vsts/work/1/s/libs/my-library
; HOME = /home/vsts
; "npm config ls -l" to show all defaults.
**/usr/local/bin/npm version 202111.04.1901 --no-git-tag-version --loglevel verbose**
npm info it worked if it ends with ok
npm verb cli [
npm verb cli '/usr/local/bin/node',
npm verb cli '/usr/local/bin/npm',
npm verb cli 'version',
npm verb cli '202111.04.1901',
npm verb cli '--no-git-tag-version',
npm verb cli '--loglevel',
npm verb cli 'verbose'
npm verb cli ]
npm info using npm#6.14.15
npm info using node#v14.18.1
npm ERR! npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]
npm ERR! (run in package dir)
npm ERR! 'npm -v' or 'npm --version' to print npm version (6.14.15)
npm ERR! 'npm view <pkg> version' to view a package's published version
npm ERR! 'npm ls' to inspect current package/dependency versions
npm verb exit [ 1, true ]
npm timing npm Completed in 117ms
npm verb code 1
##[warning]Couldn't find a debug log in the cache or working directory
##[error]Error: Npm failed with return code: 1
Finishing: Update Version of my package

I eventually did find an answer to this problem. Azure DevOps allows for Running git commands in a pipeline.
So the solution is twofold:
You need to provide your build permission to access your repository
In the pipeline I added these steps
steps:
- checkout: self
persistCredentials: true
clean: true
- task: Powershell#2
displayName: Checkout Branch and Initialize Defaults
inputs:
targetType: inline
script: |
$branch = "$(Build.SourceBranch)" -replace "refs/heads/"
git checkout $branch
git config --global user.email "ci#somorg.com"
git config --global user.name "Azure CI Build"
- task: Bash#3
displayName: Update the package version
condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
inputs:
workingDirectory: $(projectDirectory)
targetType: inline
script: |
npm version patch -m "update version to %s [skip ci]" --force
- task: Bash#3
displayName: Push Changes
condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
inputs:
targetType: inline
script: |
git add .
git commit -m "CI: update version [skip ci]"
git push

Related

configuring .npmrc to use NPM_TOKEN to publish to public repo

I've created a public npm repository: https://www.npmjs.com/package/point-cloud-visualiser
I'm trying to run npm publish from within codebuild. I have generated a token and saved it in AWS secrets manager. I am succesfully pulling the secret in the buildspec. However I am unsure how to configure the .npmrc file so that it uses the token and doesn't tell me to login.
Currently my .npmrc looks like this:
#point-cloud-visualiser:registry=https://www.npmjs.com/package/point-cloud-visualiser
//point-cloud-visualiser/:_authToken=${NPM_TOKEN}
And my buildspec.yml looks like this:
version: 0.2
env:
secrets-manager:
NPM_TOKEN: "npm-token:npm-token"
phases:
install:
commands:
- npm install
build:
commands:
- npm run build
- npm publish
But when it fails on npm publish giving the error:
npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in to https://registry.npmjs.org/
npm ERR! need auth You need to authorize this machine using `npm adduser`
I have also tried removing the .npmrc file and using the following in the buildspec.yml:
version: 0.2
env:
secrets-manager:
NPM_TOKEN: "npm-token:npm-token"
phases:
install:
commands:
- npm install
build:
commands:
- npm run build
- npm config set registry 'https://www.npmjs.com/package/point-cloud-visualiser'
- npm config set '//npmjs.com/package/point-cloud-visualiser/:always-auth' 'true'
- npm config set '//npmjs.com/package/point-cloud-visualiser/:_authToken' '${NPM_TOKEN}'
- npm publish
But this approach gives the same result as above. What am I doing wrong? Thank you!
The following buildspec worked succesfully:
version: 0.2
env:
secrets-manager:
NPM_TOKEN: "npm-token:npm-token"
phases:
install:
commands:
- npm install
build:
commands:
- npm run build
- echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrc
- echo "//registry.npmjs.org/:always-auth=true" >> ~/.npmrc
- npm publish

Get npm package latest version from a gitlab registry

I'm trying to put a package in a Gitlab registry using npm and .gitlab-ci.yml.
.gitlab-ci.yml
npm-package:
stage: build
image: node:buster
before_script:
- git config --global user.name "${GITLAB_USER_NAME}"
- git config --global user.email "${GITLAB_USER_EMAIL}"
script:
- npm config set #${CI_PROJECT_ROOT_NAMESPACE}:registry=https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/
- npm config set //${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}
- NPM_PACKAGE_NAME=$(node -p "require('.package.json').name")
- NPM_PACKAGE_VERSION=$(node -p "require('./package.json').version")
- echo $(npm view "${NPM_PACKAGE_NAME}" versions)
- echo ${NPM_PACKAGE_NAME}
- echo ${NPM_PACKAGE_VERSION}
- |
if [[ $(npm view "${NPM_PACKAGE_NAME}" versions) != *"'${NPM_PACKAGE_VERSION}'"* ]]; then
npm config set //${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}
npm publish
echo "Successfully published version ${NPM_PACKAGE_VERSION} of ${NPM_PACKAGE_NAME} to GitLab's NPM registry: ${CI_PROJECT_URL}/-/packages"
else
echo "Version ${NPM_PACKAGE_VERSION} of ${NPM_PACKAGE_NAME} has already been published, so no new version has been published."
fi
I tried my first time and the package was saved successfully in the repo. And now I'm trying to run it under the condition: if the version's package has changed then run an npm publish.
But the variable $(npm view "${NPM_PACKAGE_NAME}" versions) seems to be empty, and when I try to echo it I get the error:
npm ERR! code E401
npm ERR! 401 Unauthorized - GET https://gitlab.example.com/api/v4/projects/1/packages/npm/#my_scope/my_package
Any help?
Your CI does not have enough rights.
1- You need to generate an access token (automation type) from your npm registry via the npm UI (or via the command line).
The process with the UI:
https://docs.npmjs.com/creating-and-viewing-access-tokens
2- Assign the token to an environment variable (named NPM_TOKEN in this example) accessible to your CI
3- Then create (or update) a .npmrc file with this line at the top:
//registry.npmjs.org/:_authToken=${NPM_TOKEN}

npm version patch: clean working directory not clean

I hae a problem running npm version patch on Azure Pipelines. Initially, I discovered that SonarCloud scanner drops unwanted files in the repository. I git-ignored the directory
Consider the following fragment
- script: git checkout -f $(Build.SourceBranchName)
displayName: Force checkout of current branch
- script: git status
displayName: Git status
- script: git reset --hard
displayName: Git reset
- task: Npm#1
displayName: Tag repository and set release version
inputs:
command: custom
customCommand: version patch
- script: git push origin $(Build.SourceBranchName) --tags
displayName: Git push with tags
Required in order to push a subsequent commit
Displays a summary of the repository
Unhappy with git-status ok, I force cleaning the repo
Makes a commit and a tag out of the repository
Pushes branch and tag
git-status output
Starting: Git status
==============================================================================
Task : Command line
Description : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version : 2.201.1
Author : Microsoft Corporation
Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
==============================================================================
Generating script.
Script contents:
git status
========================== Starting Command Output ===========================
/usr/bin/bash --noprofile --norc /home/vsts/work/_temp/447a869a-56d7-4a29-a4c0-9a49b5ffce74.sh
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
Finishing: Git status
git-reset output
Starting: Git reset
==============================================================================
Task : Command line
Description : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version : 2.201.1
Author : Microsoft Corporation
Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
==============================================================================
Generating script.
Script contents:
git reset --hard
========================== Starting Command Output ===========================
/usr/bin/bash --noprofile --norc /home/vsts/work/_temp/85fedcd4-9756-4d38-aa3d-d41c03032565.sh
HEAD is now at ad6aebf Pipelines
Finishing: Git reset
npm-version output
Starting: Tag repository and set release version
==============================================================================
Task : npm
Description : Install and publish npm packages, or run an npm command. Supports npmjs.com and authenticated registries like Azure Artifacts.
Version : 1.202.0
Author : Microsoft Corporation
Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/package/npm
==============================================================================
/opt/hostedtoolcache/node/16.15.0/x64/bin/npm --version
8.5.5
/opt/hostedtoolcache/node/16.15.0/x64/bin/npm config list
; "project" config from /home/vsts/work/1/s/.npmrc
message = ":[npm-autoversion]: %s"
; "env" config from environment
cache = "/home/vsts/work/1/.npm"
userconfig = "/home/vsts/work/1/npm/7212.npmrc"
; node bin location = /opt/hostedtoolcache/node/16.15.0/x64/bin/node
; cwd = /home/vsts/work/1/s
; HOME = /home/vsts
; Run `npm config ls -l` to show all defaults.
/opt/hostedtoolcache/node/16.15.0/x64/bin/npm version patch
npm ERR! Git working directory not clean.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/vsts/work/1/.npm/_logs/2022-05-19T13_01_16_005Z-debug-0.log
##[warning]Couldn't find a debug log in the cache or working directory
##[error]Error: Npm failed with return code: 1
Finishing: Tag repository and set release version
Question: how is it possible that npm fails to make a version bump if I am triple-sure that the repository is clean?

How to solve npm error code: 254 in azure DevOps?

When I try to include my cypress tests (via nodejs & .yml) to azure DevOps, I get the following Error:
Npm failed with return code: 254
What does it mean and how can I solve this?
My complete Log is here:
Starting: Npm
==============================================================================
Task : npm
Description : Install and publish npm packages, or run an npm command. Supports npmjs.com and authenticated registries like Azure Artifacts.
Version : 1.174.0
Author : Microsoft Corporation
Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/package/npm
==============================================================================
SYSTEMVSSCONNECTION exists true
SYSTEMVSSCONNECTION exists true
/opt/hostedtoolcache/node/10.22.0/x64/bin/npm --version
6.14.6
/opt/hostedtoolcache/node/10.22.0/x64/bin/npm config list
; cli configs
metrics-registry = "https://registry.npmjs.org/"
scope = ""
user-agent = "npm/6.14.6 node/v10.22.0 linux x64"
; environment configs
userconfig = "/home/vsts/work/1/npm/2560.npmrc"
; node bin location = /opt/hostedtoolcache/node/10.22.0/x64/bin/node
; cwd = /home/vsts/work/1/s
; HOME = /home/vsts
; "npm config ls -l" to show all defaults.
/opt/hostedtoolcache/node/10.22.0/x64/bin/npm run test
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /home/vsts/work/package.json
npm ERR! errno -2
I use the following YAML:
To create this yaml i used a tutorial for Continuous integration of Cypress into azure devops.
# Node.js
# Build a general Node.js project with npm.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/javascript
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool#0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'
- script: |
npm install
displayName: 'npm install'
- task: Npm#1
inputs:
command: 'custom'
customCommand: 'run test'
continueOnError: true
- task: PublishTestResults#2
inputs:
testResultsFormat: 'JUnit'
testResultsFiles: '*.xml'
searchFolder: '$(System.DefaultWorkingDirectory)/cypress/reports/junit'
mergeTestResults: true
testRunTitle: 'Publish Test Results'
According to your error message in your logs:ENOENT: no such file or directory, open ‘/home/vsts/work/package.json’, npm cannot find your package.json file. Please check which folder your package.json is in. For example:
My package.json is in the web/app folder. I need to set this folder as working folder in npm task.
Here is the configuration of my npm task:
- task: Npm#1
inputs:
command: 'custom'
workingDir: '$(Build.SourcesDirectory)/web/app'
customCommand: 'run test'
continueOnError: true

How to configure ESLint Settings in Azure DevOps YAML?

I'm learning how to use YAML files in my build pipeline and I need to change eslint settings. Specifically, I'm trying to disable the rule that checks for console statements. I've modified it in my local .eslintrc.js file, but the pipeline doesn't appear to use the settings in that file. Am I going about this the right way?
Here's what I have so far:
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool#0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'
- script: |
npm install
npm run build
displayName: 'npm install and build'
You have not added any command to run the linter.
Add an additional command like so in the yaml.
- script: |
npm install
eslint src
npm run build
displayName: 'npm install, lint and build'
Or for better clarity, you can add a custom script in the package.json file, as below
"scripts": {
...
"linter": "eslint src",
...
}
then call it from the yaml.
- script: |
npm install
npm run linter
npm run build
displayName: 'npm install, lint and build'