【问题标题】:Environment variable not seen from Jenkins Pipeline scrit从 Jenkins Pipeline 脚本中看不到环境变量
【发布时间】:2017-07-26 14:31:03
【问题描述】:

在“Manage Jenkins” -> “Configure System” -> “全局属性”中,我添加了:

Name:  git
Value: /path/to/my/git

和:

Name: PATH+git
Value: /path/to/my/git

但是,如果我从 Jenkins 管道尝试 sh("git status"),我会得到:

git: command not found

如果我尝试使用完整路径 sh("/path/to/my/git status"),则会看到 git。

知道为什么git 在被声明为环境变量后在 Jenkins 流水线脚本中没有出现吗?

【问题讨论】:

    标签: git jenkins jenkins-pipeline


    【解决方案1】:

    您必须在 sh 步骤中使用变量引用:

    sh '$git status'
    

    注意单引号,这样 groovy 不会将 $ 解释为变量引用(由 sh 决定)。如果使用双引号,则必须转义美元符号:

    sh "\$git status"
    

    或者你可以直接在groovy中访问环境:

    sh "${env.git} status"
    

    【讨论】:

    • 有没有办法让git被视为环境变量?有一个脚本直接调用git,我修改不了。
    • 我建议您修改 PATH 变量并将路径附加到 git 的 start 路径变量,例如/path/to/my/git:$PATH。看到这个答案:stackoverflow.com/a/5819768/7509826
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 2019-09-24
    • 2013-02-06
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多