最近使用jenkins运行shell脚本,发现配置好的环境变量无法加载,导致安装的模块失效,通过查找资料发现,这是由于jenkins运行脚本是属于非交互式和非登陆式shell,详见https://blog.csdn.net/sch0120/article/details/70226903

        通过查看环境变量:

                   env
               echo $PATH

       主机命令行运行脚本:

LC_PAPER=zh_CN.UTF-8
LC_ADDRESS=zh_CN.UTF-8
LC_MONETARY=zh_CN.UTF-8
TERM=xterm
SHELL=/bin/bash
CONDA_SHLVL=1
LC_NUMERIC=zh_CN.UTF-8
USER=root
LC_TELEPHONE=zh_CN.UTF-8
LS_COLORS=rs=0:di=01
SUDO_USER=gerrit
SUDO_UID=1001
CONDA_EXE=/root/anaconda2/bin/conda
USERNAME=root
_CE_CONDA=
PATH=/root/anaconda2/bin:/root/anaconda2/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
MAIL=/var/mail/root
LC_IDENTIFICATION=zh_CN.UTF-8
CONDA_PREFIX=/root/anaconda2
PWD=/var/lib/jenkins/workspace/datasystem_test
LANG=zh_CN.UTF-8
LC_MEASUREMENT=zh_CN.UTF-8
_CE_M=
HOME=/root
SUDO_COMMAND=/bin/su
SHLVL=2
LANGUAGE=zh_CN:en_US:en
LOGNAME=root
LESSOPEN=| /usr/bin/lesspipe %s
CONDA_DEFAULT_ENV=base
SUDO_GID=1001
LC_TIME=zh_CN.UTF-8
LESSCLOSE=/usr/bin/lesspipe %s %s
LC_NAME=zh_CN.UTF-8
_=/usr/bin/env
/root/anaconda2/bin:/root/anaconda2/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

jenkins运行脚本:

LC_PAPER=zh_CN.UTF-8
LC_ADDRESS=zh_CN.UTF-8
XDG_SESSION_ID=c4
BUILD_URL=https://172.31.119.41:8080/job/datasystem_test/7/
LC_MONETARY=zh_CN.UTF-8
HUDSON_SERVER_COOKIE=799445fd3d2b12d4
SHELL=/bin/bash
BUILD_TAG=jenkins-datasystem_test-7
LC_NUMERIC=zh_CN.UTF-8
JOB_URL=https://172.31.119.41:8080/job/datasystem_test/
WORKSPACE=/var/lib/jenkins/workspace/datasystem_test
RUN_CHANGES_DISPLAY_URL=https://172.31.119.41:8080/job/datasystem_test/7/display/redirect?page=changes
USER=root
LC_TELEPHONE=zh_CN.UTF-8
JENKINS_HOME=/var/lib/jenkins
PATH=/sbin:/home/gerrit/gerrit_site/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MAIL=/var/mail/root
RUN_DISPLAY_URL=https://172.31.119.41:8080/job/datasystem_test/7/display/redirect
QT_QPA_PLATFORMTHEME=appmenu-qt5
LC_IDENTIFICATION=zh_CN.UTF-8
PWD=/var/lib/jenkins/workspace/datasystem_test
HUDSON_URL=https://172.31.119.41:8080/
LANG=zh_CN.UTF-8
JOB_NAME=datasystem_test
LC_MEASUREMENT=zh_CN.UTF-8
LUCENE=/usr/local/src/lucene-8.0.0
BUILD_DISPLAY_NAME=#7
JENKINS_URL=https://172.31.119.41:8080/
BUILD_ID=7
JOB_BASE_NAME=datasystem_test
HOME=/root
SHLVL=2
LANGUAGE=zh_CN:en_US:en
EXECUTOR_NUMBER=0
JENKINS_SERVER_COOKIE=799445fd3d2b12d4
NODE_LABELS=master
LOGNAME=root
HUDSON_HOME=/var/lib/jenkins
NODE_NAME=master
GERRIT_HOME=/home/gerrit/gerrit_site
JOB_DISPLAY_URL=https://172.31.119.41:8080/job/datasystem_test/display/redirect
BUILD_NUMBER=7
XDG_RUNTIME_DIR=/run/user/0
HUDSON_COOKIE=849cd51f-20da-401b-9274-f0144f1c2f43
LC_TIME=zh_CN.UTF-8
LC_NAME=zh_CN.UTF-8
_=/usr/bin/env
/sbin:/home/gerrit/gerrit_site/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

其中主机运行时:

PATH=/root/anaconda2/bin:/root/anaconda2/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

jenkins运行时:

PATH=/sbin:/home/gerrit/gerrit_site/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

对比两个路径发现,jenkins路径中并没有包含模块路径。

 于是打开jenkins中Manage Jenkins ->Configure System

 

在Global Properties中勾选Environment variables ->add  ,填写PATH,并将主机shell环境的path填入,即可正常运行。 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