如何访问特定构建的 groovy 脚本中创建的工件

How to access created artifacts in groovy script for a particular build

我的 groovy 脚本中有一个阶段使用 getImageVulnsFromQualys 函数生成两个 json 文件 (https://www.jenkins.io/doc/pipeline/steps/qualys-cs/) 脚本完成后,我得到两个 json 文件,其中随机散列作为工件的文件名。我无法静态引用每个文件进行解析,因为每次构建完成时 json 文件的名称都是随机的。我希望能够读取两个 json 文件并在同一阶段或同一脚本的另一阶段解析文件。我希望 Groovy 有一个简单的功能,允许我检索特定构建的所有工件,并且我可以解析每个工件。我检索所有这些工件的最佳方法是什么?

我有类似的东西可以找到工件名称。下面是一个用于查找文件及其内容的 var 步骤的示例:

//getArtifactToConentMap.groovy 

def call(args) {
    def build = args.build
    def regex = args.regex
    def files = build.getArtifacts()
    Map<String, String> artifactNameToContent = [:]
    for (int i = 0; i < files.size(); i++) {
        def f = files[i]
        if (f.name ==~ regex) {
            String artifactName = f.relativePath
            String artifactVirtualFile = build.getArtifactManager().root().child(artifactName)
            String artifactContent = getVirtualFileConent(artifactVirtualFile)
            artifactNameToContent.put(artifactName, artifactContent)

        }
    }
    return artifactNameToContent
}

def getVirtualFileConent(def virtualFile){
    InputStream is = null
    try {
        is = a.open()
        return is.text
    } finally {
        is?.close()
    }
}
    

那么你可以如下使用:

Map<String, String> jsonArtifactsNameToContent = getArtifactToConentMap(build: currentBuild.rawBuild, regex: /.*\.json/))