首先,我将管道完全编写为 groovy,以便签入 git。请不要提供任何gui必要的解决方案。我的问题陈述是:
从文件中提取变量并将其设置为等于 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 从属性文件中获取值
- 将字符串解析为属性
如果您有一个从文件中提取变量并将其设置为等于常规对象的工作示例,那么它将解决我的问题。