【问题标题】:Azure DevOps yaml pipeline : how to know which branch is being checked out?Azure DevOps yaml 管道:如何知道正在签出哪个分支?
【发布时间】:2020-12-14 14:31:44
【问题描述】:

我目前在两个不同的分支中有我的 yaml 管道和我的应用程序的源代码,我试图找到证据证明被检出的确实是源代码的分支而不是我的管道的分支,但我看到 checkout 调用在git fetch 的结尾是特定提交而不是指定分支名称。这是我的资源定义:

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

在我的一个步骤中,我做了一个checkout: RepoName。我在提取源代码后期待git checkout UAT,但正如我所说,我看到了一个特定提交的结帐。如何确定分支是否已签出?

【问题讨论】:

    标签: azure-devops azure-pipelines azure-pipelines-yaml azure-devops-pipelines


    【解决方案1】:

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

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

    要查看更多详细信息,您可以查看“Use predefined variables”。

    要显示的典型语法是:

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

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

    git branch --show-current
    

    如果当前分支是master,输出:

    master
    

    请注意,许多人报告说这不会产生任何输出

    【讨论】:

    • git branch --show-current 对我不起作用。看起来 git 分支没有产生任何输出。
    • 我也有这个问题,git branch --show-current 没有显示输出。我这样做只是为了解决其他一些非常不透明的问题。非常令人沮丧。
    【解决方案2】:

    你需要设置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 会实现什么,如果不存在会发生什么?谢谢
    • 按照建议完成,但我仍然看到类似 git checkout --progress --force ff8a52b0b7da6599d233d2d6565f085e9344d81f 的内容,甚至增加了日志详细程度,但没有证据表明正在签出哪个分支
    【解决方案3】:

    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
    

    在构建摘要页面中:

    在日志中:

    【讨论】:

    • refs/head/dev而不是简单的dev有什么不同吗?
    • 这里我只是按照文档中的syntax,实际测试结果显示没有区别。
    猜你喜欢
    • 1970-01-01
    • 2020-08-26
    • 2020-05-23
    • 1970-01-01
    • 2021-07-27
    • 2020-08-07
    • 2022-08-02
    • 2022-11-14
    • 1970-01-01
    相关资源
    最近更新 更多