groovy 获取jenkins参数中的可用工件版本

laximzn5  于 2022-11-01  发布在  Jenkins
关注(0)|答案(1)|浏览(170)

我想在Jenkins构建中有一个能力,可以获取可用的工件版本(我使用工件来存储压缩在.zip中的代码),并将它们作为下拉列表,这样我就可以选择我想在此构建中使用的版本。
你能给予一个例子说明如何做到这一点的最佳方法吗?

emeijp43

emeijp431#

为此,我使用jenkins插件Active Choices并在作业中添加React参数。这使我能够选择使用什么环境,并基于它从artifactory中获取可用工件

作业配置:

在“活动选择”参数中使用的Groovy代码:

import groovy.json.JsonSlurper
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Pattern pattern = Pattern.compile("((?:develop|master|function)_(?:latest|[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+).*)")

def repository_dev = "https://repo.nibr.novartis.net/artifactory/api/storage/nibr-generic/intuence_discovery/idaw_health_checker/develop/"
def repository_tst = "https://repo.nibr.novartis.net/artifactory/api/storage/nibr-generic/intuence_discovery/idaw_health_checker/release/"
def repository_prd = "https://repo.nibr.novartis.net/artifactory/api/storage/nibr-generic/intuence_discovery/idaw_health_checker/master/"

try {
    if (DEPLOY_TO == "dev") {
        versions = "curl -s $repository_dev"
    }
    else if (DEPLOY_TO == "tst") {
        versions = "curl -s $repository_tst"
    }
    else if (DEPLOY_TO == "prd") {
        versions = "curl -s $repository_prd"
    }

    def proc = versions.execute()
    proc.waitFor()
    def output = proc.in.text
    def jsonSlurper = new JsonSlurper()
    def artifactsJsonObject = jsonSlurper.parseText(output)
    def dataArray = artifactsJsonObject.children
    List<String> artifacts = new ArrayList<String>()
    for(item in dataArray) {
        Matcher m = pattern.matcher(item.uri)
        while (m.find()) {
            artifacts.add(m.group());
        }
    }
    return artifacts
} 

catch (Exception e) {
    return ["There was a problem fetching the artifacts", e]
}

相关问题