简体   繁体   English

Jenkins - 传递参数给 groovy function

[英]Jenkins - Passing parameter to groovy function

I've been trying to call another groovy function with parameters inside my pipeline without any luck.我一直在尝试用我的管道中的参数调用另一个 groovy function 但没有任何运气。
The groovy function I am passing the parameters to consists of a bash script, but this bash script does not recognize the parameter(s) I am passing to it. groovy function 我将参数传递给包含一个 bash 脚本,但是这个 bash 脚本无法识别我传递给它的参数。 If however the parameter passed by i defined as a parameters {} in the pipeline, then it works, but I do not want this.但是,如果我传递的参数在管道中定义为parameters {} ,那么它就可以工作,但我不希望这样。

PROBLEM:问题:
The shell script does not recognize/understand the arguments, the variables are empty, no value. shell 脚本无法识别/理解 arguments,变量为空,没有值。

pipelineJenkins.groovy pipelineJenkins.groovy

def call {
  pipeline {
    parameters { 
      string (name: VAR1, defaultValue: "Peace", description: '' }  <--- This works, but not beneficial
      string (name: VAR2, defaultValue: "Space", description: '' }  <--- This works, but not beneficial
    stages {
      stage ('Run script') {
        steps {
          groovyFunction("${VAR1}", "${VAR2}")
          groovyFunction("Peace", "Space") <--- WHAT I WANT
        }
      }
    }
  }
}

groovyFunction.groovy groovyFunction.groovy

def call(var1, var2) {
  sh 'echo MY values ${var1} and ${var2}'
  sh "echo MY values ${var1} and ${var2}" <-- Works using double quotes, this messes up sed and for-loops...
}

OUTPUT FROM PIPELINE WITH PARAMETERS: OUTPUT 来自带有参数的管道:

MY values Peace and Space

OUTPUT FROM PIPELINE WITHOUT PARAMETERS: OUTPUT 来自没有参数的管道:

MY values and

I have tried using the environment{} keyword as suggested in my previous question, without any luck.我已经尝试按照我上一个问题中的建议使用environment{}关键字,但没有任何运气。 Jenkins - environment Jenkins - 环境

I am aware that there are similar issues out there:我知道那里有类似的问题:

NOTE: This is close to a duplicate of my asked quesiton Shell parameter Jenkins注意:这接近于我提出的问题Shell 参数 Jenkins的副本

Thanks.谢谢。

UPDATED更新

I have updated the answer to use environment variable without having environment {}我已经更新了答案以在没有environment {}的情况下使用环境变量

Use environment variables like the ones i have used here (i refactored your code a little bit):使用像我在这里使用的那样的环境变量(我稍微重构了你的代码):

def callfunc() {
  sh 'echo MY values $VARENV1 and $VARENV2'
}

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

env.VARENV1 and env.VARENV2 are the environment variables i have used here inside script{} . env.VARENV1env.VARENV2是我在script{}中使用的环境变量。 You can assign values to them.您可以为它们赋值。

This is my new output:这是我的新 output:

在此处输入图像描述

I really hope it helped.我真的希望它有所帮助。

UPDATES FOR USING FOR LOOP使用 FOR 循环的更新

Using triple single quotes for shell script for loop and adding grrovy variable to it:对 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: OUTPUT:

在此处输入图像描述

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

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