Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to publish to the GitHub Registry #130

Closed
JamesIves opened this issue Mar 31, 2020 · 24 comments
Closed

Unable to publish to the GitHub Registry #130

JamesIves opened this issue Mar 31, 2020 · 24 comments
Assignees
Labels
question Further information is requested

Comments

@JamesIves
Copy link

JamesIves commented Mar 31, 2020

I've been following this document (and the README): https://github.community/t5/GitHub-Actions/bd-p/actions, specifically the part about cross publishing between npmjs and the GitHub registry, but I can't seem to get it to work. This is my workflow: https://github.com/JamesIves/github-pages-deploy-action/blob/dev/.github/workflows/publish.yml

The problem I'm having is that every time I publish it says I must authenticate, except it's providing a URL related to the npmjs registry: 401 Unauthorized - PUT https://registry.npmjs.org/github-pages-deploy-action - You must be logged in to publish packages.

This is the full log. Am I missing something here or is this a bug?

@bryanmacfarlane bryanmacfarlane added the question Further information is requested label Mar 31, 2020
@jasonkarns
Copy link

Same issue:

npm notice 
55
npm ERR! code E401
56
npm ERR! 401 Unauthorized - PUT https://registry.npmjs.org/@nodenv%2fnodenv - You must be logged in to publish packages.

with following workflow :

npm:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPMJS_TOKEN }}

I think it's a bug (or their docs are wrong).

@JamesIves
Copy link
Author

Interesting, npmjs seems to pass for me when specifying registry-url: 'https://registry.npmjs.org', but only the GitHub registry seems to fail for me. I'm guessing that whatever mechanism that overrides the .npmrc file isn't behaving correctly in my case.

@JamesIves
Copy link
Author

@jasonkarns Were you able to get this to work for yourself?

@jasonkarns
Copy link

jasonkarns commented Apr 2, 2020

