2016-08-15 82 views
3

開始我寫的管道完全作爲groovy被檢入到混帳。請不要提供任何gui必要的解決方案。我的問題陳述是:詹金斯管道提取和設置常量屬性文件中的變量

從文件中提取變量並將其設置爲與常規對象相等。

我已經試過

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 
} 

我試過其他建議無濟於事。特別是以下兩個。

如果你從文件中提取的變量並設置它等於常規的對象會解決我的問題的工作示例。

回答

4

解決:

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