简体   繁体   中英

Jenkins - Passing parameter to groovy function

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:

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

Thanks.

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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