jenkinsgradlecontinuous-integrationcontinuous-deployment

Not able to read gradle.properties in jenkins pipeline job


i am running jenkins pipeline job with gradle. I have a requirement to get the the property value mentioned the gradle.properties, how can i get it


Solution

  • Use Jenkins declarative pipeline built-in readFile() to read files from the workspace.

    Let's say your gradle.properties contains

    version=1.2.3-SNAPSHOT
    

    To read the version property from the file do this in your Jenkinsfile:

    pipeline {
        stages {
            stage("read file from workspace") {
                steps {
                    checkout scm
    
                    script {
                        String content = readFile("gradle.properties")
    
                        Properties properties = new Properties()
                        properties.load(new StringReader(content))
    
                        echo "property 'version' has value '${properties.version}'"
                    }
                }
            }
        }
    }
    

    This might fail when executed due to missing permission to execute arbitrary code, depending on your Jenkins setup. You'll may receive errors like this:

    Scripts not permitted to use method java.util.Properties load java.io.Reader. Administrators can decide whether to approve or reject this signature.
    

    Read more about this topic here: Script Approvals.

    Once approved, the property can be read in:

    [Pipeline] readFile
    [Pipeline] echo
    20:55:16 property 'version' has value '1.2.3-SNAPSHOT'