簡體   English   中英

Groovy 參數到 shell 腳本

[英]Groovy parameter to shell script

我一直在嘗試將我的代碼分成兩個不同的文件: callTheFunction.groovytheFunction.groovy

從文件名可以看出:

  • callTheFunction.groovy調用定義在Function.groovy中的theFunction.groovy ,將隨機值作為參數傳入。
  • theFunction是一個 shell 腳本 - 在 groovy function 內 - 它應該使用從callTheFunction傳遞的參數。

問題:
shell 腳本無法識別/理解 arguments,變量為空,沒有值。

theFunction.groovy

def call(var1, var2) {
  sh '''
    echo "MY values $var1 and $var2"
  '''
}

調用TheFunction.groovy

def call {
  pipeline {
    stages {
      stage ('myscript') {
        steps {
          theFunction("Value1", "Value2")
        }
      }
    }
  }
}

OUTPUT 來自管道:

MY values  and

我知道那里有類似的問題:

更新

您可以在沒有environment {}的情況下使用環境變量

使用像我在這里使用的環境變量(我稍微重構了你的代碼)。 對 shell 腳本使用三重單引號進行循環並向其中添加 grrovy 變量:

def callfunc() {
  sh '''
  export s="key"
  echo $s
  for i in $VARENV1 
    do
      echo "Looping ... i is set to $i"
    done
    '''
}

pipeline {
    agent { label 'agent_1' }
    stages {
      stage ('Run script') {
        steps {
            script {
                env.VARENV1 = "Peace"
            }
            callfunc()
        }
      }
    }
}

OUTPUT:

在此處輸入圖像描述

參考: Jenkins - 將參數傳遞給 groovy function

暫無
暫無

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

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