@JamesIves I needed to explicitly provide the scope and registry-url inputs to the action. Rather frustrating limitation at present, IMO. the scope is redundant information (it's in the package name), and the registry should either default to npm and/or respect the publishConfig setting in package.json. But at least it's working :/

@JamesIves
Copy link
Author

JamesIves commented Apr 2, 2020

Interesting, I can't seem to get it to work for the life of me. Tried in several repositories now, the first publish to npmjs.org works. It goes wrong when I try and specify anything besides npmjs. I've even try separating this into separate steps incase there's some form of conflict or permission issue.

   # Setup .npmrc file to publish to npm
    - uses: actions/setup-node@v1
      with:
        node-version: 12
        registry-url: 'https://registry.npmjs.org'

    - name: Configure git
      run: |
        git config user.email "[email protected]"
        git config user.name "James Ives"
    - run: npm install
    - run: npm run-script build
    - run: npm version patch -m "Release %s 📣"
    - run: git push
    - run: npm ci

    # Publish to npm
    - run: npm publish --access public
      env:
        NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

    # Setup .npmrc file to publish to GitHub Packages
    - uses: actions/setup-node@v1
      with:
        node-version: 12
        registry-url: 'https://npm.pkg.github.com'
        scope: '@JamesIves'

    # Publish to GitHub Packages
    - run: npm publish
      env:
        NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

However it still doesn't seem to be correctly overwriting the .npmrc file after the second call to setup-node as I get this error (the PUT request is still being made to npmjs.org instead of npm.pkg.github.com)

401 Unauthorized - PUT https://registry.npmjs.org/github-pages-deploy-action - You must be logged in to publish packages.

@petecorreia-appital
Copy link

I've got it working by setting ~/.npmrc myself with the following:

      - name: Setup Node
        uses: actions/setup-node@v1
        with:
          node-version: "13.x"
      - name: Install dependencies
        run: |
          echo "//npm.pkg.github.com/:_authToken=$NODE_AUTH_TOKEN" > ~/.npmrc
          npm ci
        env:
          NODE_AUTH_TOKEN: ${{ github.token }}

I also have .npmrc files in a few directories with the correct registries define, for example:

@example:registry=https://npm.pkg.github.com

@JamesIves
Copy link
Author

JamesIves commented Apr 7, 2020

I also have .npmrc files in a few directories with the correct registries define, for example:

Can you provide more context to this? Do you have two .npmrc files somehow? I have @JamesIves/package-name defined as the name in my package.json file and I still can't get it to switch from npmjs to github packages. Here's my updated/simplified workflow, for now I've stripped out the step that published to npmjs to see if it was something related to that but it's still trying to default to npmjs as opposed to GitHub:

name: publish-to-npm
on:
  release:
    types: [created]
  push: 
    branches:
      - publish-test
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
      with:
        ref: dev

    # Setup .npmrc file to publish to npm
    - uses: actions/setup-node@v1
      with:
        node-version: '13.x'

    - run: echo "//npm.pkg.github.com/:_authToken=$NODE_AUTH_TOKEN" > .npmrc
       env:
          NODE_AUTH_TOKEN: ${{ github.token }}

    - run: ls -a

    - name: Configure git
      run: |
        git config user.email "[email protected]"
        git config user.name "James Ives"

    - run: npm install
    - run: npm run-script build
    - run: npm ci

    # Publish to GitHub Packages
    - run: npm publish
      env:
        NODE_AUTH_TOKEN: ${{ github.token }}

Running ls -a reveals the .npmrc file is correctly being created at the root. Could there be anything in my branch that is causing this conflict?

@meister
Copy link

meister commented Apr 27, 2020

I also ran into similar issue. But after a bit of debugging, it looks like the .npmrc file for github registry adds @scope, which means if we try to publish a non-scoped package, the authentication fails. You can add

run: cat $NPM_CONFIG_USERCONFIG

to see if it’s the same for you.
Also by looking at the action code (https://github.com/actions/setup-node/blob/master/src/authutil.ts#L25) it looks like for Github registry, the scope is always added.

So not sure if it’s even possible to publish non-scoped packages in Github, if not, then it sort of conflicts with the NPM and can’t publish to both places with the same package.json file, unless a scope is added.

@JamesIves
Copy link
Author

By scoping do you mean adding @repoowner/repo-name within the name field package.json or is there a specific scope field I'm not aware of?

@meister
Copy link

meister commented Apr 27, 2020

By scoping do you mean adding @repoowner/repo-name within the name field package.json or is there a specific scope field I'm not aware of?

Yes exactly. Just tried it and it started to work. You can then publish the same thing in NPM as well, as long as you add npm publish —access public. Otherwise scoped packages there are private by default.

@chriskacerguis
Copy link

So, I don't know if this applies to your situation or not, but I was having the exact same issue. I came across this doc

https://help.github.com/en/packages/using-github-packages-with-your-projects-ecosystem/configuring-npm-for-use-with-github-packages#publishing-a-package-using-publishconfig-in-the-packagejson-file

I added this to my repo and it worked.

"publishConfig": { "registry": "https://npm.pkg.github.com/" },

Good luck

@meister
Copy link

meister commented Apr 27, 2020

Thanks @chriskacerguis for the info.

But as for me (and it looks like it’s similar to @JamesIves ), I was trying to publish to both NPM and Github.. But I wonder if with the publishConfig, would I still be able to publish to NPM? Or can I then publish to Github without scope?

Either way I renamed my package in NPM with the scope, so that it matches Github, and it started to work.

@JamesIves
Copy link
Author

JamesIves commented Apr 28, 2020

I've added the scope to the package name, and it doesn't seem to run correctly for me. For simplicity I even removed the npm publish part from the action file yet it still tries to go straight to npm.

@edouardr
Copy link

edouardr commented May 5, 2020

So I was having the same issue. I managed to get my private package published by using below workflow:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: 'Checkout GitHub Action'
        uses: actions/checkout@master
      - name: Setup Node 12
        uses: actions/setup-node@v1
        with:
          node-version: '12.x'
          registry-url: 'https://npm.pkg.github.com'
          scope: '@owner'
      - name: Authenticate with GitHub package registry
        run:
          echo "//npm.pkg.github.com:_authToken=${{ secrets.PAT }}" >
          ~/.npmrc
      - name: Build and Test
        shell: bash
        run: npm run setup
      - name: Publish
        shell: bash
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.PAT }}

