【问题标题】:Accessing external repo from azure pipeline with mutual SSL使用相互 SSL 从 azure 管道访问外部存储库
【发布时间】:2020-06-09 08:38:23
【问题描述】:

是否有一个选项可以像证书一样向 git-login-process 添加更多登录信息? 据我所知,配置中只有基本凭据是可能的。

是否有机会添加一些进一步的配置?也许是azure-pipelines.yml

【问题讨论】:

  • 请问这个状态怎么样?以下方法对您有帮助吗?如果您仍有疑问或疑问,请随时发表评论。

标签: azure-devops azure-pipelines


【解决方案1】:

通过以下方式解决了:

步骤 1

为克隆作业创建了一个模板文件:

parameters:
- name: RepoUrl
  type: string
- name: cloneIntoDir
  type: string
steps:
- task: DownloadSecureFile@1
  name: cainfo
  displayName: 'Download cainfo'
  inputs:
    secureFile: 'cainfo.cert'
- task: DownloadSecureFile@1
  name: cert
  displayName: 'Download cert.crt'
  inputs:
    secureFile: 'cert.crt'
- task: DownloadSecureFile@1
  name: keypem
  displayName: 'Download key.pem'
  inputs:
    secureFile: 'key.pem'
- script: mkdir ${{ parameters.cloneIntoDir }}
  displayName: creating directory ${{ parameters.cloneIntoDir }}    
- script: |
    cd ${{ parameters.cloneIntoDir }}
    git config --global http.sslCAInfo "$(cainfo.secureFilePath)"
    git config --global http.sslCert "$(cert.secureFilePath)"
    git config --global http.sslKey "$(keypem.secureFilePath)"
    git clone https://$(User):$(Password)@${{ parameters.RepoUrl }} .

此模板对从管道库下载的文件执行所有 git-magic。

第二步

在 build-yml 中使用了模板文件

trigger: none

strategy:
  matrix:
    linux:
      imageName: 'ubuntu-latest'
    windows:
      imageName: 'windows-2019'

pool:
  vmImage: $(imageName)

variables:
- group: Credentials


steps:
- template: ../templates/clone-repo-template.yml
  parameters:
    RepoUrl: 'www.repos_url.com/project.git'
    cloneIntoDir: 'myRepoDir'

此处重要:凭据的变量组必须包含在该文件中,尽管仅在模板中需要凭据(原因:变量不能在步骤中定位)

到那时它就完成了,您的仓库中的代码现在位于myRepoDir。您可以在步骤 2 中使用您的特定构建命令扩展该文件。 但是您可以更进一步:可能您希望将 build-yml 集成到您的存储库中,并且开发人员活动可能无法访问 azure-repo 但应该能够编辑 build-yml。为此:

第三步

创建另一个模板

parameters:
- name: RepoUrl 
  type: string
- name: copyScript
  type: string  

jobs:
- job: SyncRepos
  pool:
    vmImage: 'ubuntu-latest'
  variables:
   - name: remoteRepoDir
     value: 'DirectoryToCloneInto'
   - group: Credentials

  steps:
  - template: ../templates/clone-repo-template.yml
    parameters:
      RepoUrl: ${{ parameters.RepoUrl }}
      cloneIntoDir: $(remoteRepoDir)
  - script: 'git -c http.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" clone $(Build.Repository.Uri)'
    displayName: 'Clone Azure-Repo $(Build.Repository.Uri)'
  - script: |
        cd '$(Build.Repository.Name)'
        ${{ parameters.copyScript }}
    displayName: 'Copy file to direcorty $(Build.Repository.Name)'
  - script: |
        git config --global user.email "you@example.com"
        git config --global user.name "Your Name"
    displayName: 'Configure Git for commit'
  - script: |
        cd '$(Build.Repository.Name)'
        git add -A
        git commit -m "auto commit from azure sync"
        git -c http.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" push
    displayName: 'push changes'

如您所见,此模板也使用了步骤 1 中的模板

第四步

trigger: none

jobs:
- template: ../templates/sync-repo-files-template.yml
  parameters:
    copyScript: |
        cp -f ../$(remoteRepoDir)/azure-pipeline.yml .
    RepoUrl: 'www.repos_url.com/project.git'

在管道中运行该 yml 将克隆您的 repo,克隆 azure-repo,将 azure-pipeline.yml-file 从您的 repo 复制到 azure repo,存储、提交和推送。

您只需确保Build Service-user 也有权为您的 repo 做出贡献:

最后你可以: * 在您的私人仓库中编辑 azure 管道文件,提交 + 推送 * 让第 4 步中的同步管道运行 * 之后 azure repo 包含更新的管道文件 * 您可以在更新后的文件上运行您的构建作业

【讨论】:

    【解决方案2】:

    在构建管道的Get source步骤中,SSL是其中的默认选项。

    所以,这里有两种方法你可以考虑。

    方法一:

    1) 将 SSL 证书存储到 Azure Key Vault。

    2) 然后将此 Azure Key Vault 源连接到变量组

    3) 涉及此变量组并包括 azure 密钥库。

    对于上述内容,blog 中描述了详细的步骤。你可以检查一下。

    现在,在 azure devops 中,我们添加一个内置步骤 Azure Key Vault 任务,同时您启用 Azure 密钥保管库并将其链接到管道。此外,此内置步骤在 Get sources 步骤之前执行。所以,这个时候,证书就可以被Get sources正确的安装和使用了。

    方法二:

    另一种方法是,您配置一个自我代理并在您的构建机器中运行commands

    git config --global http.sslBackend schannel
    
    git config --global http.sslCAPath <the path/to/your/certificate.crt>
    

    【讨论】:

    • 感谢您的快速回复。我测试了方法 1,但在我的企业帐户中,缺少一些授权使用的权限:Ensure that the user has 'Owner' or 'User Access Administrator' permissions on the Subscription。在方法 2 中——据我所知——我要么需要一个单独的预配置 VM,要么使用位于例如的初始 azure-pipelines.yml。在管道的 repo 中,然后从然后启动的vmImage: 'windows-2019'-VM 中执行 git pull。对吗?
    • @gratinierer。对于您的第一个问题,您可能需要联系您的管理员为您分配 Azure 订阅中的所有者角色(注意,所有者是 azure 中的角色,而不是 azure devops 中的角色)。第二个,不需要VM,只要可以安装self-agent的机器/PC就可以了。
    • @gratinierer, vmImage: 'windows-2019' 这用于 YAML 管道,它是与托管代理一起运行的架构。正如我所说,在这里您可以使用自我代理。此时,无法使用。您应该将该架构替换为 pool: {pool name} 。看到这个:docs.microsoft.com/en-us/azure/devops/pipelines/…
    • @Liang 还有池:一切都以yml-文件开头,必须放在存储库中。因此,该作业必须能够在作业运行的第一步中访问该存储库。换句话说:在第二种解决方案中,我无法将我的yml 文件放入私有存储库中,因为作业无法通过在 azure devpos 前端中配置的直接方式访问它。对吗?
    猜你喜欢
    • 2021-01-14
    • 2023-03-08
    • 2017-01-01
    • 1970-01-01
    • 2020-02-17
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多