如何在Jenkins中使用groovy环境变量在Jenkins管道中传递bat命令

如何在Jenkins中使用groovy环境变量在Jenkins管道中传递bat命令

我想%region%COGAPP%env%:10000根据 Jenkins 参数选择连接变量 %dispatcher% =。region传递正确但env实际上不是。env有一个额外的循环,因此它返回特定的输出(例如,如果在 UI 中选择了 Dev,则返回 901)。

for (b in environmentGroup) {
                            String envX = ""
                                if ("$b" == "Dev"){
                                        envX = "901"
                                    }
                                else if ("$b" == "Test"){
                                        envX = "801"
                                    }
                                else if ("$b" == "Prod"){
                                        envX = "101"
                                    }

Jenkins Cosole 输出返回dispatcher = USOHCOGAPP:10000但它应该返回dispatcher = USOHCOGAPP901:10000

[Cube@2] Running batch script

C:\Jenkins\workspace\Pipelines\Cube@2>rem @echo off  

C:\Jenkins\workspace\Pipelines\Cube@2>set /p region= USOH 
USOH
C:\Jenkins\workspace\Pipelines\Cube@2>set /p env= 901 
901
C:\Jenkins\workspace\Pipelines\Cube@2>set /p cube= abc     
abc    
C:\Jenkins\workspace\Pipelines\Cube@2>set dispatcher = USOHCOGAPP:10000 

完整代码

/**************** Variable Definition ****************/
// Create string array of environments based on user input
def String[] environmentGroup = "${params.environment}".split(',')

// Create string array of regions based on user input
def String[] regionGroup = "${params.region}".split(',')

// Create string array of regions based on user input
def String[] cubeGroup = "${params.cube}".split(',')

/**************** Pipeline Definition ****************/
pipeline {
    // Allows pipeline to run on any available node if not explicitly defined 
    agent any
    // Create separate stages in build pipeline
    stages {
        // Define first stage in pipeline: review user inputs and output to Jenkins console
        stage ('Parameter Validation') {
        // Define steps in the 'Parameter Validation' stage
            steps {
                script {
                    // Error handling - if any parameter is left blank, the build will automatically fail
                    if ("${params.environment}" == '' || "${params.region}" == '' || "${params.cube}" == '') {
                        error("Build failed because a required parameter was not selected.\n  All available parameters in this pipeline are required.")
                    }
                }
                // Output parameters selected at time of build to Jenkins console
                echo '------------------------------------------------------------------------------------------------------------------'
                echo "Checking environment input... user selected: ${params.environment}"
                echo "Checking region input... user selected: ${params.region}"
                echo "Checking cube input(s)... user selected: ${params.cube}"
                echo '------------------------------------------------------------------------------------------------------------------'
            }
        }
        // Define second stage in pipeline: access NetApp and download file to local workspace
        stage ('Execute Script') {

            // Define steps in the 'Execute Script Stage'
            steps {

                // Script block to encapsulate three nested loops
                script {

                    // Loop through regions in the regionGroup String Array
                    for (a in regionGroup) {

                        // Loop through regions in the environmentGroup String Array
                        for (b in environmentGroup) {
                            String envX = ""
                                if ("$b" == "Dev"){
                                        envX = "901"
                                    }
                                else if ("$b" == "Test"){
                                        envX = "801"
                                    }
                                else if ("$b" == "Prod"){
                                        envX = "101"
                                    }

                            // Loop through regions in the serverGroup String Array
                            for (x in cubeGroup) {                                      
                                    // Two echos: one for spacing in the log and the other to detail what will occur in each block 
                                    echo "$x --------------------------------------------------------------------------------------------------\n"
                                    echo "The script execution in this block will refresh the $x cube in the $b $a environment."

                                    // Node label determines where the script will execute
                                    node(label: "DEFRCOGAPP901") {
                                        // Execute Windows Batch File
                                        bat """
                                            rem @echo off 
                                            set /p region= ${params.region}
                                            set /p env= $envX
                                            set /p cube= $x    
                                            set dispatcher = %region%COGAPP%env%:10000
                                            echo %dispatcher%
                                            """
                                    } 

                            }
                        }
                    }
                }
            }
        }
    }
}   

相关内容