Search code examples
gitgithubyarnpkg

git@github.com: Permission denied (publickey) when using yarn to install github js package


Today when I execute the yarn command in the macOS(Intel Chip), show error like this:

$ yarn                                                                                                 ‹ruby-2.7.2›
yarn install v1.21.1
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/4] 🔍  Resolving packages...
warning webpack > watchpack > watchpack-chokidar2 > chokidar > fsevents > node-pre-gyp@0.12.0: Please upgrade to @mapbox/node-pre-gyp: the non-scoped node-pre-gyp package is deprecated and only the @mapbox scoped package will recieve updates in the future
[2/4] 🚚  Fetching packages...
error Command failed.
Exit code: 128
Command: git
Arguments: ls-remote --tags --heads ssh://git@github.com/jiangxiaoqiang/js-wheel.git
Directory: /Users/dolphin/source/reddwarf/frontend/Cruise-Radar
Output:
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
(base)

and I config the js-wheel in package.json like this:

"js-wheel": "jiangxiaoqiang/js-wheel",

why still tell me permission denied even though the repo is public? I have tried using this command to generate the rsa key:

$ ssh-keygen -t rsa -b 4096 -C “balabala@gmail.com”                                              ‹ruby-2.7.2›
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/dolphin/.ssh/id_rsa):
/Users/dolphin/.ssh/id_rsa already exists.
Overwrite (y/n)?
(base)

what should I do to avoid this problem? When I using the https in the package.json, why the yarn still using the git way to fetch code? This is my dependencies config in package.json:

"js-wheel": "https://github.com/jiangxiaoqiang/js-wheel.git",

BTW, I have already add the config in GitHub Actions, this is the full github action script:

name: cruise-web-pro

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 16
      # https://stackoverflow.com/questions/71318659/gitgithub-com-permission-denied-publickey-when-using-yarn-to-install-github
      - name: Build React App
        run: |
          git config --global url.https://github.com/.insteadOf git@github.com: 
          npm install yarn -g
          yarn
          yarn build

      - name: Build image push to aliyun
        uses: docker/build-push-action@v1
        with:
          registry: ${{ secrets.ALI_DOCKER_HUB_REGISTRY }}
          username: ${{ secrets.ALIYUN_DOCKER_REPO_USER_NAME }}
          password: ${{ secrets.ALIYUN_DOCKER_REPO_USER_PASSWORD }}
          tags: ${{ github.sha }}
          repository: reddwarf-pro/cruise-web
          path: '.'
    
       # https://github.com/steebchen/kubectl
      - name: deploy to cluster
        uses: steebchen/kubectl@v2.0.0
        with: # defaults to latest kubectl binary version
          config: ${{ secrets.KUBE_CONFIG_DATA }}
          command: set image --record deployment/cruise-web cruise-web=registry.cn-hangzhou.aliyuncs.com/reddwarf-pro/cruise-web:${{ github.sha }} -n reddwarf-pro

      - name: verify deployment
        uses: steebchen/kubectl@v2.0.0
        with:
          config: ${{ secrets.KUBE_CONFIG_DATA }}
          version: v1.21.0 # specify kubectl binary version explicitly
          command: rollout status deployment/cruise-web -n reddwarf-pro

Solution

  • You need first to check if ssh -Tv git@github.com authenticates you, meaning if its output ends with a greeting message with your GitHub username.

    As long as that won't work, a git ls-remote ssh://git@github.com/... would not work.

    The alternative is to force the use of HTTPS URLs:

    git config --global url.https://github.com/.insteadOf git@github.com:
    

    That would work especially for public repositories, since no credentials would be needed.