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

时间:2020-09-25 13:41:29

标签: azure azure-devops azure-pipelines devops

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

name: My nice pipeline
resources:
  repositories:
    - repository: RepoB
      type: git
      name: RepoB
      # ref: ${{ replace($(System.PullRequest.TargetBranch), "refs/heads/", "")}}  <<<< here is the issue

问题是,如果我仅使用平面$(System.PullRequest.TargetBranch),输入将被接受,但是ref:的工作方式,它将追加到refs / heads / {{无论此处输入什么内容}} ,所以我最终遇到了refs / heads / refs / heads / target-branch。作为一种解决方案,我虽然可以使用replace,但是是的,谢谢您的情况……

有什么主意,我可以只获取目标分支名称而不带前缀吗?

2 个答案:

答案 0 :(得分:0)

您不能在模板表达式([ [ sum: 400, year: 2020, month: 7, ] [ sum: 500, year: 2020, month: 8, ] ... ] )中使用System.PullRequest.TargetBranch。您已将其标记为here

enter image description here

目前我找不到它,但是从我的回忆中您无法在资源库资源中使用模板表达式。

很遗憾,这是不可能的。您不能在此处使用表达式。 enter image description here

如果您尝试使用这种语法{{}},您将会得到

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

答案 1 :(得分:0)

当我想在分支中创建标签并在管道中进行特定提交时,遇到了与您相同的问题。我发现最好的解决方案是让我的代理商执行我通常要做的所有工作并将其放在模板中: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'