如何在Azure上的管道代理中提取多个私有资源?

时间:2018-10-12 20:43:05

标签: git github azure-devops azure-pipelines

我目前遇到的一个问题是,我可以在checkout阶段从一个私有GitHub来源中 拉出,但无法从另一个私有GitHub中拉出由于缺少访问权限,因此从script的源代码开始在管道中。注意:两个回购协议都是同一组织的一部分。而且我已经将Azure应用程序连接到GitHub并可以访问所有Repos

在我的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'

1 个答案:

答案 0 :(得分:1)

我也参加了。我最终使用InstallSSHKey task安装了一个新的部署密钥。这些步骤非常简单。

  1. 在本地计算机上,使用ssh-keygen -f <key file name>生成密钥
  2. 将私钥上传到secure file in Azure Pipelines
  3. 使用ssh-keyscan github.com
  4. 获取github的公共rsa密钥
  5. 将以下内容添加到您的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的任务文档,但是大多数任务是在this repo中定义的。这是我使用的InstallSSHKey task的定义。