Jenkins - Passing parameter to groovy function

与世无争的帅哥 提交于 2020-12-13 03:12:35

问题


I've been trying to call another groovy function with parameters inside my pipeline without any luck.
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. If however the parameter passed by i defined as a parameters {} in the pipeline, then it works, but I do not want this.

PROBLEM:
The shell script does not recognize/understand the arguments, the variables are empty, no value.

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

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:

MY values Peace and Space

OUTPUT FROM PIPELINE WITHOUT PARAMETERS:

MY values and

I have tried using the environment{} keyword as suggested in my previous question, without any luck. Jenkins - environment

I am aware that there are similar issues out there:

  • Pass groovy variable to shell script
  • How to assign groovy variable to shell variable
  • Jenkins parameters

NOTE: This is close to a duplicate of my asked quesiton Shell parameter Jenkins

Thanks.


回答1:


UPDATED

I have updated the answer to use environment variable without having 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{}. You can assign values to them.

This is my new output:

I really hope it helped.

UPDATES FOR USING FOR LOOP

Using triple single quotes for shell script for loop and adding grrovy variable to it:

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:



来源:https://stackoverflow.com/questions/63517881/jenkins-passing-parameter-to-groovy-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!