简体   繁体   中英

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

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'

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