无法在詹金斯管道作业中读取gradle.properties

时间:2020-05-07 08:00:47

标签: jenkins gradle continuous-integration continuous-deployment

我正在用gradle运行jenkins管道作业。我需要获取提及gradle.properties的属性值,我该如何获取

1 个答案:

答案 0 :(得分:1)

使用内置readFile()的Jenkins声明式管道从工作区读取文件。

假设您的gradle.properties包含

version=1.2.3-SNAPSHOT

要从文件中读取version属性,请在您的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}'"
                }
            }
        }
    }
}

由于缺少执行任意代码的权限(取决于您的Jenkins设置),执行时这可能会失败。您可能会收到如下错误:

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

在此处了解有关此主题的更多信息:Script Approvals

批准后,可以在以下位置读取属性:

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