Azure DevOps YAML 管道: 如何知道正在检出哪个分支?

6

我目前将我的YAML管道和应用程序源代码放在两个不同的分支中。

我试图查找证据,以确定正在检出的是源代码分支,而不是我的管道分支。

我看到git fetch结束时的checkout调用是针对特定提交,而不是指定的分支名称

这是我的资源定义:

resources:
  repositories:
  - repository: RepoName
    type: git
    name: 'MyRepository'  # repository in Azure DevOps
    trigger:
      branches:
        include:
        - UAT

在我的其中一步骤中,我执行了checkout: RepoName操作。

在拉取源代码后,我原本期望执行的是git checkout UAT命令,但我看到实际执行的是特定提交的检出。

如何确定所检出的分支?


请记住,特定的提交哈希是比分支名称更好的标识正在部署的代码的标识符。您甚至可以将多个分支的头指向相同的提交ID,因此仅凭分支名称无法告诉您正在发生什么。具体的提交哈希是精确的。 - Oreo
3个回答

3

默认情况下,构建管道将检出触发当前管道运行的单个提交。当您手动或通过其他方法触发管道运行时,运行将检出默认分支上的最新提交或您指定的分支。

无论使用什么方法触发管道运行,您都可以使用预定义的构建变量Build.SourceBranchBuild.SourceBranchName来获取提交检出的分支名称。

要查看更多详细信息,请参见"使用预定义变量"。

显示典型语法为:

steps:
- bash: echo $(Build.SourceBranch)

另外,你也可以进入本地仓库的根目录并执行 git branch 命令。该命令将输出当前分支的名称。 例如:

git branch --show-current

如果当前分支为 master,则输出:
master

请注意,有许多人报告说这个没有输出结果


2
git branch --show-current 对我不起作用。看起来 git branch 没有产生任何输出。 - Alex
我也遇到了 git branch --show-current 没有输出的问题。我只是为了解决其他令人恼火的不透明问题而这样做。非常令人沮丧。 - Nick.McDermaid

0

你需要设置 ref

resources:
  repositories:
  - repository: string  # identifier (A-Z, a-z, 0-9, and underscore)
    type: enum  # see the following "Type" topic
    name: string  # repository name (format depends on `type`)
    ref: string  # ref name to use; defaults to 'refs/heads/master'
    endpoint: string  # name of the service connection to use (for types that aren't Azure Repos)
    trigger:  # CI trigger for this repository, no CI trigger if skipped (only works for Azure Repos)
      branches:
        include: [ string ] # branch names which will trigger a build
        exclude: [ string ] # branch names which will not
      tags:
        include: [ string ] # tag names which will trigger a build
        exclude: [ string ] # tag names which will not
      paths:
        include: [ string ] # file paths which must match to trigger a build
        exclude: [ string ] # file paths which will not trigger a build

所以它将会是这样

resources:
  repositories:
  - repository: RepoName
    type: git
    name: 'MyRepository'  # repository in Azure DevOps
    ref: 'UAT'
    trigger:
      branches:
        include:
        - UAT

我能请您进一步评论一下您的建议吗?添加ref有什么作用,如果没有它会发生什么?谢谢。 - whatever
1
已按建议操作,但仍然看到类似于 git checkout --progress --force ff8a52b0b7da6599d233d2d6565f085e9344d81f 的内容,并且即使增加日志详细程度,也没有证据表明正在检出哪个分支。 - whatever

0
ref 属性中,您可以像这样设置: ref: 'refs/heads/UAT'
resources:
  repositories:
  - repository: RepoName
    type: git
    name: 'MyRepository'  # repository in Azure DevOps
    ref: 'refs/heads/UAT'
    trigger:
      branches:
        include:
        - UAT

然后在 steps 中,您需要添加 - checkout: RepoName

这是我的示例,您可以参考:

pool:
  vmImage: 'windows-2019'

trigger: none

resources:
      repositories:
      - repository: RepoName
        type: git
        name: '{proName}/{repoName}'  # repository in Azure DevOps
        ref: 'refs/heads/dev'    

steps:
  - checkout: RepoName

在构建摘要页面:

enter image description here

在日志中:

enter image description here


refs/head/dev和简单的dev有什么区别吗? - whatever
这里我只是按照文档中的语法进行操作,实际测试结果没有任何差异。 - Hugh Lin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接