Fixing Git issues caused by shallow fetch in Azure DevOps Pipelines

Previously I described how to implement Build and Release pipelines in Azure DevOps for Power Platform solutions.

These pipelines work successfully in the Azure DevOps organisation I have for personal use. I can run them again and again without issue. However, when I came to implement the Build pipeline in the Azure DevOps organisation for my workplace I ran into an odd error.

The final task in the Build pipeline commits the unmanaged Power Platform solution into Git.

The commit is achieved using a command line task:
But the log showed the following error:
It seems that there’s a problem with the git checkout line. Hmmm…

Further down in the log I saw:
So, the git checkout and the git push also seem to be failing for some reason. Why is this?

Let’s compare this behaviour with the log from a successful run:
From this log we can see that a local branch (main) was created to track the remote origin/main branch. We can see the commit and then the successful push from the local branch to the remote.

In the unsuccessful run we don’t seem to have a local branch to track origin/main, instead we’re told that “refspec main does not match”.

And it appears that we’re not trying to push to origin/main, but a specific revision: origin/ed57837b943de52f1051ec0f955d7bb5f0a9b901. So, we’re not working against the branch that we thought we were.

At the beginning of our YAML pipeline we have the agent perform a git checkout, like so:
Let’s look at the output for this checkout. In my personal Azure DevOps organisation I can see:
Then:
But in the Azure DevOps log for my workplace, I can see:
For some reason “checkout: self” seems to be doing a shallow fetch. So that we’re then working against the last revision (origin/ed57837b943de52f1051ec0f955d7bb5f0a9b901) and not the Head (origin/main).

Microsoft’s YAML documentation does NOT describe this as the default behaviour. In fact, it gives specific instructions on how to perform a shallow fetch, if required.

For 99% of Azure DevOps pipelines this will make no difference. But in pipelines where we’re subsequently working with Git, it means that checkout and push will fail.

Fortunately, the fix is simple. We perform our own fetch against the head:
And magically everything works as it did before. The pipeline commits the Power Platform solution to the Head of the repository as expected.

So far, I’ve found no explanation for this inconsistent behaviour. The “checkout: self” works as it always did in my personal Azure DevOps organisation – the added “git fetch” is unnecessary there but doesn’t hurt. Whereas it is required for success in the pipeline in the Azure DevOps organisation for my workplace. How odd.

Hope this helps!

Comments