Search code examples
azure-devopsyamlazure-pipelines-yaml

Get modified file list in Azure devops pipeline after trigger by commit/push


I have an Azure DevOps pipeline. Right now, the pipeline triggers when a push is made to the branch feat/ENC_CI.

Have plans to later change it so that the pipeline triggers when any branch merges to 'main' branch. Not sure if this will require me to change the yml to fetch modified files.

Right now, it looks like this

trigger:
  branches:
    include:
      - feat/ENV_CI

steps:
  - checkout: self
  - script: |
      echo "Commit ID: $(Build.SourceVersion)"
      changedFiles=$(git diff  --name-only --relative --diff-filter=AM HEAD^ HEAD)
      echo "Files changed: $changedFiles"
      echo "##vso[task.setvariable variable=changedFiles]$changedFiles"

No matter what I do, it always result in:

fatal: ambiguous argument 'HEAD^': unknown revision or path not in the working tree.

fatal: ambiguous argument 'main': unknown revision or path not in the working tree

fatal: ambiguous argument '@^': unknown revision or path not in the working tree.

fatal: ambiguous argument 'origin/feat/ENV_CI': unknown revision or path not in the working tree.

and others like these resulting from different git commands. WOuld be thankful if someone can help!


Solution

  • steps:
      - checkout: self
        fetchDepth: 2
    
      - task: Bash@3
        name: ModifiedFiles
        inputs:
          targetType: 'inline'
          script: |
            MODIFIED_FILES=$(git diff HEAD HEAD~ --name-only)
            echo $MODIFIED_FILES
    

    Azure DevOps's default shallow fetch is 1. Set fetchDepth to 2 to get the list of the files changed since the last commit.