Search code examples
jenkinsgroovyjenkins-pipeline

Jenkins Pipeline pass String array to function


I use Jenkins pipeline and I want pass String array to local function; i try with a Strinf is ok but not with a String array.

in pipeline::

stage('package') {
    steps {
        executeModuleScripts(["module1", "module2"])
    }
}

at the end of pipeline:

void executeModuleScripts(allModules) {

    allModules.each { module ->
    
        String gitFolder = "${module}"
          
        script {
            stage(module) {
                bat 'xcopy "' + gitFolder + '/foo/*" "temp/PUB/foo" /C /S /I /F /H'
            }
        }

    }   
}

I have this error:

groovy.lang.MissingPropertyException: No such property: operation for class: groovy.lang.Binding

Solution

  • It has to be like :

    Scripted Pipeline

    node {
        stage('package') {
            executeModuleScripts(['01','02'])
        }
    }
    void executeModuleScripts(allModules) {
        allModules.each { module ->
            script {
                stage(module) {
                    // sh "cp -R ${module}/foo/* temp/PUB/foo"
                    bat 'xcopy "' + module + '/foo/*" "temp/PUB/foo" /C /S /I /F /H'
                }
            }
    
        }   
    }
    

    Declarative Pipeline

    void executeModuleScripts(allModules) {
        allModules.each { module ->
            script {
                stage(module) {
                    // sh "cp -R ${module}/foo/* temp/PUB/foo"
                    bat 'xcopy "' + module + '/foo/*" "temp/PUB/foo" /C /S /I /F /H'
                }
            }
    
        }   
    }
    
    pipeline {
        agent any;
        stages {
            stage('package') {
                steps {
                    executeModuleScripts(['A','B'])
                }
            }
        }
    }