Azure DevOps yaml pipeline : how to know which branch is being checked out?

Viewed 9172

I currently have my yaml pipeline and my application's source code in two different branches and I was trying find evidence that what is being checked out is indeed the source code's branch and not my pipeline's branch but I see that the checkout call at the end of the git fetch is to a specific commit not to the specified branch name. This is my resources definition:

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

and in one of my steps I do a checkout: RepoName. I was expecting a git checkout UAT after pulling the source code but as said I see a checkout of a specific commit. How can I be sure about branch being checked out?

3 Answers

By default, the build pipeline will check out the single commit that triggered the current pipeline run. When you manual, or via other methods, trigger a pipeline run, the run will check out the latest commit on default branch or the branch you specify.

However, no matter what methods triggers the pipeline run, you can use the predefined build variable Build.SourceBranch or Build.SourceBranchName to get the branch name where the commit is checked out from.

To view more details, you can see "Use predefined variables".

Typical syntax to display is:

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

In addition, you also can go to the root of the local repository, and execute the git branch command. This command will output the name of the current branch. For example:

git branch --show-current

If current branch is master, the output:

master

Note that a number of people have reported that this produces no output

You need to set 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

so it would be

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

In ref property you can set like this: ref: 'refs/heads/UAT'

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

Then in steps, you need to add - checkout: RepoName

Here is my sample you can refer to:

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

In build summary page:

enter image description here

In log :

enter image description here

Related