11

I'm trying to add conditions to a Jenkins stage so it will only trigger when there's been a change to a .yaml file in the changeset

The code I've ended up trying is:

when { changeset '*.yaml' } 

According to the following site, this should cause the stage to be run when there is a change to a *.yaml file in the changeset, however this stage is always being skipped

Does anyone know where I'm going wrong, or what else I could try?

Thanks!

0

1 Answer 1

2

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] } 
Sign up to request clarification or add additional context in comments.

6 Comments

Looks like this is the way to go! One caveat is that my repository is private, which is causing me issues in the branch stage The Jenkins task is being kicked off when the 'master' branch is being updated, however, is there any chance that this mechanism causes the changeset to be passed into the Jenkinsfile somehow?
I don't fully understand the issue with the private repo/branches. (you can define credentials to the git plugin to clone the repo) Can you explain that a bit more? maybe multibranch pipelines offer an option.
Hi @lvthillo, Can you help me, I have a step checkout and multibranch pipeline. When I add a folder to the changeset like this it doesn't work ( ) : changeset "*/folder1/**"
what if you try changeset pattern instead of changest?
How to specify the folder?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.