JenkinsPipelineUnit icon indicating copy to clipboard operation
JenkinsPipelineUnit copied to clipboard

Individual "not" conditions seem to have different beahviour than grouping it

Open roDew opened this issue 2 years ago • 0 comments

Jenkins and plugins versions report

Environment
Paste the output here

What Operating System are you using (both controller, and any agents involved in the problem)?

...

Reproduction steps

Hi guys,

I'm creating a pipeline that has a condition like that:

            stage('Create, tag, scan and push the Docker image') {
               when {
                 //1st condition
                   anyOf {
                       expression { return params.PARAM1 }
                       expression { return params.PARAM2 }
                 //2nd condition
                       allOf {
                           branch 'release/**'
                           expression { env.VARIABLE == 'something' }
                       }
                 //3rd condition
                       allOf {
                           not { branch('release/**') }
                           not { changeRequest() }
                       }
                   }
               }
               steps {
                   someCustomMethod(...)
               }
            }

And this is one of my tests I'm doing adding in my tests:

    //This test seems to not react properly to the changeRequest()
    @Test
    void "when all flags are false and not release branch but PR is open then my special method is invoked"()
    {
        def customEnv = ["COMPONENT_OWNER" : "Something", "COMPONENT_NAME" : "else", "VAR_3" : "Not compulsory"]
        addParam("PARAM1", false, true)
        addParam("PARAM2", false, true)
        addEnvVar('CHANGE_BRANCH', 'develop')
        addEnvVar('BRANCH_NAME', 'feature/blah')
        addEnvVar('BUILD_NUMBER', '10')

        myPipeline.call(customEnv)

        assertEquals(false, myspecialmethodIsInvoked)
 
        printCallStack()
        assertJobStatusSuccess()
    }

### Expected Results

I would expect that my "myspecialmethodIsInvoked" is executed only when the branch is not release and when there is not a PR, so in my case that I have a PR open, I would expect not to be executed but with the pipeline as it is, it is all the time executed because of the 3rd condition (I tested the other 2)

### Actual Results

I changed the code of the pipeline by:
                   not {
                       anyOf { 
                           branch('release/**')
                           changeRequest()
                       }
                   }
and it seems to work as I was expected with the branch, but "changeRequest" seems to ignore the setting of the CHANGE_NAME 

but it seems to ignore it.

Am I missing anything?

Anything else?

No response

roDew avatar Jul 15 '22 16:07 roDew