簡體   English   中英

在jenkins聲明式管道文件中重用groovy腳本

[英]Reusing groovy script in jenkins Declarative pipeline file

有沒有一種方法可以重用一次在Jenkinsfile中加載的groovy腳本。

現在這就是我在做的

            steps {
                script {
                    def util = load("${env.WORKSPACE}/scripts/build_util.groovy")
                    util.runStep1()
                }
            }
            steps {
                script {
                    def util = load("${env.WORKSPACE}/scripts/build_util.groovy")
                    util.runStep2()
                }
            }
            steps {
                script {
                    def util = load("${env.WORKSPACE}/scripts/build_util.groovy")
                    util.runStep3()
                }
            }

我在帶有多個腳本塊的后期構建中再次執行相同的步驟來發送郵件。

有一個更好的方法嗎? 我不能使用共享庫

您可以在頂層聲明變量util ,然后在第一個階段為其分配值,然后再在任何階段使用它。

def util;

pipeline {
   agent any
   stages {
      stage('one') {
        steps {
            script {
                util = load("${env.WORKSPACE}/scripts/build_util.groovy")
                util.runStep1()
            }
        }
      }
      post {
        util.xxxx
      }

      stage('two') {
        steps {
            script {
                util = load("${env.WORKSPACE}/scripts/build_util.groovy")
                util.runStep2()
            }
        }
      }
      post {
        util.xxxx
      }

   }

   post {
        util.xxxx
   }
}

是的,您只需要加載一次腳本。

def util = load("${env.WORKSPACE}/scripts/build_util.groovy")

您可以創建一個階段,然后在其中加載腳本並存儲在變量中,然后執行以下操作:

stage('Environment') {
     agent  { node { label 'master' } }
        steps {
          script {
                def util = load("${env.WORKSPACE}/scripts/build_util.groovy")
               }
            }
         }
post {
        // Things that we want done regardless of pipeline's outcome
        //
        always {

            // Push the overall statistics from all the stages to InfluxDB
            //
            node (LINUX_BUILD_NODE){
                script{
                    //Mail sending function call
                    //
                    util.runStep1()
                    util.runStep2()
                    util.runStep3()                        
                }
            }
        }
    }

您可以在任何階段使用“ util ”來調用不同的函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM