I've designed a Declarative Pipeline for my CI job, including some conditional stages related to branch changelog magic words. It's working fine, though trying to establish a flow control in Declarative Pipeline results in a lot of stages which are redundant in some ways.
Due to that, I've decided to try Scripted Pipeline syntax, however I couldn't find any equivalent of "changelog" symbol, which is useful for determining a magic word match in branch changelog.
I was expecting I can move on like this:
//Declarative
pipeline {
agent any
stages {
stage('Dummy') {
when
{
changelog '\\[ci BUILD\\]'
}
steps
{
echo 'Building dummy...'
}
}
}
}
//Scripted
node {
stage('Dummy') {
if (changelog '\\[ci BUILD\\]')
{
echo 'Building dummy...'
}
}
}
However it did not work. Is there any way to set up this?
I've managed to write a Groovy function using regex find operator, tested on Jenkins 2.375.2 with "Pipeline: Supporting APIs" plugin, version 839.v35e2736cfd5c, and it's working correctly. Here is the implementation:
boolean matchChangelog(GString ptrnString)
{
def ptrn = ~"${ptrnString}"
for(i in currentBuild.changeSets)
{
for(j in i.items)
{
if(j.msg =~ ptrn)
{
return true
}
}
}
return false
}
With using this method, This:
when{ expression{ return matchChangelog("pattern") }
is equivalent of this:
when{ changelog 'pattern' }