Search code examples
jenkinsjenkins-pipelinejenkins-groovy

Getting OR to work properly on Jenkins Pipeline


I'm trying to disallow certain names to be used on a pipeline. simply put: " If it is name1 or NAME1, ignore - otherwise, proceed" But apparently it is not working properly. (still prints hello, according to the example) It does work if I put "params.project != 'name1' || params.project != 'name1' " Which I really don't understand why.

What am I missing here ?

pipeline {

    agent { label 'master'}
    parameters {
        string(defaultValue: '', description: '', name: 'project')
    }
    stages {
        stage ('project stage') {
            steps {
                script {
                    if (params.project != 'name1' || params.project != 'NAME1')
                        println "Hello"
                    }
                }
            }
        }
    }

It is not duplicate of THIS since I'm trying to understand the logic behind this behavior.

In the end I went with

if (params.project!= 'name') {
  if (params.project!= 'NAME1') { 
  }   
}

But I believe there is a better way than that..

Update:
I found another way, which is to use contains
if (project.contains('name'))


Solution

  • If your objective is to disallow both values from being used for the project parameter, then you need the && logical operator ("and"):

    if (params.project != 'name1' && params.project != 'NAME1')
    

    This will prevent both values for the parameter as desired.

    Alternatively, you could also use a regular expression similar to the following:

    if !(params.project ==~ /name1|NAME1/) // =~ if instead do not want to additionally require type match