跨阶段聚合 Artifactory buildInfo 并上传环境变量

aggregating Artifactory buildInfo across stages and uploading environment variables

我有两个不同的问题。

我正在使用 jenkins 多分支声明式管道。 在其中,我定义了多个阶段。

第 1 期

我在初始化我的环境的某个阶段从 Artifactory 下载了一个。管道的输出在单独的阶段上传到 Artifactory 中的不同区域。 我在 here 中看到 buildInfo 可以附加到。这是上述参考文献中的重点:

Publishing Build-Info to Artifactory

Both the download and upload methods return a build-info object which can be published to Artifactory as shown in the following examples:
def buildInfo1 = server.download downloadSpec
def buildInfo2 = server.upload uploadSpec
buildInfo1.append buildInfo2
server.publishBuildInfo buildInfo1

如何保留第一阶段的构建信息以用作第二阶段调用 publishBuilInfo 的一部分?

第 2 期

发布构建信息和相关工件后,当我查看 Build Browser 尤其是“环境”选项卡时,我发现没有填充任何环境或系统变量。我还应该提到,我已按照参考资料中提供的必要步骤收集环境变量:

buildInfo.env.capture = true
server.publishBuildInfo buildInfo

这让我想到了一个相关问题:在第一阶段对第一个 builInfo 进行收集是否有意义:

buildInfo.env.collect()

第 1 期

有两种方法可以在管道脚本中将多个构建聚合到一个 buildInfo 实例中。

第一个正是您所做的 - 保存从 server.uoloadserver.downkoad[=66 返回的 buildInfo 实例=] 方法,然后使用 buildInfo.append 方法附加(聚合)两个 buildInfo 实例。

第二种方法,这可能是您需要的,是创建一个 buildInfo 实例并将其作为参数发送给 server.uoload server.doenload 方法。这样,您可以将同一个 buildInfo 实例发送到多个上传或下载方法,并让它聚合所有内容。

这里是你如何做的:

def buildInfo = Artifactory.newBuildInfo()
server.download spec: downloadSpec, buildInfo: buildInfo
server.upload spec: uploadSpec, buildInfo: buildInfo
server.publishBuildInfo buildInfo

它对你有什么帮助?

好吧,由于您手动创建了 buildInfo 实例,因此您可以将其定义在两个阶段之上的范围内,这样,所有阶段都可以使用 buildInfo 实例。

这是一个例子:

node {
    // Obtain an Artifactory server instance, defined in Jenkins --> Manage:
    def server = Artifactory.server "SERVER_ID"
    // Create a buildInfo instance, to be used by the stages of this pipeline:
    def buildInfo = Artifactory.newBuildInfo()

    stage ('Upload files to Artifactory') {
        def uploadSpec = """{
            "files": [
                    { "pattern": "/Users/eyalb/.m2/repository/a*a*.jar",
                      "target": "generic-local", 
                      "excludePatterns": ["*SNAPSHOT*"],
                      "flat": "false" 
                    }
                ]
            }"""

        server.upload spec: uploadSpec, buildInfo: buildInfo
    }

    stage ('Collect env vars') {
        buildInfo.env.filter.addExclude("DONT_COLLECT*")

        // By default the filter is configured to exclude "*password*,*secret*,*key*", but since we're overriding this configuration by adding our own exclusion, let's add these excludes:
        buildInfo.env.filter
            .addExclude("*password*")        
            .addExclude("*secret*")        
            .addExclude("*key*")        

        withEnv(['DO_COLLECT_FOO=BAR', 'DONT_COLLECT_FOO=BAR']) {
            buildInfo.env.collect()
        }
    }

    stage ('Access build info env vars') {
        // BAR will printed
        echo buildInfo.env.vars['DO_COLLECT_FOO']

        // null will be printed, because we excluded it.
        echo buildInfo.env.vars['DONT_COLLECT_FOO'] 
    }

    stage ('Set build retention') {
        buildInfo.retention maxBuilds: 1, maxDays: 2, doNotDiscardBuilds: ["3"], deleteBuildArtifacts: true
    }

    stage ('Publish build info') {
        server.publishBuildInfo buildInfo
    }
}

如果您想使用声明式管道,可以通过以下方式在多个阶段之间共享同一个 buildInfo 实例。注意 initBuildInfo() 方法——它只需要被调用一次。以下示例仅包含上述脚本管道示例中的两个阶段:

pipeline {
    agent {
      label "my-agents"
    }
    stages {
      stage('Upload files to Artifactory') {
        steps {
            initBuildInfo()
            def uploadSpec = """{
                "files": [
                        { "pattern": "/Users/eyalb/.m2/repository/a*a*.jar",
                          "target": "generic-local", 
                          "excludePatterns": ["*SNAPSHOT*"],
                          "flat": "false" 
                        }
                    ]
                }"""

            server.upload spec: uploadSpec, buildInfo: buildInfo
        }
      }
      stage('Collect env vars') {
        steps {
            buildInfo.env.filter.addExclude("DONT_COLLECT*")

            // By default the filter is configured to exclude "*password*,*secret*,*key*", but since we're overriding this configuration by adding our own exclusion, let's add these excludes:
            buildInfo.env.filter
                .addExclude("*password*")        
                .addExclude("*secret*")        
                .addExclude("*key*")        

            withEnv(['DO_COLLECT_FOO=BAR', 'DONT_COLLECT_FOO=BAR']) {
                buildInfo.env.collect()
            }
        }
      }
    }
  }

def rtServer, buildInfo
void initBuildInfo() {
    script {
        rtServer = Artifactory.server "JX_ARTIFACTORY_SERVER"
        buildInfo = Artifactory.newBuildInfo()
    }
}

第 2 期

当你 运行:

buildInfo.env.collect()

您要求 Jenkins 现在收集环境变量(在 collect() 方法执行时)并将它们存储在此 buildInfo 实例中。

设置时:

buildInfo.env.capture = true

您要求 Jenkins 在使用此 buildInfo 的每个上传和下载方法中收集环境变量。您可以按如下方式使用它:

def buildInfo = Artifactory.newBuildInfo()
buildInfo.env.capture = true
server.download spec: downloadSpec, buildInfo: buildInfo
server.upload spec: uploadSpec, buildInfo: buildInfo
server.publishBuildInfo buildInfo

注意你应该设置

buildInfo.env.capture = true

执行上传或下载之前。

所以使用的优势:

buildInfo.env.capture = true

您可以在 buildInfo 实例上设置一次,然后从那时起为您收集环境变量。 另一方面,在某些情况下,您希望在管道中的特定点收集环境变量。那就是

buildInfo.env.collect()

派上用场。