WIth Jenkins pipelines, start your job by querying the build changeset, as in here.
See if an when { anyOf { changeset
directive is enough to build if and only when the changeset includes a specific element.
See the pipeline syntax built-in condition
changeset
Execute the stage if the build’s SCM changeset contains one or more files matching the given string or glob. Example: when { changeset "**/*.js" }
As noted by PFudd in the comments:
WARNING: When you create a new branch, the changeset will be empty on the first build. It's also empty if you click "Rebuild".
See "Jenkins Pipeline how to check for an empty changelog"
The Jenkins team is aware of this, and due to the complexity of Git, they don't intend to fix it, because there is no universally-correct way to determine the right parent to compare with: see issue JENKINS-26354.
Reference: stackoverflow.com/questions/70307972/…