What I did was to generate the .npmrc file manually at ~/ and inject a Private Access Token with sufficient permissions.

On top of that, I also used scoped package name in package.json.

{
...
  "name": "@owner/package-name",
...
}

@redreceipt
Copy link

We're having the same problem. Locally, it publishes to the correct registry (GPR) but when using Github Actions, it tries to publish to NPM

Screen Shot 2020-09-10 at 3 50 38 PM

Here's our config https://github.com/ApollosProject/apollos-apps/blob/master/.github/workflows/publish.yml

@redreceipt
Copy link

I did two things. One of them fixed it, not sure which one it was and I'm not going to touch anything 😂

  1. Added @scope to the Action config
    - name: Use Node 10.19.x
      uses: actions/setup-node@v1
      with:
        node-version: 10.19.x
        registry-url: 'https://npm.pkg.github.com'
        scope: '@apollosproject'
  1. Added registry to my lerna.json. Didn't think it was necessary because I have them defined per package but I gues this action may need it here...
{
  "packages": ["packages/*"],
  "version": "1.5.0",
  "npmClient": "yarn",
  "useWorkspaces": true,
  "command": { "publish": { "registry": "https://npm.pkg.github.com" } }
}

@JamesIves
Copy link
Author

JamesIves commented Sep 11, 2020

I've managed to resolve this with my actions with the following workflow file:

https://github.com/JamesIves/github-pages-deploy-action/blob/dev/.github/workflows/publish.yml

I also added the scope to the package.json file here: https://github.com/JamesIves/github-pages-deploy-action/blob/dev/package.json#L2

It now cross publishes between npm and GitHub correctly. Thanks to everyone for their help resolving this. I'll leave this open as I still believe this is an issue with setup-node.

anaynayak added a commit to anaynayak/aws-cloudwatch-annotations that referenced this issue Oct 2, 2020
* Suspect it needs scoped packages. See actions/setup-node#130
nkuba added a commit to keep-network/keep-ecdsa that referenced this issue Jan 4, 2021
Package publication fails with `'@keep-network/[email protected]'
is not in the npm registry` error. Here we try to workaround the
problem by explicitly adding registry and scope, which was suggested in
actions/setup-node#130
nkuba added a commit to keep-network/keep-ecdsa that referenced this issue Jan 4, 2021
Package publication fails with `'@keep-network/[email protected]'
is not in the npm registry` error. Here we try to workaround the
problem by explicitly adding registry and scope, which was suggested in
actions/setup-node#130
@hpl002
Copy link

hpl002 commented Apr 3, 2022

Amazing that this hasn't been patched yet. Very misleading that the published actions is broken(for this particular use case) by default.

Please see @JamesIves post for remedy

@jojobyte
Copy link

jojobyte commented Jun 2, 2022

My repo was having a similar issue to this one. Replacing the package name in package.json by adding the scope to it solved it for Github Package Registry.

// original
{
    "name": "traefikjam",
}

// fix for GPR publish
{
    "name": "@jojobyte/traefikjam",
}

My fix: https://github.com/jojobyte/traefikjam/blob/master/.github/workflows/node-build.yml#L40

jobs:
  npm-build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: '18.x'
          registry-url: 'https://registry.npmjs.org'
      - run: npm ci
      - name: Publish to NPM
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
      - uses: actions/setup-node@v3
        with:
          node-version: '18.x'
          registry-url: 'https://npm.pkg.github.com'
          scope: '@scope'
      - run: npm ci
      - name: Publish to GitHub Package Registry
        run: |
          sed -i 's+"name": ".*+"name": "@${{ github.repository }}",+gI' ./package.json
          npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

