1

I have CI/CD set up using Azure Devops. whenever a merge happens I'm able to build the source code from git repo and push the resulting build into a distribution channel.

For me the requirement is more like , when a merge happens I would like to know which file got changed and I want to know the name of the file and I want to copy the contents of the file and post that as a request payload body to an API.

So my doubts are ?

  • How do I find the name of the file that got changed in the merge ?

  • How do I read the contents of the particular file from git repo where merge happened ?

  • Is there any pre-built REST client plugin available with Azure devops to post the payload to server ?

  • May I write a shell script or something to do all of this but how do I find the files that got changed with latest source code merge ? any particular API azure devops provides to find or just look for last modified date of the file ?

Any suggestions are welcome.

Shiva
  • 545
  • 1
  • 10
  • 41
  • For much of the requirements that you have, it would be easier to get them done if you are an administrator. You can get details on commits and merges when you have webhooks and email notifications enabled. But I think sending the content of files that got changed as a request payload can be detrimental if a particular merge request is quite large ( ie, a major change going in as a part of the merge request) – Kavitha Karunakaran Nov 13 '19 at 08:20
  • By the way, since you are using azure devops, I was wondering if you looked at using azure pipelines to get the last part done... just my two cents – Kavitha Karunakaran Nov 13 '19 at 08:25
  • my two cents :) that's polite Thanks. The changes are going to be minimal with any merge. the new content of the files needs to posted to some API tbh. that's my requirement. I am already looking into that my question is how do I know find which file got changed with this recent merge ? – Shiva Nov 13 '19 at 10:29

1 Answers1

2

You can use run git command in powershell to get the contents of changed files. Below command lists all the changed files in a commit. Check this thread for more information

git diff-tree --no-commit-id --name-only -r $commitID

To get show the file content, you can use below command. Refer to this thread

git show ${commitId}:$file

The whole powershell script example in azure pipeline is like below:

Set-Location -Path "$(Build.Repository.LocalPath)"  #set working folder to git sources folder

$commit = "$(Build.SourceVersion)"  #get the commit hash

$files = git diff-tree --no-commit-id --name-only -r $commit  #list the changed files

ForEach($file in $files){

    $content = git show ${commit}:$file  #get the file content

    (Invoke-RestMethod ......) # you can then invoke your API To send the file content.

}

Check this site to learn more about predefined variables in azure pipeline.

Then you can add a powershell task in your build pipeline to run above scripts.

enter image description here

If you want to use webhook to do this. You can refer to this simple guide about how to create your own webhook server.

Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
  • Thanks for the detailed answer, Let me check this solution and get back to you – Shiva Nov 14 '19 at 12:34
  • Hi@Shiva Did you get a chance to try out above answer? Please let me know how did it go? We can discuss it to work out a better solution. – Levi Lu-MSFT Nov 22 '19 at 10:05
  • Hi thanks for your support :), I will check out this week and let you know. – Shiva Nov 24 '19 at 18:26
  • Hi @Shiva Havenot heard from you for a while, you might have tried out above answer, May i know how did it go? – Levi Lu-MSFT Nov 28 '19 at 09:26
  • Thanks for following up with me appreciated ! I was very busy so couldn't spend time.. I will do when I get free time next week and let you know – Shiva Nov 29 '19 at 16:41