Jenkins must have first collected the changelog e.g. checkout scm. When you just run checkout scm in a jenkins pipeline it will tell you: ERROR: ‘checkout scm’ is only available when using “Multibranch Pipeline” or “Pipeline script from SCM”.
So add your pipeline in a Jenkinsfile to your repository. My repository contains a file test.yaml and a file Jenkinsfile with this pipeline:
pipeline{ agent any stages{ stage('checkout'){ steps{ git branch: 'master', url: 'https://github.com/xxx/xxx.git' } } stage('test'){ when { changeset "*.yaml" } steps{ echo "The file did change in the last commit (SCM checking)" } } } }
Now it will work. When there was a change in Git in your file you will see:
[Pipeline] { (test) [Pipeline] echo The file did change in the last commit (SCM checking) [Pipeline] } [Pipeline] // stage
And else if there was no change:
[Pipeline] { (test) Stage "test" skipped due to when conditional [Pipeline] }