2

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.

user1098490
  • 478
  • 1
  • 4
  • 16

1 Answers1

2

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:

enter image description here

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:

enter image description here

Aditya Nair
  • 514
  • 1
  • 9
  • 18
  • Hi Aditya Nair, thank you for your answer. The main problem I had was to use pass variables to a groovy function which is a bash script. The script that I use is a mixture of groovy-variables and bash-variables. I managed to solve this using the triple-double-quotes `"""` and escaping every bash-variable `\$`. – user1098490 Aug 24 '20 at 07:11
  • I see...I dont know about you but that sounds like a more hectic workaround. The above solution helps you to use groovy variable with `env` prefix that can be used globally throughout the whole pipeline, not to mention even inside the bash function `callfunc` with single quotes in the script. If you feel like the above solution is a more viable solution, please do give it a try and accept or upvote the answer for the same. Thank you – Aditya Nair Aug 24 '20 at 08:18
  • I can see you are able to use single quotes `'`, but how will this be if you have shell-variables too, e.g `for in`. Will groovy be able to differentiate these? – user1098490 Aug 24 '20 at 08:39
  • Please check out the answer...i have updated it as per your requirement, you can use triple single quotes and still be able to use the environment variable. – Aditya Nair Aug 24 '20 at 08:56
  • sorry i left out using the shell variable part...i have updated it...it seems using `export` prefix before variable helps to distinguish it from groovy ones. – Aditya Nair Aug 24 '20 at 09:26