Remi Serriere
Remi Serriere

Reputation: 19

Is it possible to use a regex with Jenkinsfile "when changeset" conditions

We have a jenkinsfile in a gitlab repo and I would like to avoid a few stages depending on gitlab changeset. Basically there is no need to build and push our container images when only K8S conf files have been modified. Let's say there are 4 folders in the repo:

First I tried the following, but changing files in dockerimages and k8s folders on the same commit would be considered as a valid condition and steps would be avoided... Which is not what we want of course.

when {
   not {
      changeset "**/k8s/**" 
   }
   not {
      changeset "Monitoring/**"
   }
   not {
      changeset "
   }
}

So I came up with this, however it looks like the regex is not been evaluated properly and steps are always being avoided whatever the changes. So either the regex is wrong, or regex are not permitted in changeset conditions?

when {
   anyOf {
      changeset '^((?!velero|k8s|monitoring).)*$'
   }
}

Of course we could use a "anyOf" condition listing all the folders we WANT to trigger a build and push, but this is less flexible as we have more folders to build than folders to avoid, and adding a new folder to build would need an update to the jenkinsfile as well.

If you guys have an idea... Please let me know :)

Upvotes: 0

Views: 1380

Answers (2)

Alexander Pletnev
Alexander Pletnev

Reputation: 3498

As of 2023, changeset allows to use different types of pattern evaluation:

The optional parameter comparator may be added after an attribute to specify how any patterns are evaluated for a match:

  • EQUALS for a simple string comparison
  • GLOB (the default) for an ANT style path glob case insensitive (this can be turned off with the caseSensitive parameter).
  • REGEXP for regular expression matching

For example: when { changeset pattern: ".TEST\\.java", comparator: "REGEXP" } or when { changeset pattern: "*/*TEST.java", caseSensitive: true }

Upvotes: 0

Remi Serriere
Remi Serriere

Reputation: 19

Never mind... Found another way around. I was planning to bypass our pipeline steps based on the commit content changes but the following gives more flexibility, allowing developers to bypass those steps when typing "#DNB" (case insensitive) in any commit comment.

when { 
    not {
        changelog '^.*(?i)#DNB.*$'
    } 
}

Upvotes: 1

Related Questions