简体   繁体   中英

GitHub Actions throws Git Error when npm installing private GitHub repo

I have an GitHub Repo with some testing workflows:

name: Tests the App

on:
  push:
    branches: [main]

jobs:
  test:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16.x]

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
        cache: "npm"
    - run: echo "Testing App."
    - run: npm install
    - run: echo "Using ESLint."
    - run: npm run lint
    - run: echo "Testing App."
    - run: npm run test
    - run: echo "Test completed!"

Unfortunately it throws an git error with exit code 128:

npm ERR! code 128
npm ERR! An unknown git error occurred
npm ERR! command git --no-replace-objects ls-remote ssh://git@github.com/MYNAME/REPONAME.git
npm ERR! Warning: Permanently added the RSA host key for IP address 'SOMEIPADDR' to the list of known hosts.
npm ERR! git@github.com: Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.
npm ERR! 
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.
[…]
Error: Process completed with exit code 128.

When it tries to npm install the dependencies there is a private GitHub Repo REPONAME which needs to be installed from my account.

"dependencies": {
   "pckgname": "git+ssh://git@github.com:MYNAME/REPONAME.git#main"
}

What's the best way to make this work in ci/cd environments?

You could use webfactory/ssh-agent@v0.5.3 . First thing you would do is create an SSH key pair in the REPONAME.git repository, (preferably dedicated for Github actions), then put the private key as a secret in Github Actions, for this example we'll call it SSH_PRIVATE_KEY , then simply update your workflow like this:

 steps:
    - uses: actions/checkout@v2
    - uses: webfactory/ssh-agent@v0.5.3
        with:
          ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
    - ...

Further details on this action here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM