在 Github Actions 中克隆私有仓库

5

我正在尝试在Github actions中克隆另一个私有仓库。我已经在运行actions的repo的secrets中设置了SECRET_USERSECRET_PASSWORD。在actions中,我运行以下命令:

git clone https://$SECRET_USER:$SECRET_PASSWORD@github.com/other-organization/other-repo.git

但是出现了错误

Cloning into 'other-repo'...
remote: Repository not found.
fatal: Authentication failed for 'https://github.com/other-organization/other-repo.git/'
##[error]Process completed with exit code 128.

在Github Actions中,尽管我已经验证了用户可以访问https://github.com/other-organization/other-repo(这显然不是内部仓库的真实URL),但仍存在问题。

在您的本地机器上,git clone https://$SECRET_USER:$SECRET_PASSWORD@github.com/other-organization/other-repo.git 是否可用? - Patrick Quirk
秘密应该像这样引用 ${{ secrets.SECRET_USER }},除非您的 run 步骤正在设置 env 变量。 - peterevans
3个回答

9

我在我的go.yml文件中添加了一个git配置步骤,解决了问题:

- name: Configure git
  env:
    TOKEN: ${{ secrets.ACCESS_TOKEN }}
  run: git config --global url."https://${TOKEN}:x-oauth-basic@github.com/".insteadOf "https://github.com/"

其中ACCESS_TOKEN个人访问令牌,我使用它来替代用户名/密码组合,因为我想要访问的私有仓库需要SSO启用的令牌来访问,而不是用户名/密码组合。不幸的是,这个信息从错误信息中并不明显,需要与其他人交流才能得知。

1
注意在企业环境中使用访问令牌,这可能很容易被暴露。最好使用部署密钥。 - rodrigocprates

0

您可以使用git-credential-store将您的凭据保存在文件中,这样当需要时,git就可以使用它。

在(我的情况下)Podfile/specfile中没有必要修改URL。

git config --global credential.helper store
echo "https://$SECRET_USER:$SECRET_PASSWORD@github.com" > ~/.git-credentials

我对将这些数据保存在明文文件中有着复杂的感受,但在 CI/CD 机器上,我认为这是可以接受的。我在这里使用它 here


0
  1. 创建名为USER_TOKEN的新个人令牌。

  2. 使用所创建的令牌。

    步骤:
       - uses: actions/checkout@v3
       - name: Git Bolivia
         run: |
           git --version
           git clone https://${{ secrets.USER_TOKEN }}:x-oauth-basic@github.com/Organization/repository.git
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接