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

pipeline/freestylejob cannot get empty param value

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • core
    • None
    • ubuntu
      jenkins 2.249.3

      jdk8

      There are 2 questions :

      1。 pipeline/freestylejob cannot get empty param value。
      2。 pipeline under docker agent can  get empty param value。
      

      I define the pipelie with parameter like below

      parameters {
           string(name: 'ANDROID_PLATFORM', defaultValue: '')
      }
      

      in stage i try to print this parameter 'ANDROID_PLATFORM', like below

      stage("0.clean ws") {
      steps {
        sh '''#!/bin/bash
      env
      '''
      }
      }
      
      
      

      start this pipeline with empty string ANDROID_PLATFORM, the env command not print this value.
      in python code i try to get ANDROID_PLATFORM like this

        os.environ.get("ANDROID_PLATFORM")

      but this return None.I want this return "", an empty string value。

      workaround in python maybe work well like this:

        os.environ.get("ANDROID_PLATFORM", "")

          [JENKINS-65066] pipeline/freestylejob cannot get empty param value

          Oleg Nenashev added a comment -

          After https://github.com/jenkinsci/jenkins/pull/5275 I suspect this use-case should be considered unsupported in Jenkins. CC jglick timja for an opinion

          Oleg Nenashev added a comment - After https://github.com/jenkinsci/jenkins/pull/5275  I suspect this use-case should be considered unsupported in Jenkins. CC jglick timja  for an opinion

          bright.ma added a comment - - edited

          I define a free style job with parameters looks like this:

          I try to debug the code, and tracking the jenkin code execution flow:

          at this line https://github.com/jenkinsci/jenkins/blob/b25281994720ee4adce0ff16bae4333d27593f58/core/src/main/java/hudson/tasks/CommandInterpreter.java#L128

          the envVars contains the empty parameters

          Maybe these empty envVars were deleted somewhere in the code .
          in public static class LocalLauncher extends Launcher class I found they were deleted by "EnvVars jobEnv = inherit(ps.envs);" at https://github.com/jenkinsci/jenkins/blob/b25281994720ee4adce0ff16bae4333d27593f58/core/src/main/java/hudson/Launcher.java#L979

          maybe this use-case was not supported in Jenkins at beginning。

          bright.ma added a comment - - edited I define a free style job with parameters looks like this: I try to debug the code, and tracking the jenkin code execution flow: at this line https://github.com/jenkinsci/jenkins/blob/b25281994720ee4adce0ff16bae4333d27593f58/core/src/main/java/hudson/tasks/CommandInterpreter.java#L128 the envVars contains the empty parameters Maybe these empty envVars were deleted somewhere in the code . in public static class LocalLauncher extends Launcher class I found they were deleted by "EnvVars jobEnv = inherit(ps.envs);" at https://github.com/jenkinsci/jenkins/blob/b25281994720ee4adce0ff16bae4333d27593f58/core/src/main/java/hudson/Launcher.java#L979 maybe this use-case was not supported in Jenkins at beginning。

          bright.ma added a comment - - edited

          but if run under pipeline with docker agent like below:

          pipeline {
              agent {
                  docker {
                      image 'maven:3-alpine'
                      args '-v $HOME/.m2:/root/.m2'
                      label 'docker-01'
                  }
              }
                      parameters {    
                  string(name: 'ANDROID_PLATFORM')
                      }
              stages {
                  stage('Build') {
                      steps {
                          sh 'env'
                      }
                  }
              }
          }
          
          Started by user unknown or anonymous
          Replayed #42
          Running in Durability level: MAX_SURVIVABILITY
          [Pipeline] Start of Pipeline
          [Pipeline] node
          Running on docker-01 in /tmp/docker-01/workspace/test_pipeline
          [Pipeline] {
          [Pipeline] isUnix
          [Pipeline] sh
          + docker inspect -f . maven:3-alpine
          .
          [Pipeline] withDockerContainer
          docker-01 does not seem to be running inside a container
          $ docker run -t -d -u 1000:1000 -v $HOME/.m2:/root/.m2 -w /tmp/docker-01/workspace/test_pipeline -v /tmp/docker-01/workspace/test_pipeline:/tmp/docker-01/workspace/test_pipeline:rw,z -v /tmp/docker-01/workspace/test_pipeline@tmp:/tmp/docker-01/workspace/test_pipeline@tmp:rw,z -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** maven:3-alpine cat
          $ docker top 77f3858dd91a10ee59db7ca27678001783012cd5b46801e779da9a5b5f63d3c4 -eo pid,comm
          [Pipeline] {
          [Pipeline] stage
          [Pipeline] { (Build)
          [Pipeline] sh
          + env
          JENKINS_HOME=/work/brightma/github/jenkins/war/work
          JAVA_ALPINE_VERSION=8.131.11-r2
          HOSTNAME=77f3858dd91a
          RUN_CHANGES_DISPLAY_URL=http://10.0.12.116:8080/jenkins/job/test_pipeline/85/display/redirect?page=changes
          BRANCH=
          NODE_LABELS=docker-01
          HUDSON_URL=http://10.0.12.116:8080/jenkins/
          SHLVL=2
          ASSIGNER=
          HOME=/
          BUILD_URL=http://10.0.12.116:8080/jenkins/job/test_pipeline/85/
          CONFIG_BRANCH=
          BUILD_TYPE=
          JENKINS_SERVER_COOKIE=durable-9c5625ac73e1e26a7682d97c02c19744
          MAVEN_HOME=/usr/share/maven
          WORKSPACE=/tmp/docker-01/workspace/test_pipeline
          AAIS_CLEAN_OUT=false
          JAVA_VERSION=8u131
          GROUP_ID=
          NODE_NAME=docker-01
          RUN_ARTIFACTS_DISPLAY_URL=http://10.0.12.116:8080/jenkins/job/test_pipeline/85/display/redirect?page=artifacts
          EXECUTOR_NUMBER=0
          STAGE_NAME=Build
          BUILD_DISPLAY_NAME=#85
          RUN_TESTS_DISPLAY_URL=http://10.0.12.116:8080/jenkins/job/test_pipeline/85/display/redirect?page=tests
          JOB_BASE_NAME=test_pipeline
          HUDSON_HOME=/work/brightma/github/jenkins/war/work
          AAIS_CLEAN_ALL=false
          PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin
          BUILD_ID=85
          IMAGEMODE=
          BUILD_TAG=jenkins-test_pipeline-85
          JENKINS_URL=http://10.0.12.116:8080/jenkins/
          LANG=C.UTF-8
          JOB_URL=http://10.0.12.116:8080/jenkins/job/test_pipeline/
          BUILD_NUMBER=85
          JENKINS_NODE_COOKIE=9ea59d35-1e8c-418e-998b-910f8cedadd4
          RUN_DISPLAY_URL=http://10.0.12.116:8080/jenkins/job/test_pipeline/85/display/redirect
          HUDSON_SERVER_COOKIE=925e1992be03d5fd
          JOB_DISPLAY_URL=http://10.0.12.116:8080/jenkins/job/test_pipeline/display/redirect
          MANIFEST_FILE=
          CLASSPATH=
          PRODUCT=
          CARRIER=
          JOB_NAME=test_pipeline
          JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk
          PWD=/tmp/docker-01/workspace/test_pipeline
          MAVEN_CONFIG=/root/.m2
          WORKSPACE_TMP=/tmp/docker-01/workspace/test_pipeline@tmp
          VARIANT=
          ANDROID_PLATFORM=
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] }
          $ docker stop --time=1 77f3858dd91a10ee59db7ca27678001783012cd5b46801e779da9a5b5f63d3c4
          $ docker rm -f 77f3858dd91a10ee59db7ca27678001783012cd5b46801e779da9a5b5f63d3c4
          [Pipeline] // withDockerContainer
          [Pipeline] }
          [Pipeline] // node
          [Pipeline] End of Pipeline
          Finished: SUCCESS
          

          the linux env command show the parameter ANDROID_PLATFORM as empty string value.

          Maybe these empty envVars pass to docker with -e options. see https://github.com/jenkinsci/docker-workflow-plugin/blob/eb0b0db4791ca8b40fe38f53ec7668118a82b7b4/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java#L135

          I'm not sure if I am correct

          bright.ma added a comment - - edited but if run under pipeline with docker agent like below: pipeline { agent { docker { image 'maven:3-alpine' args '-v $HOME/.m2:/root/.m2' label 'docker-01' } } parameters { string(name: 'ANDROID_PLATFORM' ) } stages { stage( 'Build' ) { steps { sh 'env' } } } } Started by user unknown or anonymous Replayed #42 Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline [Pipeline] node Running on docker-01 in /tmp/docker-01/workspace/test_pipeline [Pipeline] { [Pipeline] isUnix [Pipeline] sh + docker inspect -f . maven:3-alpine . [Pipeline] withDockerContainer docker-01 does not seem to be running inside a container $ docker run -t -d -u 1000:1000 -v $HOME/.m2:/root/.m2 -w /tmp/docker-01/workspace/test_pipeline -v /tmp/docker-01/workspace/test_pipeline:/tmp/docker-01/workspace/test_pipeline:rw,z -v /tmp/docker-01/workspace/test_pipeline@tmp:/tmp/docker-01/workspace/test_pipeline@tmp:rw,z -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** maven:3-alpine cat $ docker top 77f3858dd91a10ee59db7ca27678001783012cd5b46801e779da9a5b5f63d3c4 -eo pid,comm [Pipeline] { [Pipeline] stage [Pipeline] { (Build) [Pipeline] sh + env JENKINS_HOME=/work/brightma/github/jenkins/war/work JAVA_ALPINE_VERSION=8.131.11-r2 HOSTNAME=77f3858dd91a RUN_CHANGES_DISPLAY_URL=http: //10.0.12.116:8080/jenkins/job/test_pipeline/85/display/redirect?page=changes BRANCH= NODE_LABELS=docker-01 HUDSON_URL=http: //10.0.12.116:8080/jenkins/ SHLVL=2 ASSIGNER= HOME=/ BUILD_URL=http: //10.0.12.116:8080/jenkins/job/test_pipeline/85/ CONFIG_BRANCH= BUILD_TYPE= JENKINS_SERVER_COOKIE=durable-9c5625ac73e1e26a7682d97c02c19744 MAVEN_HOME=/usr/share/maven WORKSPACE=/tmp/docker-01/workspace/test_pipeline AAIS_CLEAN_OUT= false JAVA_VERSION=8u131 GROUP_ID= NODE_NAME=docker-01 RUN_ARTIFACTS_DISPLAY_URL=http: //10.0.12.116:8080/jenkins/job/test_pipeline/85/display/redirect?page=artifacts EXECUTOR_NUMBER=0 STAGE_NAME=Build BUILD_DISPLAY_NAME=#85 RUN_TESTS_DISPLAY_URL=http: //10.0.12.116:8080/jenkins/job/test_pipeline/85/display/redirect?page=tests JOB_BASE_NAME=test_pipeline HUDSON_HOME=/work/brightma/github/jenkins/war/work AAIS_CLEAN_ALL= false PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin BUILD_ID=85 IMAGEMODE= BUILD_TAG=jenkins-test_pipeline-85 JENKINS_URL=http: //10.0.12.116:8080/jenkins/ LANG=C.UTF-8 JOB_URL=http: //10.0.12.116:8080/jenkins/job/test_pipeline/ BUILD_NUMBER=85 JENKINS_NODE_COOKIE=9ea59d35-1e8c-418e-998b-910f8cedadd4 RUN_DISPLAY_URL=http: //10.0.12.116:8080/jenkins/job/test_pipeline/85/display/redirect HUDSON_SERVER_COOKIE=925e1992be03d5fd JOB_DISPLAY_URL=http: //10.0.12.116:8080/jenkins/job/test_pipeline/display/redirect MANIFEST_FILE= CLASSPATH= PRODUCT= CARRIER= JOB_NAME=test_pipeline JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk PWD=/tmp/docker-01/workspace/test_pipeline MAVEN_CONFIG=/root/.m2 WORKSPACE_TMP=/tmp/docker-01/workspace/test_pipeline@tmp VARIANT= ANDROID_PLATFORM= [Pipeline] } [Pipeline] // stage [Pipeline] } $ docker stop --time=1 77f3858dd91a10ee59db7ca27678001783012cd5b46801e779da9a5b5f63d3c4 $ docker rm -f 77f3858dd91a10ee59db7ca27678001783012cd5b46801e779da9a5b5f63d3c4 [Pipeline] // withDockerContainer [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS the linux env command show the parameter ANDROID_PLATFORM as empty string value. Maybe these empty envVars pass to docker with -e options. see https://github.com/jenkinsci/docker-workflow-plugin/blob/eb0b0db4791ca8b40fe38f53ec7668118a82b7b4/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java#L135 I'm not sure if I am correct

          Jesse Glick added a comment -

          By default the Bourne shell will not distinguish an empty string from an undefined parameter; other tools like Python of course may be more precise. The trouble is that parameters are traditionally defined via the GUI config form which cannot differentiate between null and "", so in PR-5275 I normalized the value so that it will not change during round-tripping and will not result in a spurious <defaultValue></defaultValue> in persisted form. OTOH https://github.com/jglick/jenkins/blob/58174df061274fd2c3623bb3fc82a284b9e5da0c/core/src/main/java/hudson/model/StringParameterValue.java#L52 is supposed to convert null to "" so I wonder what is happening. Probably depends on the precise mechanism used to both define the parameter and trigger the build.

          Jesse Glick added a comment - By default the Bourne shell will not distinguish an empty string from an undefined parameter; other tools like Python of course may be more precise. The trouble is that parameters are traditionally defined via the GUI config form which cannot differentiate between null and "" , so in PR-5275 I normalized the value so that it will not change during round-tripping and will not result in a spurious <defaultValue></defaultValue> in persisted form. OTOH https://github.com/jglick/jenkins/blob/58174df061274fd2c3623bb3fc82a284b9e5da0c/core/src/main/java/hudson/model/StringParameterValue.java#L52 is supposed to convert null to "" so I wonder what is happening. Probably depends on the precise mechanism used to both define the parameter and trigger the build.

          Jesse Glick added a comment -

          mmh19891113 you are really using 2.249.3 as the issue claims? In which case you were not using PR-5275, which was just merged?

          Jesse Glick added a comment - mmh19891113 you are really using 2.249.3 as the issue claims? In which case you were not using PR-5275, which was just merged?

          Jesse Glick added a comment -

          Hmm, seems Declarative Pipeline behaves differently than Scripted Pipeline: an empty parameter value is not bound to an environment variable. There is also different behavior between the first build (triggered before the project is parameterized) and subsequent builds (when it has been). But both of these issues seem to be true also in 2.277.1, before PR-5275. My tentative conclusion is that defaultValue: '' is pointless and your jobs should just be prepared to handle a missing environment variable, either by being more defensive in a program run by sh/bat (like your Python workaround), or by using withEnv.

          Jesse Glick added a comment - Hmm, seems Declarative Pipeline behaves differently than Scripted Pipeline: an empty parameter value is not bound to an environment variable. There is also different behavior between the first build (triggered before the project is parameterized) and subsequent builds (when it has been). But both of these issues seem to be true also in 2.277.1, before PR-5275. My tentative conclusion is that defaultValue: '' is pointless and your jobs should just be prepared to handle a missing environment variable, either by being more defensive in a program run by sh / bat (like your Python workaround), or by using withEnv .

          bright.ma added a comment -

          like you said an empty parameter value is not bound to an environment variable.
          aggree with you we should handle the defaultValue empty value in sh or python.

          what do you think "pipeline with docker agent " the defaultValue empty value was passed to docker with -e options.
          see https://github.com/jenkinsci/docker-workflow-plugin/blob/eb0b0db4791ca8b40fe38f53ec7668118a82b7b4/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java#L135

          The results of these two are not the same ,1: pipeline/freestylejob cannot get empty defaultValue value。
          2: pipeline under docker agent can get empty defaultValue value。

          bright.ma added a comment - like you said an empty parameter value is not bound to an environment variable. aggree with you we should handle the defaultValue empty value in sh or python. what do you think "pipeline with docker agent " the defaultValue empty value was passed to docker with -e options. see https://github.com/jenkinsci/docker-workflow-plugin/blob/eb0b0db4791ca8b40fe38f53ec7668118a82b7b4/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java#L135 The results of these two are not the same ,1: pipeline/freestylejob cannot get empty defaultValue value。 2: pipeline under docker agent can get empty defaultValue value。

          Jesse Glick added a comment -

          Not sure offhand why the behavior would differ in the docker-workflow plugin.

          Jesse Glick added a comment - Not sure offhand why the behavior would differ in the docker-workflow plugin.

            Unassigned Unassigned
            mmh19891113 bright.ma
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: