Not able to access the variable from a grovvy file

I am getting the below error when i try to access a varble from a groovy file
Also: org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: cd91fd2e-c88b-4bcf-80e8-c1062b0386ca
groovy.lang.MissingPropertyException: No such field found: field java.lang.String servers

I am able to print the same. using

steps {
                script {
                    def sslConfig = env.SSL_CONFIG
                    echo "Loaded sslConfig: ${sslConfig}"
                    
                    // Debugging: Print loaded sslConfig object
                    echo "Loaded sslConfig in Deploy Renewed Certs stage: ${sslConfig}"
                    
                    if (!sslConfig) {
                        echo "Error: sslConfig is null"
                        currentBuild.result = 'FAILURE'
                        return
                    }

                    def servers = sslConfig.servers
                    def source_paths = sslConfig.source_paths
                    def domains_list = []

                    servers.each { server, domains ->
                        domains_list = domains.collect { "\"${it}\"" }.join(',')

This is my groovy file

def servers = [
//servers
]

def source_paths = [
    //paths
]

Hello @babayaga, and welcome to this community. :wave:

In your Groovy file, you have defined servers and source_paths as local variables. If I’m not mistaken, these variables are only accessible within the script where they are defined.
If you’re trying to access these variables from a Jenkins pipeline script, they won’t be available, hence the MissingPropertyException. At least, that’s my understanding.

To make these variables accessible from your Jenkins pipeline script, I think you could return them from your Groovy script.

def servers = [
    //servers
]

def source_paths = [
    //paths
]

return [servers: servers, source_paths: source_paths]

Then, in your Jenkins pipeline script, you could load the Groovy script and hopefully access the servers and source_paths variables with something like this:

steps {
    script {
        def sslConfig = load 'path/to/your/groovy/file.groovy'
        echo "Loaded sslConfig: ${sslConfig}"
        
        // Debugging: Print loaded sslConfig object
        echo "Loaded sslConfig in Deploy Renewed Certs stage: ${sslConfig}"
        
        if (!sslConfig) {
            echo "Error: sslConfig is null"
            currentBuild.result = 'FAILURE'
            return
        }

        def servers = sslConfig.servers
        def source_paths = sslConfig.source_paths
        def domains_list = []

        servers.each { server, domains ->
            domains_list = domains.collect { "\"${it}\"" }.join(',')
        }
    }
}

Hello @poddingue thanks a bunch mate for the suggestion but i am trying return the values but the issues is still there.

StringBuilder iniContent = new StringBuilder()

// Append servers
servers.each { server, domains ->
    iniContent.append("[$server]\n")
    domains.each { domain ->
        iniContent.append("$domain\n")
    }
    iniContent.append("\n")
}

// Append source_paths
source_paths.each { domain, path ->
    iniContent.append("[$domain]\n")
    iniContent.append("path=$path\n\n")
}

println iniContent.toString()

// Return servers and source_paths
return [
    servers: servers,
    source_paths: source_paths
]
1 Like