This lets you publish to NPM with an unscoped package name and GPR with a scoped package name.

@nhhockeyplayer
Copy link

get off of node v16 and goto v18 for the latest npm
stay away from npmjs.org they are still collecting credit card hits and its not worth the $7 someone is still collecting charity and maybe its well needed but that entire footprint should be a lesson on how NOT to model an ecosystem

node v18... use the latest npm it will accept your scoped package name as appropriate
otherwise it will force you to NOT use the scoped package name which will fail your entire effort

prayers for microsoft to revamp all of this and stabilize this ecosystem for good

@dsame dsame self-assigned this Aug 12, 2022
@dsame
Copy link
Contributor

dsame commented Aug 13, 2022

Hello @nhhockeyplayer
Despite the node version the name of package must have a scope according to the docs

If the package name is not scoped one has to use the workaround like the one suggested by @jojobyte

While we are improving the README, please use the snippet as a reference in order to upload the package to both repos

    steps:
...
      - uses: actions/setup-node@v3
        with:
          node-version: 'v16.13.2'
          registry-url: 'https://registry.npmjs.org'

      - name: publish to npmjs
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          npm publish

      - uses: actions/setup-node@v3
        with:
          node-version: 'v16.13.2'
          scope: '@akv-demo'
          registry-url: 'https://npm.pkg.github.com'

      - name: publish to github
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN  }}
        run: |
          npm publish 

and the package.json must have both scope name and repository url match the github repo:

{
  "name": "@akv-demo/pkg1",
  ...
  "repository": {
    "url": "https://github.com/akv-demo/setup-node-test",
    "type": "git"
  }
}

The corresponding build is here: https://github.com/akv-demo/setup-node-test/runs/7819174590?check_suite_focus=true

and as it was already noted one of the reason of "401 Unauthorized - PUT https://registry.npmjs.org/github-pages-deploy-action - You must be logged in to publish packages" might be the package is not initially created with the public access.

@dsame
Copy link
Contributor

dsame commented Aug 22, 2022

The issue is closed because it is not reproducible in the current version of action.
@JamesIves please feel free to reopen this issue or create new one if the problem is not solved.

@dsame dsame closed this as completed Aug 22, 2022
deining pushed a commit to deining/setup-node that referenced this issue Nov 9, 2023
Bumps [husky](https://github.com/typicode/husky) from 4.3.6 to 4.3.7.
- [Release notes](https://github.com/typicode/husky/releases)
- [Commits](typicode/husky@v4.3.6...v4.3.7)

Signed-off-by: dependabot[bot] <[email protected]>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
@Vansh-Baghel
Copy link

I am not able to publish it on npmjs via GitHub actions. I tried to debug the code manier times, but I feel there an issue from the development side.

I used GitHub configured node js publish package as well, but even that didn't work.

Though npm publish --access public works.

Note: My name on npmjs account: haxionaz

GitHub workflow file:

name: Publish Package to npmjs
on:
  push:
    branches:
      - main

jobs:
  npm-publish:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Setup Node
        uses: actions/setup-node@v2
        with:
          node-version: "20.11.0"
          registry-url: "https://registry.npmjs.org"
      - name: Publish package on NPM 📦
        run: npm publish --access public
        env:
          NPM_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

package.json

  "name": "@haxionaz/test-actionsss",
  "repository": {
    "type": "git",
    "url": "https://github.com/Vansh-Baghel/Actions.git"
  },
  "version": "1.1.0",
...

Constant error:
image

@maddsua
Copy link

maddsua commented Sep 7, 2024

I also ran into similar issue. But after a bit of debugging, it looks like the .npmrc file for github registry adds @scope, which means if we try to publish a non-scoped package, the authentication fails. You can add

Thanks for mentioning this!
The error npm gives is utterly useless, how they expect anyone to be able to debug that ?!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests