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

sshCommand cannot access Environment variables, set from profile.d shell scripts

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Major Major
    • ssh-steps-plugin
    • None
    • Jenkins 2.284
      SSH Pipeline Steps 2.0.0

      Remote host details:
      Ubuntu 20.04

      Which type shell "SSH Pipeline Steps" uses for logging in to remote hosts? I had set an environment variable using a shell script inside /etc/profile.d/devOpsEnvVars.sh.

      export datetime=2021-06-13_20_40_00
      export backedUpFileName=ProjectName_$datetime.war

      According to the Ubuntu Environment variables documentation,] whenever a "bash" shell login happens, the shell scripts inside /etc/profile.d/ will be executed.

      If I login through SSH using regular terminal, then the new variables will be available.

      Does "SSH Pipeline Steps" uses "bash" shell for logging in to remote system?

      Below are some logs showing the behavior. I have tried accessing "JAVA_HOME" which has been set in /etc/environment file. And it is successfully accessed. But the variables "datetime" and "backedUpFileName" are printing empty lines (Not able to access them).

       

      Capturing new file name to be used for back up
      [Pipeline] sshCommand
      Executing command on ****-VirtualBox[192.168.0.104]: touch /etc/profile.d/devOpsEnvVars.sh && sudo chmod 755 /etc/profile.d/devOpsEnvVars.sh && sudo sh -c 'echo "export datetime=$(date +"%Y-%m-%d_%H_%M_%S")" > /etc/profile.d/devOpsEnvVars.sh' sudo: true
      509e8ae0-503b-44ad-a281-cde88d2e0cee
      [Pipeline] echo
      pty before resetting = true
      [Pipeline] sshCommand
      Executing command on ****-VirtualBox[192.168.0.104]: sudo sh -c 'echo "export backedUpFileName=ProjectName_\$datetime.war" >> /etc/profile.d/devOpsEnvVars.sh' sudo: true
      3b248ad2-2074-4b8f-9afa-1c74b9da5373
      [Pipeline] echo
      rempty = true
      [Pipeline] echo
      Accessing Environment Variables.
      [Pipeline] sshCommand
      Executing command on ****-VirtualBox[192.168.0.104]: cat /etc/profile.d/devOpsEnvVars.sh sudo: false
      export datetime=2021-06-13_20_40_00
      export backedUpFileName=ProjectName_$datetime.war
      [Pipeline] sshCommand
      Executing command on ****-VirtualBox[192.168.0.104]: ls -l /etc/profile.d/devOpsEnvVars.sh sudo: false
      -rwxr-xr-x 1 root root 86 Jun 13 20:40 /etc/profile.d/devOpsEnvVars.sh
      [Pipeline] sshCommand
      Executing command on ****-VirtualBox[192.168.0.104]: echo $datetime && echo $backedUpFileName sudo: false
      
      [Pipeline] sshCommand
      Executing command on ****-VirtualBox[192.168.0.104]: echo $JAVA_HOME sudo: false
      /opt/jdk-15.0.2/
      [Pipeline] sshCommand
      Executing command on ****-VirtualBox[192.168.0.104]: echo $custEnvVar sudo: false
      [Pipeline] sshCommand
      

      However I cannot use /etc/environment file because, in each build, I have to keep updating the variables. Writing code to search particular variable and replacing the whole line in "/etc/environment" is not what I'm expecting to do. "profile.d" way is more easy.

      Temporarily, as of now, I'm creating shell script in home directory and executing that script, in each "sshCommand" step, and then appending new command after "&&". But I don't want to do like this as well. I want to use Ubuntu features.

      Is there is any work around to access Environment variables in proper way?

      OR

      Is it possible to make "SSH Pipeline Steps" to use "bash" shell, if that is not what being used?

            nrayapati Naresh Rayapati
            padmahasa Padmahasa
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: