Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
If statement Jenkins管道多次分配变量_If Statement_Jenkins_Jenkins Groovy - Fatal编程技术网

If statement Jenkins管道多次分配变量

If statement Jenkins管道多次分配变量,if-statement,jenkins,jenkins-groovy,If Statement,Jenkins,Jenkins Groovy,如果在一个脚本块中,是否可以在内部多次重新分配变量值? 我有一个脚本块,需要将变量值传递到不同的环境: script { if (env.DEPLOY_ENV == 'staging') { echo 'Run LUX-staging build' def ENV_SERVER = ['192.168.141.230'] def UML_SUFFIX = ['stage-or'] sh 'ansible-playbook n

如果在一个脚本块中,是否可以在内部多次重新分配变量值? 我有一个脚本块,需要将变量值传递到不同的环境:

script {
    if (env.DEPLOY_ENV == 'staging') {
        echo 'Run LUX-staging build'
        def ENV_SERVER = ['192.168.141.230']
        def UML_SUFFIX = ['stage-or']
        sh 'ansible-playbook nginx_depl.yml --limit 127.0.0.1'
        
        echo 'Run STAGE ADN deploy'
        def ENV_SERVER = ['192.168.111.30']
        def UML_SUFFIX = ['stage-sg']
        sh 'ansible-playbook nginx_depl.yml --limit 127.0.0.1'                      
        
        echo 'Run STAGE SG deploy'
        def ENV_SERVER = ['stage-sg-pbo-api.example.com']
        def UML_SUFFIX = ['stage-ba']
        sh 'ansible-playbook nginx_depl.yml --limit 127.0.0.1'                                              
    }
}
但我在第二个变量赋值的Jenkins作业中收到一个错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 80: The current scope already contains a variable of the name ENV_SERVER
@ line 80, column 11.
                    def ENV_SERVER = ['192.168.111.30']
         ^

WorkflowScript: 81: The current scope already contains a variable of the name UML_SUFFIX
 @ line 81, column 11.
                    def UML_SUFFIX = ['stage-sg']
         ^

或者,如果脚本块的一部分中有多个赋值,则可以使用任何其他方法。

使用
def
定义变量。这只在第一次通话时需要。 因此,在其他通话中删除def应该是可行的

script {
  if (env.DEPLOY_ENV == 'staging') {
      echo 'Run LUX-staging build'
      def ENV_SERVER = ['192.168.141.230']
      def UML_SUFFIX = ['stage-or']
      sh 'ansible-playbook nginx_depl.yml --limit 127.0.0.1'
      
      echo 'Run STAGE ADN deploy'
      ENV_SERVER = ['192.168.111.30']
      UML_SUFFIX = ['stage-sg']
      sh 'ansible-playbook nginx_depl.yml --limit 127.0.0.1'                      
      
      echo 'Run STAGE SG deploy'
      ENV_SERVER = ['stage-sg-pbo-api.example.com']
      UML_SUFFIX = ['stage-ba']
      sh 'ansible-playbook nginx_depl.yml --limit 127.0.0.1'                                              
  }
}

变量的作用域仅限于if块,因此您无法在该块之外访问它们。

!谢谢你的信息!