Search code examples
gitazure-devopsazure-pipelinesazure-pipelines-yamlgit-diff-tree

Azure Pipelines: git diff-tree shows empty string


I am trying to get all the committed files on an Azure Build Pipeline. This is the yaml script I'm using:

trigger:
  branches:
    include:
      - swagger_dev
  paths:
    include:
      - swagger_dev/swaggers/*.yaml

variables:
  CLIENT_CREDENTIALS: $(ClientCredentials)

steps:
  - powershell: |
      echo "$(Build.SourceVersion)"
      echo "$(git diff-tree --no-commit-id --diff-filter=d --name-only -r $(Build.SourceVersion))"

When I commit one or more files, the Pipeline correctly echoes the Build.SourceVersion but then echoes an empty output for the git command: pipeline log

How is that possible? I am currently on a branch called swagger_dev and the committed files are in the directory swagger_dev/swaggers. Maybe I should add those informations to the diff-tree command?


Solution

  • git diff-tree command requires at least depth 2 to get the changed files.

    The cause of the issue can be related to the fetch depth of the Pipeline repo.

    By default, the Shallow fetch of the pipeline repo is 1 by default.

    You can try to set the fetchDepth to 0 or >2 in YAML Pipeline.

    For example:

    steps:
        - checkout: self
          fetchDepth: 0
    

    Or you can navigate to YAML Pipeline -> ... -> Triggers -> YAML -> Get sources -> Shallow fetch. You can unselect the option.

    enter image description here

    enter image description here