git $(System.PullRequest.SourceBranch)未找到

htzpubme  于 2023-09-29  发布在  Git
关注(0)|答案(3)|浏览(105)

参考文档:Microsft Guide
我正在使用命令行任务来创建git标签,我想针对System.PullRequest.SourceBranch。问题是不允许我使用这个,因为它通过Azure管道给了我以下错误:

System.PullRequest.SourceBranch: /home/vsts/work/_temp/be5fc781-2582-4e49-a126-7e4db3302e75.ps1:7
Line |
   7 |  git checkout -b $("$(System.PullRequest.SourceBranch)".replace('refs/ …
     |                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The term 'System.PullRequest.SourceBranch' is not recognized
     | as a name of a cmdlet, function, script file, or executable
     | program. Check the spelling of the name, or if a path was
     | included, verify that the path is correct and try again.

##[error]PowerShell exited with code '1'.

即使我尝试上面的命令,它也不会让我看到任何值:

echo $(System.PullRequest.SourceBranch)

我现在使用这个命令:
git checkout -b $("$(System.PullRequest.SourceBranch)".replace('refs/heads/', ''))
我的当前文件如下:

trigger:
- none
## notes
pool:
  vmImage: ubuntu-latest

## Job to calculate semantic version
jobs:
  - job: CalculateVersion
    displayName: Semantic versioning
    
    steps:
      # Checkout with persist credentials
      - checkout: self
        persistCredentials: true

      # Install GitVersion
      - task: gitversion/setup@0
        displayName: Install GitVersion
        inputs:
          versionSpec: '5.x'

      # Retrieve Pull Request Description
      - task: PullRequestDescription@0
        name: RetrievePullRequestDescription
        displayName: Retrieve Pull Request description
        inputs:
          action: 'view'
          outputVariable: 'PullRequest.DescriptionContent'
          isOutput: true
          stripIdentifiers: false

      # Add git commit message that will be picked up by GitVersion ("+semver: patch/minor/major")
      # Depending on the Pull Request description, where the developer has marked the type of change
      - task: PowerShell@2
        displayName: Add git commit message for SemVer
        inputs:
          targetType: inline
          script: |
            Write-Host "Configuring git author info.." -ForegroundColor Cyan
          
            git config user.email "[email protected]"
            git config user.name "alan.haro"
            Write-Host "Doing git checkout..." -ForegroundColor Cyan
            git checkout -b $("$(System.PullRequest.SourceBranch)".replace('refs/heads/', ''))
            Write-Host "Checking Pull Request description..." -ForegroundColor Cyan
            $PRdesc = "$(RetrievePullRequestDescription.PullRequest.DescriptionContent)"
            if ($PRdesc -match '(\[x\] \bFix\b)') {
              Write-Host "Adding git (empty) commit message to mark this branch as a 'patch' SemVer increment." -ForegroundColor Cyan
              git commit -a -m "+semver: patch [skip azurepipelines]" --allow-empty
            } elseif ($PRdesc -match '(\[x\] \bFeature\b)') {
              Write-Host "Adding git (empty) commit message to mark this branch as a 'minor' SemVer increment." -ForegroundColor Cyan
              git commit -a -m "+semver: minor [skip azurepipelines]" --allow-empty
            } elseif ($PRdesc -match '(\[x\] \bBig\b)') {
              Write-Host "Adding git (empty) commit message to mark this branch as a 'major' SemVer increment." -ForegroundColor Cyan
              git commit -a -m "+semver: major [skip azurepipelines]" --allow-empty
            } else {
              Write-Host "##vso[task.LogIssue type=error;]Please select the type of change in the Pull Request description, and Re-queue the validation." -ForegroundColor Cyan
              $PRdesc
              exit 1
            }
            Write-Host "Doing git push.." -ForegroundColor Cyan
            git push --set-upstream origin $("$(System.PullRequest.SourceBranch)".replace('refs/heads/', ''))
            Write-Host "Done." -ForegroundColor Cyan
          
      # Determine the semantic version
      - task: gitversion/execute@0
        displayName: Determine SemVer

主要的想法或概念是能够使用这个工具:Major, Minor, Patch Tags
有人知道如何解决这个问题吗?

7gcisfzg

7gcisfzg1#

如果您将脚本用于pull request验证,则该脚本可以正常工作:

如果你只为某个分支触发构建,它将不起作用:

我只测试了这一行:

git checkout -b $("$(System.PullRequest.SourceBranch)".replace('refs/heads/', ''))

检查您的生成验证策略:Improve code quality with branch policies

fzwojiic

fzwojiic2#

我通过在我的pull request描述中添加以下模板来解决我的问题:
描述感谢您对Bla Bla repo的贡献。在提交此PR之前,请确保:

  • [ ] 修复
  • [ ] 特征
  • [ ] 大

现在,脚本正在工作。如果您想查看将此添加到项目中的指南,则必须查看此指南:https://www.moderndata.ai/2021/10/automatic-semantic-versioning-in-azure-devops-with-release-notes/

yrefmtwq

yrefmtwq3#

  • System.PullRequest.SourceBranch* 仅在 Build.Reason=PullRequest 时设置。否则它是未定义的。

检查Build.Reason的值,如果是 PullRequest,则使用System.PullRequest.SourceBranch,如果是 IndividualCI,则使用 * Build.SourceBranchName *。

相关问题