无法读取jenkins管道作业中的gradle.properties

问题描述 投票:1回答:1

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

jenkins gradle continuous-integration continuous-deployment
1个回答
0
投票

使用内置的[J0]的Jenkins声明性管道从工作空间中读取文件。

假设您的readFile()包含

gradle.properties

要从文件中读取version=1.2.3-SNAPSHOT 属性,请在version中执行此操作:

Jenkinsfile

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

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}'"
                }
            }
        }
    }
}

在此处阅读有关此主题的更多信息: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
© www.soinside.com 2019 - 2024. All rights reserved.