jenkins运行shell脚本时读取系统环境变量

FreeStyle的JenkinsJob

在freestyle的job中配置执行shell命令后,job在运行时默认的执行方式为

/bin/bash -xe /tmp/jenkinsxxxx.sh

1
2
3
4
5
6
7
Building in workspace /var/lib/jenkins/workspace/jenkins_shell_test
[jenkins_shell_test] $ /bin/sh -xe /tmp/jenkins6138408577395354213.sh
+ set -x
+ export
export BUILD_CAUSE="MANUALTRIGGER"
export BUILD_CAUSE_MANUALTRIGGER="true"
...

在执行机上查看对应的进程

1
2
3
4
[root@localhost ~]# ps -ef |grep -i jenkins
jenkins 12371 1 20 16:30 ? 00:03:55 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon --httpPort=8080 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20
jenkins 27359 12371 0 16:48 ? 00:00:00 /bin/sh -xe /tmp/jenkins6138408577395354213.sh
jenkins 27360 27359 0 16:48 ? 00:00:00 sleep 30

/bin/sh -xe xxx.sh方式运行的shell为非交互式非登录的模式,这种模式下的shell在启动时不会读取/etc/profile中的内容,所以/etc/profile中定义的环境变量不能访问

解决方法:

在shell中添加 #!/bin/bash -ilex,将shell模式修改为交互登录模式即可。

1
2
3
4
5
[root@localhost ~]# ps -ef |grep -i jenkins
jenkins 12371 1 10 16:30 ? 00:04:43 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon --httpPort=8080 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20
jenkins 15197 12371 0 17:13 ? 00:00:00 /bin/bash -liex /tmp/jenkins15271049705299428998.sh
jenkins 15211 15197 0 17:13 ? 00:00:00 sleep 30
[root@localhost ~]#

注意, #!/bin/bash -ilex 一定要在首行

pipeline模式的JenkinsJob

pipeline模式下的原因与解决方案与FreeStyle类似,只是pipeline模式下脚本运行的进程不是jenkins主进程直接fork出来的。

1
2
3
4
5
6
7
[root@localhost ~]# ps -ef |grep -i jenkins
jenkins 12371 1 11 16:30 ? 00:05:13 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon --httpPort=8080 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20
jenkins 17509 1 0 17:16 ? 00:00:00 sh -c (pid=$$; { while [ \( -d /proc/$pid -o \! -d /proc/$$ \) -a -d '/var/lib/jenkins/workspace/pipeline_shell_test@tmp/durable-da5871cf' -a \! -f '/var/lib/jenkins/workspace/pipeline_shell_test@tmp/durable-da5871cf/jenkins-result.txt' ]; do touch '/var/lib/jenkins/workspace/pipeline_shell_test@tmp/durable-da5871cf/jenkins-log.txt'; sleep 3; done } & jsc=durable-7a8937f3d4c335791e06262c93820f88; JENKINS_SERVER_COOKIE=$jsc 'sh' -xe '/var/lib/jenkins/workspace/pipeline_shell_test@tmp/durable-da5871cf/script.sh' > '/var/lib/jenkins/workspace/pipeline_shell_test@tmp/durable-da5871cf/jenkins-log.txt' 2>&1; echo $? > '/var/lib/jenkins/workspace/pipeline_shell_test@tmp/durable-da5871cf/jenkins-result.txt.tmp'; mv '/var/lib/jenkins/workspace/pipeline_shell_test@tmp/durable-da5871cf/jenkins-result.txt.tmp' '/var/lib/jenkins/workspace/pipeline_shell_test@tmp/durable-da5871cf/jenkins-result.txt'; wait) >&- 2>&- &
jenkins 17511 17509 0 17:16 ? 00:00:00 sh -c (pid=$$; { while [ \( -d /proc/$pid -o \! -d /proc/$$ \) -a -d '/var/lib/jenkins/workspace/pipeline_shell_test@tmp/durable-da5871cf' -a \! -f '/var/lib/jenkins/workspace/pipeline_shell_test@tmp/durable-da5871cf/jenkins-result.txt' ]; do touch '/var/lib/jenkins/workspace/pipeline_shell_test@tmp/durable-da5871cf/jenkins-log.txt'; sleep 3; done } & jsc=durable-7a8937f3d4c335791e06262c93820f88; JENKINS_SERVER_COOKIE=$jsc 'sh' -xe '/var/lib/jenkins/workspace/pipeline_shell_test@tmp/durable-da5871cf/script.sh' > '/var/lib/jenkins/workspace/pipeline_shell_test@tmp/durable-da5871cf/jenkins-log.txt' 2>&1; echo $? > '/var/lib/jenkins/workspace/pipeline_shell_test@tmp/durable-da5871cf/jenkins-result.txt.tmp'; mv '/var/lib/jenkins/workspace/pipeline_shell_test@tmp/durable-da5871cf/jenkins-result.txt.tmp' '/var/lib/jenkins/workspace/pipeline_shell_test@tmp/durable-da5871cf/jenkins-result.txt'; wait) >&- 2>&- &
jenkins 17512 17509 0 17:16 ? 00:00:00 sh -xe /var/lib/jenkins/workspace/pipeline_shell_test@tmp/durable-da5871cf/script.sh
jenkins 17514 17512 0 17:16 ? 00:00:00 sleep 30
jenkins 17786 17511 0 17:16 ? 00:00:00 sleep 3

pipeline中配置bash的登录模式

1
2
3
4
// #!/bin/bash -ilex 一定要在首行,与第一个 """ 同行
sh """#!/bin/bash -ilex
export
"""