⚠️ Update 2024: For anyone looking at this gist in 2024, we have moved on from using the Pipelines API to instead using tags. Each time we successfully deploy to an environment, we tag the commit with the environment name. When testing for changes, we then compare the current commit being deployed to the tagged commit for the same environment. This has proven to be a bit more robust than using the changes API. The principles are the same though. Let me know in a comment if it's not obvious how to adapt the script.
One devops strategy is to keep infrastructure state and code in a single repository, and deploy them together. It ensures your infrastructure is always in the correct state to match the version of your code.
The downside is that infrastructure deployments are slow, and generally don't change as frequently as code.
This gist shows how to skip an infrastructure deployment task if there are no changes in a particular sub-path of the repository.
It takes advantage of the pipelines API, which can provide a list of all commits since the last successful pipeline execution. This ensures that it works even when multiple commits are pushed at once, or when the build with the infrastructure change failed. The pipelines API does the hard work of working out which commits need checking.
https://gist.github.com/Myrddraal/f5a84cf3242e3b3804fa727005ed2786#file-azure-pipelines-yaml-L23:
There is an extra ' in the end of this line.