jenkins 执行 shell 时,command not found

805 阅读1分钟

有时,当我们在 Jenkins 中执行 shell cmd 时,会抛出如下错误:

xxx command not found

原因在 Jenkins 中,Shell 脚本执行时,通常不会继承当前用户的环境变量,这是因为 Jenkins 在执行时使用的是其自身的构建环境。

要获取当前用户的环境变量,可以在 Shell 脚本中使用 source 命令读取 /etc/profile 或当前用户的 .bash_profile 文件,以便获得所有的环境变量。例如:

source /etc/profile 

source ~/.bash_profile 

#!/bin/bash -ilex

如果您需要使用特定的环境变量,请确保将它们添加到 Jenkins 任务的构建环境中。在 Jenkins 中,您可以在“构建环境”选项卡中设置环境变量。 请注意,Jenkins 中的环境变量和 Linux 用户的环境变量是不同的,因此您需要在构建环境中显式地设置 Jenkins 环境变量。