Search code examples
dockerjenkinsjenkins-pipelineartifactory

Artifactory rtBuildInfo does not collect env variables in Jenkins docker pipeline


I want to collect env variables inside of Jenkins declarative dockerized pipeline using Artifactory plugin.

pipeline {
  agent {
    docker {
      image 'node:10.16'
    }
  }
  stages {
    stage ('Build') {
      // This publish build info but without env variables:
      rtBuildInfo captureEnv: true
      rtPublishBuildInfo serverId: "Artifactory1"
      // This publish build info WITH env variables:
      script {
        server = Artifactory.server "Artifactory1"

        buildInfo = Artifactory.newBuildInfo()
        buildInfo.env.capture = true
        buildInfo.env.collect()
        server.publishBuildInfo buildInfo
      }
    }
  }
}

So using scripted syntax it works, using declarative not. I was following this JFrog documentation: https://www.jfrog.com/confluence/display/RTF/Declarative+Pipeline+Syntax#DeclarativePipelineSyntax-PublishingBuild-InfotoArtifactory


Solution

  • Both rtBuildInfo captureEnv: true and buildInfo.env.capture = true

    set the Build-Info object to automatically capture environment variables while downloading and uploading files

    which means env variables will only be collected during upload and download.

    buildInfo.env.collect(), however

    collect environment variables at any point in the script

    which is why the env variables are collected in your case. Declarative does not support a similar option.