简体   繁体   English

Azure DevOps:使用“git clone”的 Powershell 管道无法正常工作

[英]Azure DevOps: Pipeline with Powershell using "git clone" not working

Having the following error:出现以下错误:

remote: TF401019: The Git repository with name or identifier MyRepo does not exist or you do not have permissions for the operation you are attempting.远程:TF401019:名称或标识符为 MyRepo 的 Git 存储库不存在,或者您没有权限执行您正在尝试的操作。 fatal: repository 'https://dev.azure.com/MyCompany/MyProject/_git/MyRepo/' not found致命:未找到存储库“https://dev.azure.com/MyCompany/MyProject/_git/MyRepo/”

Pipeline:管道:

stages:
 - stage: "Stage1"
   jobs:
    - job: MyJob
      displayName: 'MyJob'
      pool: 'MyWindowsSrv'
      steps:
      - checkout: self
        persistCredentials: true

      - task: PowerShell@2
        displayName: Some name...
        env:
          SYSTEM_ACCESSTOKEN: $(System.AccessToken)
        name: some_name
        inputs:
          targetType: 'inline'
          script: |
            $path = "C:\MyRepo"
            If(!(test-path -PathType container $path))
            {
                    Set-Location C:\ -PassThru
                    Git clone https://$env:SYSTEM_ACCESSTOKEN@dev.azure.com/MyCompany/MyProject/_git/MyRepo
            }
            Set-Location $path -PassThru
            Git pull
          failOnStderr: true
          showWarnings: true
          workingDirectory:  $(Build.SourcesDirectory)

As you can see, I have also set the security settings:如您所见,我还设置了安全设置: 存储库中的安全设置

Any ideas?有任何想法吗?

Please note that there are two service accounts, one is the collection level account (Project Collection Build Service (xxxxx)) and another is project level account ({ProjectName} Build Service (xxx) ).请注意,有两个服务帐户,一个是集合级帐户(Project Collection Build Service (xxxxx)),另一个是项目级帐户({ProjectName} Build Service (xxx) )。 If you have enabled the Limit job authorization scope to current project for non-release pipelines setting, you are using the project level account.如果您已启用将作业授权 scope 限制为非发布管道的当前项目设置,则您使用的是项目级别帐户。 Please make sure the corresponding build service account has the required permission.请确保相应的构建服务帐户具有所需的权限。

At the same time, please check if you have enabled Protect access to repositories in YAML pipelines .同时请检查您是否在 YAML 管道中启用了保护对存储库的访问

With this option enabled, you can reduce the scope of access for all pipelines to only Azure DevOps repositories explicitly referenced by a checkout step or a uses statement in the pipeline job that uses that repository.启用此选项后,您可以将所有管道的 scope 访问权限减少到仅 Azure DevOps 存储库,这些存储库由使用该存储库的管道作业中的结帐步骤或使用语句明确引用。

If you do, you could try disabling the setting Protect access to repositories in YAML pipelines in Project settings->Pipelines->Settings to see if it works.如果这样做,您可以尝试在 Project settings->Pipelines->Settings 中禁用设置Protect access to repositories in YAML pipelines ,看看它是否有效。 在此处输入图像描述 For your reference, you could find more details in this official doc: Access repositories, artifacts, and other resources .作为参考,您可以在此官方文档中找到更多详细信息: Access repositories, artifacts, and other resources

By the way, you could also click the button Gnerate Git Credential when you trying to clone the repo.顺便说一句,您还可以在尝试克隆存储库时单击按钮Gnerate Git Credential 在此处输入图像描述

And use the username and the password in the following script to clone the repo:并使用以下脚本中的用户名和密码克隆存储库:

git clone https://username:password@dev.azure.com/MyOrganization/MyProject/_git/MyRepoName

And you could also consider simply using the checkout step.您也可以考虑简单地使用结帐步骤。

If you want to checkout multiple repositories, you could consider following the steps in this official doc: Check out multiple repositories in your pipeline .如果你想检出多个存储库,你可以考虑按照这个官方文档中的步骤操作: 检出管道中的多个存储库

resources:
  repositories:
  - repository: MyAzureReposGitRepository
    type: git
    name: OtherProject/MyAzureReposGitRepo

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

steps:
- checkout: self
- checkout: MyAzureReposGitRepository

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM