Get npm package latest version from a gitlab registry - npm

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}

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

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 can I set the npm version in gitlab CI?

I have a gitlab pipeline for an Angular project with the image in .gitlab-ci.yml
image: node:16
build:
stage: build
script:
- npm ci
- nodejs -v
- npm -v
- npm run build:prod
- npm doctor
When the pipeline reaches the npm doctor it fails with the following error on npm version:
$ npm doctor
Check Value Recommendation/Notes
npm ping ok
npm -v not ok Use npm v7.24.0
node -v ok current: v16.9.1, recommended: v16.9.1
npm config get registry ok using default registry (https://registry.npmjs.org/)
which git ok /usr/bin/git
Perms check on cached files ok
Perms check on local node_modules ok
Perms check on global node_modules ok
Perms check on local bin folder ok
Perms check on global bin folder ok
Verify cache contents ok verified 1361 tarballs
How can I solve this?
In my machine I have the npm version v7.24.0 and the command succeeds.
Install npm before you do anything else, specifying a version, i.e. npm install npm#version -g.
For completeness, if you can't update the global npm, you can do so locally, by replacing npm with npx npm#latest or npx npm#7.24.0. npx will install npm if it needs to, locally, and then run the local installation.
Lastly you can install locally with npm install npm#latest and then run it with $(npm bin)/npm, but this is what npx is for.
Note that I don't understand why you're running npm doctor after a build. Presumably if you care about npm thining it's set up properly you should run it before the build, so the pipeline fails early.
P.S. I stupidly read 'github' as 'gitlab'. If you were using github I would recommend using
setup-node if only for dependency caching, which can seriously speed up pipelines. I don't know if gitlab does anything similar.

Github actions: NPM publish 404 not found

In my github project Im trying to automatically create a new version and publish it to NPM whenever something is pushed to the master branch.
The idea
Create a new minor version
Publish the package to NPM
Im using github actions. My workflow file looks like this:
# This workflow will run tests using node and then publish a package to the npm registry when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
name: Node.js Package
on:
#trigger on every commit to the main branch
push:
branches:
- main
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: 12
- run: npm test
publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: git config user.name $GITHUB_ACTOR
- run: git config user.email gh-actions-${GITHUB_ACTOR}#github.com
- run: git remote add gh-origin https://${GITHUB_ACTOR}:${GITHUB_TOKEN}#github.com/${GITHUB_REPOSITORY}.git
- run: echo "//registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN" >> ~/.npmrc
- run: npm version patch
- run: npm publish
- run: git push gh-origin HEAD:master --tags
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
(https://github.com/ether/ep_align/actions/runs/322527776)
I keep getting a 404 error when doing the publish. Which I dont' understand because the package is online here: https://www.npmjs.com/package/ep_align
npm ERR! code E404
npm ERR! 404 Not Found - PUT https://registry.npmjs.org/ep_align - Not found
npm ERR! 404
npm ERR! 404 'ep_align#0.2.7' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
This problem is driving me nuts for a few hours now, and I have no idea what it could be.
Any ideas?
it is just an authentication issue with your token.
Have you ever set the access token in your Repository secrets ? (You might want to create environments as well.)
https://docs.npmjs.com/creating-and-viewing-access-tokens
Or it might be an issue with authorization as well. Please check your token type. It should be automation one.
Here is an example from me >
https://github.com/canberksinangil/canberk-playground
Where I have set my token under the repository settings.
Let me know if this helps :)
The NODE_AUTH_TOKEN token is attached to the wrong step, so the npm publish has no authentication. You need:
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
- run: git push gh-origin HEAD:master --tags
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
Also, make sure the case of the env variable (npm_token) here matches that in the GitHub actions settings. Environment variables are case senstive.

Can command line args with npm eliminate the need for a `.npmrc` file?

My JavaScript project is successfully harnessing a multi stage build process within a Dockerfile. During the "build" stage a .npmrc file is being built on the fly using a secret supplied as a Docker build arg, as illustrated below:
FROM node:12.16-alpine AS build
ARG ACCESS_TOKEN
WORKDIR /app
COPY package*.json ./
RUN echo "//npm.pkg.github.com/:_authToken=$ACCESS_TOKEN" > .npmrc && \
echo "#my_org:registry=https://npm.pkg.github.com" >> .npmrc && \
npm ci --production && \
rm -f .npmrc
# ...remainder of file omitted
I know that I can supply the token at the command line as follows: NPM_TOKEN=$ACCESS_TOKEN npm ci --production. However, this fails as follows:
... earlier docker build output omitted
---> 68c5ba096f60
Step 6/16 : RUN NPM_TOKEN=$ACCESS_TOKEN npm ci --production --registry https://npm.pkg.github.com/
---> Running in 418c51a85c15
npm ERR! code E401
npm ERR! Unable to authenticate, need: Basic realm="GitHub Package Registry"
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-09-09T11_07_17_789Z-debug.log
failed to build: couldn't build "my_org/my-project": unable to stream build output: The command '/bin/sh -c NPM_TOKEN=$ACCESS_TOKEN npm ci --production --registry https://npm.pkg.github.com/' returned a non-zero code: 1
Is what I am attempting possible or must there always be a .npmrc file somewhere that is used for authentication?
Did you try by setting as ENV before the the npm cmd?
ENV NPM_TOKEN=$ACCESS_TOKEN
And in your command line you are passing NPM_TOKEN=$ACCESS_TOKEN, I think it should be ACCESS_TOKEN=${NPM_TOKEN} as the ARG variable in dockerfile is ACCESS_TOKEN
https://docs.npmjs.com/docker-and-private-modules#update-the-dockerfile