3

So I'm trying to make conditions in my .gitlab-ci.yml if there is no package then, npm publish to pack the library in the GitLab registry I gave my pipeline the permission to the registry and the npm access token, but I still get the unauthorized error this is the part of the .gitlab-ci.yml where I create the .npmrc file and set the configurations.

script:
    - |
      if [[ ! -f .npmrc ]]; then
        echo 'No .nmprc found! Creating one now.'
        echo "@${CI_PROJECT_ROOT_NAMESPACE}:registry=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/">.npmrc
        echo "//${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}">>.npmrc
        echo "//registry.npmjs.org/:_authToken=${NPM_ACCESS_TOKEN}">>.npmrc
        echo "Created the following .npmrc:"; cat .npmrc
      fi

The pipeline get me this when I try to find if there is a package with the name of : $NPM_PACKAGE_NAME

@scope:registry = "https://gitlab.example.com/api/v4/projects/project_id/packages/npm/" 
//gitlab.example.com/api/v4/projects/projet_id/packages/npm/:_authToken = (protected) 
//registry.npmjs.org/:_authToken = (protected) 
; "cli" config from command line options
long = true
$ npm config set always-auth true
$ echo $(npm view "${NPM_PACKAGE_NAME}" )
npm ERR! code E401
npm ERR! 401 Unauthorized - GET https://gitlab.example.com/api/v4/projects/project_id/packages/npm/@scope%2fmy-package

Where :

- NPM_PACKAGE_NAME=$(node -p "require('./my-package/package.json').name")
Haru
  • 83
  • 9

2 Answers2

1

Instead of echo to the .npmrc, you could try the npm commands directly

npm config set -- '//gitlab.example.com/api/v4/projects/<your_project_id>/packages/npm/:_authToken' "${NPM_TOKEN}"
npm config set -- '//gitlab.example.com/api/v4/packages/npm/:_authToken' "${NPM_TOKEN}"

That way, you are sure the .npmrc is properly updated.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Actually that's what I've tried first, and .npmrc is what I found on the internet, and I also tried both which gave me the message: `overridden` when I add the command `npm config ls -l` to my gitlab-ci file, that's why I came here to ask for help. Thanks for the suggestion! – Haru Jul 11 '22 at 23:28
0

Could you try to create the .npmrc in your home folder instead of locally?

We do this in our pipeline and it works without any problem:

publish:
    stage: publish
    script:
        - echo "@<scope>:registry=https://${CI_SERVER_HOST}/api/v4/projects/${REGISTRY_PROJECT_ID}/packages/npm/" > ~/.npmrc
        - echo "//${CI_SERVER_HOST}/api/v4/projects/${REGISTRY_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}" >> ~/.npmrc
        - npm version --no-git-tag-version "$(<.next-version)" --allow-same-version
        - npm publish --tag ${NPM_TAG_NAME}

As you see, other than the npm version and npm publish commands, the only difference is the .npmrc file location.

zeb
  • 1,105
  • 2
  • 9
  • 32
  • Thanks for help, I tried another approach, using git tags, so I managed to control when the publish can happen. – Haru Jul 13 '22 at 19:41