4

I am currently trying to publish a package to the Github registry.

The package is generated code, although this should not really matter here. The important thing is that I have the following:

The package.json file contains:

{
    "name": "@company-name/repository-name",
    "version": "v1.7.0",
    "repository": "git://github.com/@company-name/repository-name.git"
}

The .npmrc file reads:

@company-name:registry=https://npm.pkg.github.com

Whereas this is what I am running in my Github Action:

      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '12.x'
          registry-url: 'https://npm.pkg.github.com'
          # Defaults to the user or organization that owns the workflow file
          scope: '@company-name'

      # ...

      - name: Install and publish
        working-directory: .generated/
        run: |
          sudo npm install
          sudo npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

I am following the docs but it seems I am missing something.

Log output

npm notice === Tarball Details === 
npm notice name:          @company-name/repository-name
npm notice version:       1.7.0                                      
npm notice package size:  11.6 kB                                    
npm notice unpacked size: 104.6 kB                                   
npm notice shasum:        2262a7f9ef1bb95b1d6ae2dc92095d04eb2a22b6   
npm notice integrity:     sha512-WaJvaoZV8uo2Y[...]Re2hW2A/BIO5Q==   
npm notice total files:   24                                         
npm notice 
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/2021-09-03T19_27_04_437Z-debug.log
Error: Process completed with exit code 1.
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
  • There is [a similar question here](https://stackoverflow.com/questions/68917546/github-action-in-github-enterprise-giving-401-and-404s-on-npm-packages). The first answer asked for feedback regarding some operations, could be interesting to discuss this topic there if you tried what has been suggested. – GuiFalourd Sep 03 '21 at 20:21
  • 3
    @GuiFalourd Thanks for the link. However, he's referring to [this](https://stackoverflow.com/questions/58919401/installing-packages-from-github-npm-registry-auth-error-401/63243950#63243950) solution and the problem here is that I am doing this for an organization and cannot use one of my personal tokens. :/ – Stefan Falk Sep 04 '21 at 06:58

2 Answers2

1

The way we were able to get around this issue was by adding a simple script to the action:

    - name: Install Dependencies
      run: |
        echo "@INSERT_ORG_NAME:registry=https://npm.pkg.github.com" > .npmrc
        echo "//npm.pkg.github.com/:_authToken=$GITHUB_PAT" >> .npmrc
        npm install
      env:
        GITHUB_PAT: ${{ secrets.REPO_READ_PAT }}

Second line is because we're dealing with a private repo.

cigien
  • 57,834
  • 11
  • 73
  • 112
JBB
  • 176
  • 1
  • 4
  • You can also just use environment variables directly in `.npmrc` files so you don't need to write the file in the action, just define the environment variable as you've done in the last two lines. https://stackoverflow.com/questions/48728714/how-to-set-env-var-for-npmrc-use – Henry Woody May 23 '22 at 17:33
0

i am using a private repository and my credentials was expired, with this situation i faced similar issue on executing

npm i

to fix this, i just edited my .npmrc to have always-auth = false and executed

npm i --registry https://registry.npmjs.org which worked for me.

Shashank.gupta40
  • 915
  • 1
  • 8
  • 26