[英]Install an npm module from a private GitHub repository using GitHub Actions
我正在嘗試使用 GitHub 操作為 Node.js 項目運行構建。 作為npm install
的一部分,我需要直接從私有 GitHub 存儲庫(不是來自 GPR)安裝 npm 模塊。
在package.json
我有:
"dependencies": {
...
"my-module": "github:<org>/<my-module>#master",
...
},
但是,在運行npm install
時,我得到:
npm 錯誤。 git@github:com。 權限被拒絕(公鑰):npm ERR。 致命:無法從遠程存儲庫中讀取。
該存儲庫是我自己組織的一部分,並且在本地(即從我的機器上)它可以工作。 我怎樣才能讓這個運行?
我已經嘗試設置NODE_AUTH_TOKEN
環境變量,但沒有任何區別。 盡管您經常發現此建議,但它似乎只針對 GPR。 我想避免的是必須將令牌硬編碼到package.json
文件中。 對此有什么想法嗎?
這就是我設法從私有 GitHub 存儲庫安裝依賴項的方式。
package.json 中的依賴可以添加如下。 github:
前綴是可選的。 指定#branch
或#tag
也是可選的。
"dependencies": {
...
"myrepo": "username/myrepo#master",
"myotherrepo": "github:username/myotherrepo"
},
這是一個示例工作流程。 PAT
是一個repo
范圍的Personal Access Token 。 在actions/checkout
上禁用持久憑證很重要,否則它們將覆蓋您的PAT
。 請注意, git config
更改在步驟之間持續存在,因此您只需為每個作業運行一次。
- uses: actions/checkout@v2
with:
persist-credentials: false
- uses: actions/setup-node@v1
with:
node-version: 12.x
- run: git config --global url."https://${{ secrets.PAT }}@github.com/".insteadOf ssh://git@github.com/
- run: npm ci
...
從錯誤和您包含依賴項的方式(在 package.json 中)來看,您似乎沒有傳遞身份驗證憑據(令牌、ssh。
有關作為依賴項的 Git URL 的詳細信息,請參閱此文檔
它可以通過 https 和 oauth 或 ssh 完成。
https 和 oauth : 創建一個具有“repo”范圍的訪問令牌,然后使用以下語法:
"package-name": "git+https://<github_token>:x-oauth-basic@github.com/<user>/<repo>.git"
或者
ssh :設置 ssh 然后使用以下語法:
"package-name": "git+ssh://git@github.com:<user>/<repo>.git"
(note the use of colon instead of slash before user)
一個非常簡單的解決方案對我有用。 您創建一個新密鑰,為其授予對存儲庫的只讀訪問權限,並且在構建過程中使用該密鑰。 您可以添加多個鍵。
基於以下操作 - https://github.com/webfactory/ssh-agent
package.json
中提供一個私有repo,格式如下:"dependencies": {
"repo": "git+ssh://git@github.com:user/private_repo.git",
}
ssh-keygen -f my_key
my_key.pub
的內容REPO_SSH_KEY
的 GitHub 操作密鑰。 使用my_key
的內容——它是一個私鑰# ...
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Add SSH key to chekout a private repo
uses: webfactory/ssh-agent@v0.5.4
with:
ssh-private-key: ${{ secrets.REPO_SSH_KEY }}
- name: Checkout code
uses: actions/checkout@v2
# ... the rest steps
您應該編輯您的.npmrc
文件。 你也可以使用npm config
npm config set @myco:registry http://reg.example.com
有關更多信息,請參閱以下主題: 有沒有辦法在單個 npmrc 文件中配置多個注冊表
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.