How To Only Check in Changes In Azure DevOps If More Than X Files Have Changed?

Issue

I’m running an Azure DevOps Pipeline that grabs all configuration changes from a dev server and attempts to check it in. The changes come as a zip file, but get extracted via a custom exe into smaller XML files. This is currently working fine with one exception, the zip file is always different, even though the extracted XML files are not. I do not want to check in the zip file changes if only the zip files have changed (so I only want to check in the changes if more than 2 files are different, since there are two zip files)

I was thinking of trying to use git show –shortstat to get the number of file changes, and if it is two, skip it, but I don’t know how to make it work. Here is my current Pipeline YAML:

# -----------------
# Check solution into Git
# -----------------
- script: |
    COPY "$(Build.ArtifactStagingDirectory)\${{ parameters.solutionName }}.zip" "$(Build.SourcesDirectory)\${{ parameters.solutionGitFolderPath }}\${{ parameters.solutionName }}.zip" /Y
    COPY "$(Build.ArtifactStagingDirectory)\${{ parameters.solutionName }}_managed.zip" "$(Build.SourcesDirectory)\${{ parameters.solutionGitFolderPath }}\${{ parameters.solutionName }}_managed.zip" /Y
    echo commit all changes
    git config user.email "$(Build.RequestedForEmail)"
    git config user.name "$(Build.RequestedFor)"
    git checkout main
    git add --all
    git commit -m "automated"
    echo push code to new repo
    git -c http.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" push origin main
  displayName: 'Commit / Push to Git Repo'

Solution

To answer the question directly to conditionally commit to git repo dependent on the changes on the repo.

Within a powershell script you could run the following command that:

git status --porcelain

Invoking the git command in PowerShell returns an object array of all the changes within the working tree.

Full Script Example

$gitStatusArray = iex "git status --porcelain"

if ($gitStatusArray -and $gitStatusArray -gt 2)
{
    # Changes Detected - more than 2 changes detected.
}
elseif ($gitStatusArray -and $gitStatusArray -le 2)
{
    # Changes Detected - 2 or less changes detected.
}
else
{
    # No changes detected
}

Alternative solutions

  1. (Recommended) Leave the file within the Artifact Staging Directory and point to the file in its original location to the custom exe.
  2. Use a .gitignore file in the root or within the specific folder (solutionGitFolderPath).

Answered By – Davesmall28

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published