Jenkins-pipeline从Groovy中的属性文件中提取和设置变量 [英] Jenkins-pipeline Extract and Set variables from properties file in groovy

查看:3460
本文介绍了Jenkins-pipeline从Groovy中的属性文件中提取和设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我将整个管道写成groovy来检查git。请不要提供任何gui必要的解决方案。我的问题语句是:

从文件中提取一个变量并将其设置为一个常规对象。



我试过的东西

  def SERVICE_MAJOR_VERSION 
节点{
runGitClone(GIT_REPO_URL,GIT_HASH)
def conf = readFile(gradle.properties)
echo conf
//下面的注释不起作用
// 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 conte nt = readFile file_path
def matcher = content =〜/${string_name}\==(.+)/
if(matcher){
par1 = string_name +='+ matcher [0] [1] +'
新的GroovyShell(this.binding).evaluate(par1)
if(should_print_load){
println par1
}
}
return par1
}

我尝试了其他建议,无济于事。特别是下面两个。



如果您有一个工作示例一个文件中的变量,并将其设置为一个常规对象,它可以解决我的问题。

解决方案

解决方案:

  def content = readFile'gradle.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


To begin i'm writing the pipeline entirely as groovy to be checked in to git. Please do not provide any gui necessary solutions. My Problem statement is:

Extract a variable from a file and set it equal to a groovy object.

What i've tried

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
}

I've tried other suggestions to no avail. Particularly the below two.

If you have a working example of extracting a variable from a file and setting it equal to a groovy object it would solve my problem.

解决方案

SOLVED:

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

这篇关于Jenkins-pipeline从Groovy中的属性文件中提取和设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