首页 > 解决方案 > 如何将多个私有源拉入 Azure 上的管道代理?

问题描述

我目前遇到了一个问题,即我能够checkout阶段中的一个私有 GitHub 源中提取,但由于缺乏访问权限而无法从管道中的后期提取另一个私有 GitHub 源。script注意:两个 Repos 都是同一个组织的一部分。而且我已经将 Azure 应用程序连接到 GitHub 并可以访问所有存储库

在我的checkout舞台上,我明确设置了persistCredentials: true. 据我了解,这应该允许管道中的以下脚本使用在“获取源”的结帐中使用的 GitHub 凭据。

以下是失败的脚本示例:

- script: |
git clone --branch=username --single-branch 
https://github.com/username/myRepo.git $(Agent.BuildDirectory)/myRepo
displayName: 'clone myRepo' 

和输出:

Generating script.
[command]/bin/bash --noprofile --norc /Users/vsts/agent/2.140.2/work/_temp/cb2622cc-28e0-435a-bb98-154bdabf9641.sh
Cloning into '/Users/vsts/agent/2.140.2/work/1/myRepo'...
fatal: could not read Username for 'https://github.com': Device not configured
##[error]Bash exited with code '128'

标签: gitgithubazure-devopsazure-pipelines

解决方案


我也遇到了这个问题。我最终使用InstallSSHKey 任务安装了一个新的部署密钥。这些步骤非常简单。

  1. 在本地机器上生成一个密钥ssh-keygen -f <key file name>
  2. 将私钥上传到Azure Pipelines 中的安全文件
  3. 使用获取 github 的公共 rsa 密钥ssh-keyscan github.com
  4. 将以下内容添加到您的azure-pipelines.yml
steps:
# ...
- task: InstallSSHKey@0
  inputs:
    knownHostsEntry: github.com ssh-rsa ...  # Github's public key from ssh-keyscan
    sshPublicKey: ssh-rsa ... # The public key (.pub) to key you generated
    sshKeySecureFile: my_deploy_key # This is the name you gave the secure file when you uploaded it
- script: |
    git clone git@github.com:user/repo.git
# ...
  1. 将公钥作为部署密钥添加到您需要在 github 中克隆的存储库中。

IIRC,您只能在一个存储库中使用部署密钥,因此可能需要为您需要克隆的每个存储库执行此操作。

PS。我发现 Azure Pipelines 的任务文档有点难以理解,但大多数任务都在这个 repo中定义。这是我使用的InstallSSHKey 任务的定义。


推荐阅读