Richlewis
Richlewis

Reputation: 15374

Using an external Groovy file with Jenkins pipeline

I have two questions here regarding Jenkins pipeline and Groovy methods. Firstly I have multiple build that share common methods so thought best to have all these in a single class and then import the file for each build.

A snippet from my Groovy script looks like

import groovy.json.JsonSlurperClassic;
import groovy.json.JsonSlurper;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.HashSet;
import java.util.Set;
import java.io.Serializable;

Map get_var() {
      def gradleVars = readFile "${env.WORKSPACE}/gradle-client/gradle.properties"
      Properties properties = new Properties();
      InputStream is = new ByteArrayInputStream(gradleVars.getBytes());
      properties.load(is)

      def sdk_version = "SDKVersion"
      def SDK_VERSION = properties."$sdk_version"

    return [sdk_version: "$SDK_VERSION"}
}

And in my pipeline script I have

def groovyMethod = load("release_pipeline.groovy")
// Call method
groovyMethod.getVar()

The first problem I have is how do I use ${env.WORKSPACE} within my method, and secondly how do I use readFile within my script as I get the error

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: Helpers.readFile()

I am really new to Groovy and Java.

Upvotes: 1

Views: 1498

Answers (1)

Rao
Rao

Reputation: 21359

Would you please try the below :

def getVar() {
  def properties = new Properties()
  File propertiesFile = new File("${System.getenv['WORKSPACE']}/gradle-client/gradle.properties")
  properties.load(propertiesFile.newDataInputStream())


  return [sdk_version: properties.SDKVersion]

}

May be it appears that, you have different method name get_var() and you are trying to use getVar().

And I am not really sure where that error coming from above script

groovy.lang.MissingMethodException: No signature of method: Helpers.readFile()

EDIT: based on OP comment

Please see if the this helps:

def workspace = Thread.currentThread().executable.workspace.toString()

Upvotes: 1

Related Questions