简体   繁体   English

Jenkins 管道多行脚本命令作为变量

[英]Jenkins pipeline multiline script command as variable

how can I save a command in a variable and executed anywhere in the stage如何将命令保存在变量中并在舞台的任何地方执行

tried differnt way, but still success尝试了不同的方法,但仍然成功

here is my example这是我的例子

pipeline {
   agent any
   environment {
       myscript = sh '''
       echo "hello"
       echo "hello"
       echo "hello"
       '''
   }
   stages {
       stage("RUN") {
           steps {
               sh "${myscript}" 
           }          
      }
   }
}

you can do it like this.你可以这样做。 Not with a groovy variable but can be more dynamic with groovy function/method不使用 groovy 变量,但可以使用 groovy 函数/方法更加动态

def reusableScript(message) {
    sh """
      echo Hello World
      echo Hi ${message}
    """
}
pipeline {
    agent any;
    stages {
        stage('01') {
            steps {
                script {
                    reusableScript("From ${env.STAGE_NAME}")
                }
            }
        }
        stage('02') {
            steps {
                script {
                    reusableScript("From ${env.STAGE_NAME}")
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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