如何在 sls 部署期间安装私有 npm github 包

How to install a private npm github package during sls deploy

我的 CI/CD 无服务器部署失败,因为它无法安装私有 npm 包。

Error --------------------------------------------------

npm install failed with code 1 npm ERR! code ENOENT npm ERR! syscall spawn git npm ERR! path git npm ERR! errno ENOENT npm ERR! enoent Error while executing: npm ERR! enoent undefined ls-remote -h -t ssh://git@github.com/private-org/private-repo.git npm ERR! enoent npm ERR! enoent npm ERR! enoent spawn git ENOENT npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent

npm ERR! A complete log of this run can be found in: npm ERR! /github/home/.npm/_logs/2020-05-28T13_30_18_595Z-debug.log

  at ChildProcess.child.on.exitCode (/github/workspace/node_modules/serverless-webpack/lib/utils.js:91:16)
  at ChildProcess.emit (events.js:198:13)
  at ChildProcess.EventEmitter.emit (domain.js:448:20)
  at maybeClose (internal/child_process.js:982:16)
  at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)

From previous event: at PluginManager.invoke (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:505:22) at PluginManager.spawn (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:525:17) at ServerlessWebpack.BbPromise.bind.then.then.then (/github/workspace/node_modules/serverless-webpack/index.js:91:53) at runCallback (timers.js:705:18) at tryOnImmediate (timers.js:676:5) at processImmediate (timers.js:658:5) at process.topLevelDomainCallback (domain.js:126:23)

Get Support -------------------------------------------- Docs: docs.serverless.com Bugs: github.com/serverless/serverless/issues Issues: forum.serverless.com Your Environment Information --------------------------- Operating System: linux Node Version: 10.20.1 Framework Version: 1.54.0 Plugin Version: 3.6.12 SDK Version: 2.3.1 Components Core Version: 1.1.2 Components CLI Version: 1.4.0

  deploy:
    name: deploy
    needs: test
    if: startsWith(github.ref, 'refs/tags/')
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - uses: actions/setup-node@v1
      with:
        node-version: '12.x'
    - uses: webfactory/ssh-agent@v0.2.0
      with:
        ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} 
    - name: npm install
      run: npm install 
    - name: serverless deploy
      uses: serverless/github-action@master
      with:
        args: deploy
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        SLS_DEBUG: true

通常我使用 webfactory/ssh-agent@v0.2.0 解决这个问题,所以第一个 npm 安装在这里工作正常,它设法使用提供的 SSH 密钥安装私有包。

但是在无服务器部署过程中出现上述错误,无法安装私有 npm 包。有没有一种方法可以指定要使用的无服务器操作的 SSH 密钥?

我想出了一个解决方案,但这意味着我不得不放弃无服务器操作。

  deploy:
    name: deploy
    needs: test
    if: startsWith(github.ref, 'refs/tags/')
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - uses: actions/setup-node@v1
      with:
        node-version: '12.x'
    - uses: webfactory/ssh-agent@v0.2.0
      with:
        ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
    - name: deploy
      run: |    
        npm i -g serverless
        npm install 
        serverless config credentials --provider aws --key $AWS_ACCESS_KEY_ID --secret $AWS_SECRET_ACCESS_KEY
        sls deploy
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

我遇到了和你一样的错误,我错误地找到了解决方案。 在 circleCI 中,无服务器正在读取包含私人 npm 包授权令牌的 ~/.npmrc 文件,但它没有读取包含私人公司包路径的本地项目 .npmrc 文件。

所以不小心将私有路径复制到 ~/.npmrc 并且神奇地部署 t 成功了。

之后我更新了我的 circleCI 步骤以在 ~/.npmrc

中获取这两条信息
step_login_github_packages: &step_login_github_packages
  name: Log in to Github Packages
  command: |
    echo "//npm.pkg.github.com/:_authToken=$GITHUB_PACKAGES_TOKEN" >> ~/.npmrc
    echo "@my-company:registry=https://npm.pkg.github.com/my-company" >> ~/.npmrc