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

Can't access environment in windows bat script in pipline

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Major Major
    • pipeline
    • jenkins 2.332.1

      pipeline {
        agent {label 'windows'}
       parameters {
          choice(choices: ['22.03', '20.03', '22.09'], name: 'release')     }
      
        environment {
          release_version = '0'    
        }
      
        stages {
          stage('prepare') {
            steps {
              script {
                nstdout = bat(returnStdout: true, script: '@python get_sp_version.py %release%').trim()                                
                release_version = nstdout                    
                println("stdout ####" + release_version + "###########")
              }
           bat "echo %release_version%.%BUILD_NUMBER%"
           echo "${release_version}.${BUILD_NUMBER}"
         }
         }
        }
      }
      

      The code above will output like:
       

      stdout ####2203.1###########
      0.47
      2203.1.47
      

       
      2203.1 is the value of the nstdout and release_version.

      My expected output is

       

      stdout ####2203.1########### 
      2203.1.47
      2203.1.47

       

      Am i missing something?
      IMO the env var can be accessed by the bat script directly, right?

          [JENKINS-70381] Can't access environment in windows bat script in pipline

          chaoran created issue -
          chaoran made changes -
          Description Original: ``
          pipeline {
              agent \{label 'windows'}

              parameters {
                  choice(choices: ['22.03', '20.03', '22.09'], name: 'release')
              }

              environment {
                  release_version = '0'
              }

              stages {
                  stage('prepare') {
                      steps {
                          script {
                              nstdout = bat(returnStdout: true, script: '@python get_sp_version.py %release%').trim()
                              println("stdout ####" + nstdout + "###########")
                          }
                          bat "echo %nstdout%.%BUILD_NUMBER%"
                          echo "${nstdout}.${BUILD_NUMBER}"
                      }
                  }
              }
          }```
          The code above will output like:
          ```
          stdout ####2203.1###########
          .47
          2203.1.47
          ```
           
          2203.1 is the value of the `nstdout`
          Am i missing something?
          IMO the env var can be accessed by the bat script directly, right?
          New: {code:java}
          pipeline

          {     agent \{label 'windows'}

              parameters

          {         choice(choices: ['22.03', '20.03', '22.09'], name: 'release')     }

              environment

          {         release_version = '0'     }

              stages {
                  stage('prepare') {
                      steps {
                          script

          {                     nstdout = bat(returnStdout: true, script: '@python get_sp_version.py %release%').trim()                     println("stdout ####" + nstdout + "###########")                 }

                          bat "echo %nstdout%.%BUILD_NUMBER%"
                          echo "${nstdout}.${BUILD_NUMBER}"
                      }
                  }
              }
          }
          {code}
          The code above will output like:
           
          {code:java}
          stdout ####2203.1###########
          .47
          2203.1.47
          {code}
           
          2203.1 is the value of the nstdout
          Am i missing something?
          IMO the env var can be accessed by the bat script directly, right?
          chaoran made changes -
          Description Original: {code:java}
          pipeline

          {     agent \{label 'windows'}

              parameters

          {         choice(choices: ['22.03', '20.03', '22.09'], name: 'release')     }

              environment

          {         release_version = '0'     }

              stages {
                  stage('prepare') {
                      steps {
                          script

          {                     nstdout = bat(returnStdout: true, script: '@python get_sp_version.py %release%').trim()                     println("stdout ####" + nstdout + "###########")                 }

                          bat "echo %nstdout%.%BUILD_NUMBER%"
                          echo "${nstdout}.${BUILD_NUMBER}"
                      }
                  }
              }
          }
          {code}
          The code above will output like:
           
          {code:java}
          stdout ####2203.1###########
          .47
          2203.1.47
          {code}
           
          2203.1 is the value of the nstdout
          Am i missing something?
          IMO the env var can be accessed by the bat script directly, right?
          New: {code:java}
          pipeline {
            agent {label 'windows'}
           parameters {
              choice(choices: ['22.03', '20.03', '22.09'], name: 'release')     }

            environment {
              release_version = '0'    
            }

            stages {
              stage('prepare') {
                steps {
                  script {
                    nstdout = bat(returnStdout: true, script: '@python get_sp_version.py %release%').trim()                    
                    println("stdout ####" + nstdout + "###########")
                  }
               bat "echo %nstdout%.%BUILD_NUMBER%"
               echo "${nstdout}.${BUILD_NUMBER}"
             }
            }
            }
          }
          {code}
          The code above will output like:
           
          {code:java}
          stdout ####2203.1###########
          .47
          2203.1.47
          {code}
           
          2203.1 is the value of the nstdout
          Am i missing something?
          IMO the env var can be accessed by the bat script directly, right?
          chaoran made changes -
          Description Original: {code:java}
          pipeline {
            agent {label 'windows'}
           parameters {
              choice(choices: ['22.03', '20.03', '22.09'], name: 'release')     }

            environment {
              release_version = '0'    
            }

            stages {
              stage('prepare') {
                steps {
                  script {
                    nstdout = bat(returnStdout: true, script: '@python get_sp_version.py %release%').trim()                    
                    println("stdout ####" + nstdout + "###########")
                  }
               bat "echo %nstdout%.%BUILD_NUMBER%"
               echo "${nstdout}.${BUILD_NUMBER}"
             }
            }
            }
          }
          {code}
          The code above will output like:
           
          {code:java}
          stdout ####2203.1###########
          .47
          2203.1.47
          {code}
           
          2203.1 is the value of the nstdout
          Am i missing something?
          IMO the env var can be accessed by the bat script directly, right?
          New: {code:java}
          pipeline {
            agent {label 'windows'}
           parameters {
              choice(choices: ['22.03', '20.03', '22.09'], name: 'release')     }

            environment {
              release_version = '0'    
            }

            stages {
              stage('prepare') {
                steps {
                  script {
                    nstdout = bat(returnStdout: true, script: '@python get_sp_version.py %release%').trim()                                
                    release_version = nstdout                   
                    println("stdout ####" + release_version + "###########")
                  }
               bat "echo %release_version%.%BUILD_NUMBER%"
               echo "${release_version}.${BUILD_NUMBER}"
             }
            }
            }
          }
          {code}
          The code above will output like:
           
          {code:java}
          stdout ####2203.1###########
          .47
          2203.1.47
          {code}
           
          2203.1 is the value of the nstdout and release_version.

          My expected output is

           
          {code:java}
          stdout ####2203.1###########
          2203.1.47
          2203.1.47{code}
           

          Am i missing something?
          IMO the env var can be accessed by the bat script directly, right?
          chaoran made changes -
          Description Original: {code:java}
          pipeline {
            agent {label 'windows'}
           parameters {
              choice(choices: ['22.03', '20.03', '22.09'], name: 'release')     }

            environment {
              release_version = '0'    
            }

            stages {
              stage('prepare') {
                steps {
                  script {
                    nstdout = bat(returnStdout: true, script: '@python get_sp_version.py %release%').trim()                                
                    release_version = nstdout                   
                    println("stdout ####" + release_version + "###########")
                  }
               bat "echo %release_version%.%BUILD_NUMBER%"
               echo "${release_version}.${BUILD_NUMBER}"
             }
            }
            }
          }
          {code}
          The code above will output like:
           
          {code:java}
          stdout ####2203.1###########
          .47
          2203.1.47
          {code}
           
          2203.1 is the value of the nstdout and release_version.

          My expected output is

           
          {code:java}
          stdout ####2203.1###########
          2203.1.47
          2203.1.47{code}
           

          Am i missing something?
          IMO the env var can be accessed by the bat script directly, right?
          New: {code:java}
          pipeline {
            agent {label 'windows'}
           parameters {
              choice(choices: ['22.03', '20.03', '22.09'], name: 'release')     }

            environment {
              release_version = '0'    
            }

            stages {
              stage('prepare') {
                steps {
                  script {
                    nstdout = bat(returnStdout: true, script: '@python get_sp_version.py %release%').trim()                                
                    release_version = nstdout                   
                    println("stdout ####" + release_version + "###########")
                  }
               bat "echo %release_version%.%BUILD_NUMBER%"
               echo "${release_version}.${BUILD_NUMBER}"
             }
            }
            }
          }
          {code}
          The code above will output like:
           
          {code:java}
          stdout ####2203.1###########
          0.47
          2203.1.47
          {code}
           
          2203.1 is the value of the nstdout and release_version.

          My expected output is

           
          {code:java}
          stdout ####2203.1###########
          2203.1.47
          2203.1.47{code}
           

          Am i missing something?
          IMO the env var can be accessed by the bat script directly, right?

          Mark Waite added a comment -

          You'll have better results if you ask questions related to the use of Pipeline in the community forum https://community.jenkins.io or in the gitter chat https://gitter.im/jenkinsci/jenkins . We use the Jenkins issue tracker to track bugs and enhancements. I'm reasonably confident this is not a bug.

          Mark Waite added a comment - You'll have better results if you ask questions related to the use of Pipeline in the community forum https://community.jenkins.io or in the gitter chat https://gitter.im/jenkinsci/jenkins . We use the Jenkins issue tracker to track bugs and enhancements. I'm reasonably confident this is not a bug.
          Mark Waite made changes -
          Resolution New: Fixed [ 1 ]
          Status Original: Open [ 1 ] New: Closed [ 6 ]

            Unassigned Unassigned
            pkking chaoran
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: