【问题标题】:How to access created artifacts in groovy script for a particular build如何访问在特定构建的 groovy 脚本中创建的工件
【发布时间】:2021-03-23 03:14:03
【问题描述】:

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

【问题讨论】:

    标签: jenkins groovy jenkins-plugins jenkins-groovy


    【解决方案1】:

    我有类似的东西可以找到工件名称。这是一个 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/))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多