Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-28828

echo step prints null when passed a non-String (was: cannot access envVars from RunWrapper)

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • workflow-cps-plugin
    • None
    • Jenkins 1.609.1
      workflow 1.8

      Hello,

      in Jenkins, I have two jobs:

      • one freestyle named foo, which calls a shell script which does echo $BUILD_ID
      • one workflow job

      When trigerring the job foo from the workflow, it is not possible to get the env vars the build had. For example, if I do

      def foo = build 'foo'
      echo foo.buildVariables
      echo foo.rawBuild.envVars
      

      Then I only get null. However, it is possible, from the script console to get those variables using

      def build = Jenkins.getInstance().getItemByFullName("foo").getBuildByNumber(6)
      build.getEnvVars()
      

      I get

      Result: {BUILD_DISPLAY_NAME=#6, BUILD_ID=6, BUILD_NUMBER=6, BUILD_TAG=jenkins-foo-6, BUILD_URL=http://localhost:8080/job/foo/6/, CLASSPATH=, HUDSON_HOME=/****/jenkins-home, HUDSON_SERVER_COOKIE=****, HUDSON_URL=http://localhost:8080/, JENKINS_HOME=/****/jenkins-home, JENKINS_SERVER_COOKIE=****, JENKINS_URL=http://localhost:8080/, JOB_NAME=foo, JOB_URL=http://localhost:8080/job/foo/, WORKSPACE=/****/jenkins-home/jobs/foo/workspace}
      

      The call to foo.getBuildVariables() return an empty map, but not null, as it is in the workflow.

      The workflow is not sandboxed so the foo.rawBuild is a Run object as in the script console. The two code should return the exact same result, don't you think?

          [JENKINS-28828] echo step prints null when passed a non-String (was: cannot access envVars from RunWrapper)

          Hello,

          even if the echo foo.rawBuild.envVars display null, it is possible to

          echo foo.rawBuild.envVars.toString()
          echo foo.rawBuild.envVars.get("BUILD_ID")
          

          and there is no NullPointerException and the values are correct.

          Adrien Lecharpentier added a comment - Hello, even if the echo foo.rawBuild.envVars display null , it is possible to echo foo.rawBuild.envVars.toString() echo foo.rawBuild.envVars.get( "BUILD_ID" ) and there is no NullPointerException and the values are correct.

          Jesse Glick added a comment -

          The echo step takes a String parameter. You may not pass other objects to it; there is no implicit casting in step parameters currently. So while

          echo foo.buildVariables

          is illegal, you may use

          echo foo.buildVariables.toString()

          or even

          echo "${foo.buildVariables}"

          . (GString is given special treatment since this is so commonly used in Groovy.)

          println foo.buildVariables

          may work, since println is declared to accept an Object.

          Why echo would print null, I am unsure. Probably the build should fail with a descriptive error.

          BTW you should be able to use echo foo.rawBuild.envVars.BUILD_ID for the normal case that the variable is a legal identifier.

          BUILD_ID is not a “build variable”. I know parameters appear among “build variables”, and certain plugins add “build variables”, but these are distinct from “environment variables”. You may file a separate RFE for exposing .envVars directly from the RunWrapper for use in sandboxed scripts.

          Jesse Glick added a comment - The echo step takes a String parameter. You may not pass other objects to it; there is no implicit casting in step parameters currently. So while echo foo.buildVariables is illegal, you may use echo foo.buildVariables.toString() or even echo "${foo.buildVariables}" . ( GString is given special treatment since this is so commonly used in Groovy.) println foo.buildVariables may work, since println is declared to accept an Object . Why echo would print null , I am unsure. Probably the build should fail with a descriptive error. BTW you should be able to use echo foo.rawBuild.envVars.BUILD_ID for the normal case that the variable is a legal identifier. BUILD_ID is not a “build variable”. I know parameters appear among “build variables”, and certain plugins add “build variables”, but these are distinct from “environment variables”. You may file a separate RFE for exposing .envVars directly from the RunWrapper for use in sandboxed scripts.

          Jesse Glick added a comment -

          Possibly caused by JENKINS-33217.

          Jesse Glick added a comment - Possibly caused by JENKINS-33217 .

            jglick Jesse Glick
            alecharp Adrien Lecharpentier
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: