获取 Azure 管道运行时的分支名称?

huangapple go评论61阅读模式
英文:

How to get branch name while running azure pipeline?

问题

我正在尝试通过Azure管道运行以下命令来获取分支名称,但执行后只得到了HEAD。

Process process = Runtime.getRuntime().exec("git rev-parse --abbrev-ref HEAD");
    
process.waitFor();

BufferedReader reader = new BufferedReader(
        new InputStreamReader(process.getInputStream()));

return reader.readLine();

我还尝试了以下命令,但没有成功:

  • git branch | grep '*'
  • git symbolic-ref --short HEAD

PS:我没有使用多分支管道,要求是不使用多分支。

英文:

I am trying to run below command through azure pipeline to get branch name but after execution getting HEAD.

Process process = Runtime.getRuntime().exec("git rev-parse --abbrev-ref HEAD");
    
    process.waitFor();

    BufferedReader reader = new BufferedReader(
            new InputStreamReader(process.getInputStream()));

    return reader.readLine();

I have tried below commands as well but no luck

  • git branch | grep '*'
  • git symbolic-ref --short HEAD

PS I am not using multi-branching pipeline and the requirements were to not use multi-branching.

答案1

得分: 2

如果您尝试获取触发构建的分支,可以使用一些内置的管道变量,无需编写脚本。

Build.SourceBranch 将返回类似于 refs/heads/mainrefs/heads/feature/tools 的内容。

Build.SourceBranchName 将返回 maintools

您可以在您的 YAML 文件中以不同的方式使用这些变量。

作为变量

$(Build.SourceBranch)
$(Build.SourceBranchName)

作为表达式

variables: 
  - name: isMain
    value: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]
  - name: isMaster
    value: $[eq(variables['Build.SourceBranch'], 'refs/heads/master')]
  - name: isDevelop
    value: $[eq(variables['Build.SourceBranch'], 'refs/heads/develop')]
  - name: isFeature
    value: $[startsWith(variables['Build.SourceBranch'], 'refs/heads/feature/')]
  - name: isRelease
    value: $[startsWith(variables['Build.SourceBranch'], 'refs/heads/release/')]
  - name: isHotFix
    value: $[startsWith(variables['Build.SourceBranch'], 'refs/heads/hotfix/')]

作为环境变量 在脚本中。使用环境变量时,. 会被替换为 _

trigger:
- none

pool:
  vmImage: ubuntu-latest

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      write-host $env:BUILD_SOURCEBRANCH
      write-host $env:BUILD_SOURCEBRANCHNAME
    pwsh: true

- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |
      echo $BUILD_SOURCEBRANCH
      echo $BUILD_SOURCEBRANCHNAME
英文:

If you are trying to get the branch that the build was triggered from there are some built in pipeline variables you can use without needing to write a script

Build.SourceBranch which will return something like refs/heads/main or refs/heads/feature/tools

Build.SourceBranchName which will return main or tools

you can use these in your YAML in various ways

As variables

$(Build.SourceBranch)
$(Build.SourceBranchName)

As expressions

variables: 
  - name: isMain
    value: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]
  - name: isMaster
    value: $[eq(variables['Build.SourceBranch'], 'refs/heads/master')]
  - name: isDevelop
    value: $[eq(variables['Build.SourceBranch'], 'refs/heads/develop')]
  - name: isFeature
    value: $[startsWith(variables['Build.SourceBranch'], 'refs/heads/feature/')]
  - name: isRelease
    value: $[startsWith(variables['Build.SourceBranch'], 'refs/heads/release/')]
  - name: isHotFix
    value: $[startsWith(variables['Build.SourceBranch'], 'refs/heads/hotfix/')]

As environment variables in a script. With environment variables . is replaced with _

trigger:
- none

pool:
  vmImage: ubuntu-latest

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      write-host $env:BUILD_SOURCEBRANCH
      write-host $env:BUILD_SOURCEBRANCHNAME
    pwsh: true

- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |
      echo $BUILD_SOURCEBRANCH
      echo $BUILD_SOURCEBRANCHNAME

答案2

得分: 0

最简单的方式是使用内置变量: Build.SourceBranch 或 Build.SourceBranchName (如果只需要名称,不需要引用)。

英文:

the easiest way to get what you desire is using the built-in variable: Build.SourceBranch or Build.SourceBranchName (if you only need the name, not ref)

https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services

huangapple
  • 本文由 发表于 2023年7月6日 21:28:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定