跨阶段汇总Artifactory buildInfo并上传环境变量

时间:2018-09-27 13:07:11

标签: artifactory multibranch-pipeline

我有两个独立的问题。

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

问题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

如何从第一阶段保留buildinfo以便在第二阶段作为对publishBuilInfo的调用的一部分?

问题2

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

buildInfo.env.capture = true
server.publishBuildInfo buildInfo

这带给我一个相关的问题:在第一阶段对第一个builInfo进行收集是否有意义:

buildInfo.env.collect()

1 个答案:

答案 0 :(得分:2)

问题1

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

第一个操作就是您要做的-保存从 server.uoload server.downkoad 方法返回的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()

派上用场了。