簡體   English   中英

Jenkins-pipeline從groovy中的屬性文件中提取和設置變量

[英]Jenkins-pipeline Extract and Set variables from properties file in groovy

首先,我正在將管道編寫為groovy,以便檢入git。 請不要提供任何必要的解決方案。 我的問題陳述是:

從文件中提取變量並將其設置為等於groovy對象。

我試過的

def SERVICE_MAJOR_VERSION
node {
    runGitClone(GIT_REPO_URL, GIT_HASH)
    def conf = readFile("gradle.properties")
    echo conf
    //THE BELOW COMMENT DOESN'T WORK
    //SERVICE_MAJOR_VERSION = loadEnvFromFile("SERVICE_VERSION_MAJOR", "gradle.properties", true, SERVICE_VERSION_MAJOR)
}    

def runGitClone(git_repo_url, git_hash) {
    checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: git_hash]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'WipeWorkspace']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '85572032-4284-4095-9eec-4df70ddfdb68', url: git_repo_url]]]
}

def loadEnvFromFile(string_name, file_path, should_print_load) {
    def par1 = null
    def content = readFile file_path
    def matcher = content =~ /${string_name}\=(.+)/
    if (matcher) {
        par1 = string_name + "='" + matcher[0][1] + "'"
        new GroovyShell(this.binding).evaluate(par1)
            if (should_print_load) {
            println par1
        }
    }
    return par1
}

我嘗試了其他建議無濟於事。 特別是下面兩個。

如果你有一個從文件中提取變量並將其設置為groovy對象的工作示例,它將解決我的問題。

解決了:

def content = readFile 'gradle.properties'

Properties properties = new Properties()
InputStream is = new ByteArrayInputStream(content.getBytes());
properties.load(is)

def runtimeString = 'SERVICE_VERSION_MINOR'
echo properties."$runtimeString"
SERVICE_VERSION_MINOR = properties."$runtimeString"
echo SERVICE_VERSION_MINOR

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM