将Azure管道中的模板用于存储库引用

将Azure管道中的模板用于存储库引用,azure,azure-devops,azure-pipelines,devops,Azure,Azure Devops,Azure Pipelines,Devops,我有一个有多个存储库的项目,我想使用一个特定的分支来签出RepoB,它与PR的目标分支相匹配 name: My nice pipeline resources: repositories: - repository: RepoB type: git name: RepoB # ref: ${{ replace($(System.PullRequest.TargetBranch), "refs/heads/", "&qu

我有一个有多个存储库的项目,我想使用一个特定的分支来签出RepoB,它与PR的目标分支相匹配

name: My nice pipeline
resources:
  repositories:
    - repository: RepoB
      type: git
      name: RepoB
      # ref: ${{ replace($(System.PullRequest.TargetBranch), "refs/heads/", "")}}  <<<< here is the issue
name:我的好管道
资源:
存储库:
-存储库:RepoB
类型:git
姓名:RepoB

#ref:${replace($(System.PullRequest.TargetBranch),“refs/heads/,”“)}不能在模板表达式中使用
System.PullRequest.TargetBranch
(即
{}
)。你已经做了标记

我现在找不到它,但据我回忆,您不能在资源库中使用模板表达式

不幸的是,这是不可能的。这里不能用表达式。

如果您尝试使用此语法
ref:$(System.PullRequest.TargetBranch)
,您将得到

/azure-pipelines-11.yml:无法使用ref-refs/heads/$(System.PullRequest.TargetBranch)获取托管在上的存储库kmadof/devops模板的最新源版本。GitHub报告了错误,“未找到SHA:refs/heads/$(System.PullRequest.TargetBranch)的提交”


当我想在分支上创建标记并在管道中指定提交时,我遇到了与您相同的问题。我找到的最好的解决办法就是让我的代理人做我通常会做的所有部分,然后把它放在一个模板中:p我就是这样解决的

  steps:
- checkout: none
  clean: True

- task: PowerShell@2
  displayName: Remove refs/heads from branch path and set pipeline variable
  inputs:
    targetType: 'inline'
    script: |
      $branchSourcePath = "$(Build.SourceBranch)" -replace "refs/heads/",""
      Write-Host "##vso[task.setvariable variable=BRANCH_PATH;]$branchSourcePath"
      Write-Host "Setting pipeline variable BRANCH_PATH to $branchSourcePath"

- task: PowerShell@2
  displayName: Create temp folder
  inputs:
    targetType: 'inline'
    script: 'New-Item -Path "$(Pipeline.Workspace)\temp" -ItemType Directory'

- task: PowerShell@2
  displayName: Init git repo
  inputs:
    targetType: 'inline'
    script: 'git init'
    workingDirectory: '$(Pipeline.Workspace)\temp'

- task: PowerShell@2
  displayName: Git remote add
  inputs:
    targetType: 'inline'
    script: git remote add $(Build.Repository.Name) https://$(git-accesstoken)@{azure-repo-path}/_git/$(Build.Repository.Name)
    workingDirectory: '$(Pipeline.Workspace)\temp'

- task: PowerShell@2
  displayName: Fetch
  inputs:
    targetType: 'inline'
    script: 'git fetch $(Build.Repository.Name) $(BRANCH_PATH)'
    workingDirectory: '$(Pipeline.Workspace)\temp'

- task: PowerShell@2
  displayName: Checkout commit
  inputs:
    targetType: 'inline'
    script: |
      Write-Host "Checking out commit $(Build.SourceVersion) \n"
      git checkout -b $(BRANCH_PATH) $(Build.SourceVersion)
    workingDirectory: '$(Pipeline.Workspace)\temp'

- task: PowerShell@2
  displayName: Set user email
  inputs:
    targetType: 'inline'
    script: git config --global user.email "$(Build.RequestedForEmail)"
    workingDirectory: '$(Pipeline.Workspace)\temp'

- task: PowerShell@2
  displayName: Set user name
  inputs:
    targetType: 'inline'
    script: git config --global user.name "$(Build.RequestedFor)"
    workingDirectory: '$(Pipeline.Workspace)\temp'

- task: PowerShell@2
  displayName: Create tag
  inputs:
    targetType: 'inline'
    script: 'git tag -a {tag-name} $(Build.SourceVersion) -m "{tag-description}"'
    workingDirectory: '$(Pipeline.Workspace)\temp'

- task: PowerShell@2
  displayName: 'Push tag'
  inputs:
    targetType: 'inline'
    script: 'git push {repo-name} {tag-name}'
    workingDirectory: '$(Pipeline.Workspace)\temp'

- task: DeleteFiles@1
  displayName: Delete temp folder
  inputs:
    SourceFolder: '$(Pipeline.Workspace)'
    Contents: 'temp'

那么,如何从$(System.PullRequest.TargetBranch)中删除不必要的ref/heads/,有什么方法可以做到吗?恐怕您无法参数化存储库资源。