0

I'm looking for a way to create an artifact that I can attach to a deployment pipeline that only contains the files that were changed in the commits that triggered this build.

What I have is a repo that has change scripts for database objects, so I want to package up only the change scripts for the last commit into a zip file and attach it to the build outputs. That way I can take zip file and apply each of the files on top of the database, this will be done later in a different step, right now I'm just trying to get all of the files that were changed.

Editted I have created the following step in the YAML file based on the comments below

- powershell: |
   #get the changed template
   echo "git diff-tree --no-commit-id --name-only -r $(Build.SourceVersion)"
   $a = git diff-tree --no-commit-id --name-only -r $(Build.SourceVersion)  

   #assign the filename to a variable        
   echo "Files"
   echo "##vso[task.setvariable variable=fileName]$a" 
- powershell: |
   #Print Files
   $fileName: echo "$env:fileName"

Below is the result, you can see that no files are changed. Here I changed the Readme file, which triggered the build. enter image description here

Paul Cavacas
  • 4,194
  • 5
  • 31
  • 60

2 Answers2

1

Not sure if this would help you, but hopefully will point in the right direction.

Assuming you have Git as source control. Have you considered to query those changes using Git instead? (I haven't tried, but I bet you'll be able to find in the pipeline metadata which merge triggered the build, and then use it to query Git for the file changes.

Have a look at this question in Stackoverflow

Hope it will help.

JdMV
  • 116
  • 4
0

If you are using Git version control, you could try to add a script task to get the changed file names in your pipeline, copy them to artifact directory and then publish them.

It is easy to get the changed files using git commands git diff-tree --no-commit-id --name-only -r commitId. When you get the changed file's name, you need to assign it to a variable using expression ##vso[task.setvariable variable=VariableName]value. Then you can use this variable in the copy and publish task.

You can check below yaml pipeline for example:

    - powershell: |
       #get the changed template
       $a = git diff-tree --no-commit-id --name-only -r $(Build.SourceVersion)  
       
       #assign the filename to a variable        
       echo "##vso[task.setvariable variable=fileName]$a" 

    - powershell: |
       echo "$env:fileName"
Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39