GitHub Action执行npm install含私有仓库依赖时报错如何解决
问题原因
该报错本质是npm安装依赖时自动将SSH格式的私有仓库地址转换为HTTPS协议请求,GitHub已停止支持HTTPS密码验证,导致即使配置了SSH密钥也无法正常拉取私有依赖。
解决方案
按以下步骤修改配置即可修复:
- 首先确认密钥配置正确:你生成的SSH公钥需添加到待拉取的私有依赖仓库的**Deploy keys(部署密钥)**中,且勾选读取权限;私钥需完整填入当前工作流仓库的Secrets中,键名对应
SSH_PRIVATE_KEY。 - 在SSH密钥配置步骤后、安装依赖步骤前,添加Git全局配置,强制所有GitHub地址走SSH协议,禁止转换为HTTPS:
git config --global url."git@github.com:".insteadOf "https://github.com/" - 可选添加SSH连通性校验步骤,确认密钥配置生效:
ssh -o StrictHostKeyChecking=no -T git@github.com || true
修改后的工作流示例
steps: - uses: actions/checkout@v2 - name: webfactory/ssh-agent uses: webfactory/ssh-agent@v0.5.3 with: ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} # 新增Git协议重写配置 - name: Configure Git to use SSH for GitHub run: git config --global url."git@github.com:".insteadOf "https://github.com/" # 可选校验SSH连通性 - name: Test SSH connection to GitHub run: ssh -o StrictHostKeyChecking=no -T git@github.com || true - name: Set up Node Js version as ${{ matrix.node-version }} uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - name: Install The Dependencies run: npm install
内容的提问来源于stack exchange,提问作者Harsh Raj
相关产品推荐
相关产品推荐

