1

I'm currently trying to atopt my declarative pipeline in the following way:

I have multiple stages, some apply for all branches, some only for specific names.

What I'm now searching for is a way to get a when condition for the existance of a branch like this:

if branch b exist
   checkout branch b
else
   checkout branch a

Sadly I haven't found a solution for it. I'm pretty sure there is a way using the git plugin though.

There's a solution for scripted pipelines. I can't translate them though.

scripted pipeline solution

Can someone help me with this or point me at the right direction?

1

I know this isn't the solution you want to hear, but you're probably going to have to switch to scripted pipelines. Generally speaking, scripted pipelines are more flexible and powerful than declarative pipelines, whereas declarative pipelines are best used for simple, straightforward builds. Once you have some complexity in your build, such as these requirements you're describing here, scripted pipelines become not just superior but necessary.

From the official Pipeline docs:

Declarative limits what is available to the user with a more strict and pre-defined structure, making it an ideal choice for simpler continuous delivery pipelines. Scripted provides very few limits, insofar that the only limits on structure and syntax tend to be defined by Groovy itself, rather than any Pipeline-specific systems, making it an ideal choice for power-users and those with more complex requirements.

1

There's a resolveScm plugin:

checkout resolveScm(source: [$class: 'GitSCMSource',
                             credentialsId: '<credentialsId>',
                             id: '_',
                             remote: 'https://your.git.remote/repository.git',
                             traits: [[$class: 'jenkins.plugins.git.traits.BranchDiscoveryTrait']]],
                    targets: [BRANCH_NAME, 'master'])

Above procedure will look whether $BRANCH_NAME exists, check it out if yes and check out master otherwise.

0
pipeline{
agent any 
stages{ 
  stage(“Tests”) {
             when { expression { branch==“master”} }
              steps { echo “FUT”}
}
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.