简体   繁体   中英

Jenkins conditionalstep groovy script

I tried to use this Conditional multi step plugin and wrote the grrovy script for DSL however when i bootstrap using this code, the steps listed are outside before the conditional block, what am i doing wrong here?

Ref: https://jenkinsci.github.io/job-dsl-plugin/#method/javaposse.jobdsl.dsl.helpers.step.StepContext.conditionalSteps

Code:

def configSeed(environment, slaveLabel) {
   { it ->
      parameters {
        stringParam('BUILD_REQUIRED', 'true', '');
    }
    scm {
        git {
            remote {
                name('origin');
                url('xyz');
                refspec('$GERRIT_REFSPEC');
                credentials('xyz');
            }
            branch('$GERRIT_BRANCH');
            strategy {
                gerritTrigger();
            }
        }
    }
    steps {
        conditionalSteps {
            condition {
                stringsMatch('${BUILD_REQUIRED}', 'true', false)
            }
            runner('Fail')
            steps {
                environmentVariables {
                    envs(environment);
                }
                batchFile('''
                    call npm install
                    ''');                   
                batchFile('''
                    call mkdir buildArchive
                    ''');
            }
        }
    }
    publishers {
        wsCleanup {
            includePattern('build/**')
        }
    }
    wrappers {
        preBuildCleanup();
        timeout {
            noActivity(300);
            abortBuild();
        }
    }
    label(slaveLabel);
 }
};

So jenkins job which gets created is shown as

npm install
mkdir buildArchive

Conditional step

Instead of

Conditional step
{
   npm install
   mkdir buildArchive
}

What i am doing wrong here?

When you generate a job with your steps configuration

job('foobar') {
    steps {
        conditionalSteps {
            condition {
                stringsMatch('${BUILD_REQUIRED}', 'true', false)
            }
            runner('Fail')
            steps {
                environmentVariables {
                    envs(FOO: 'bar', TEST: '123')
                }
                batchFile('call npm install')               
                batchFile('call mkdir buildArchive')
            }
        }
    }
}

the result seems fine:

在此处输入图片说明

In my experience, when you have any issues with the Job DSL syntax and its results, a good approach is to diff the config.xml of your job:

  • Manually configure your job, as you want it to be, and save the config.xml
  • Generate your job locally and save the config.xml as well
  • Diff the two configuration files and look out for the exact XML tag order
  • Then trial and error to see what is going on when you change the Job DSL script

When I started to use the Job DSL with no experience with groovy - especially closures ;) - I had a lot of simmilar issues when I tried to extract job configuration blocks into methods like in your case. This is what helped me to understand what is really going on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM