JS-DevTools / npm-publish

GitHub Action to publish to NPM
https://jstools.dev/npm-publish
MIT License
613 stars 74 forks source link

fix: Include registry URL pathname in npmrc auth token config #186

Closed bdr99 closed 5 months ago

bdr99 commented 5 months ago

Thanks for creating this action! I was trying to use it to publish to a private npm registry, but ran into an issue with authentication.

My workflow looked like this:

on:
  push:
    branches: main

jobs:
  publish:
    runs-on: node-20
    steps:
      - uses: actions/checkout@v4
      - run: npm ci
      - uses: JS-DevTools/npm-publish@v3
        with:
          token: ${{ secrets.MY_REGISTRY_TOKEN }}
          registry: https://git.mydomain.com/api/packages/myuser/npm/

And this is the output I was getting from the action when I tried to run the workflow:

::error::NpmCallError: Call to "npm publish" exited with non-zero exit code 1%0Anpm ERR! code ENEEDAUTH%0Anpm ERR! need auth This command requires you to be logged in to https://git.mydomain.com/api/packages/myuser/npm/%0Anpm ERR! need auth You need to authorize this machine using npm adduser%0A%0Anpm ERR! A complete log of this run can be found in: /root/.npm/_logs/2024-03-28T19_08_34_852Z-debug-0.log

After much troubleshooting, I realized that the problem was in the npmrc file. Authentication would only work if the auth token line included the host and path from the registry URL, not just the host. With this change, I was able to publish successfully to the private registry. I'm hoping to get this merged in case it helps anyone in a similar situation to mine.

mcous commented 5 months ago

Thanks for your contribution @bdr99! Following the example of actions/setup-node, I added some tests and a little bit of extra logic to ensure the registry always ends in a trailing slash

bdr99 commented 5 months ago

Thanks for merging this, and for adding those tests!