Jenkins Pipeline sh:命令未找到 - 某些命令现在不可用吗?

时间:2017-02-04 04:27:23

标签: bash shell jenkins sh jenkins-pipeline

我的Jenkins管道中有以下阶段:

stage('Update Android SDK') {
    withEnv(['ANDROID_HOME=/Users/Shared/android-sdk-macosx',
             'PATH=$ANDROID_HOME:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$PATH'
    ]) {
        sh 'echo y | android update sdk --no-ui --all --filter platform,tool,platform-tool,extra-android-m2repository'
}

这曾经很完美。但是,我刚刚更新到Jenkins 2.44和最新的Pipeline插件,从那时起,android命令就不再运行了。这是来自jenkins日志的错误消息:

+ android update sdk --no-ui --all --filter platform,tool,platform-tool,extra-android-m2repository
/Users/Jenkins/.jenkins/workspace/d_release-3-0-3dummy.1gpbab-FJUYVCADYU5YVX7LNYATQTVORNDAKFSYICGSZRW4QXTUW5E2OMEQ@tmp/durable-f249f287/script.sh: line 2: android: command not found

并且它不仅仅是android 。它看起来像一些安全设置已经改变或某些东西,某些命令不能再通过sh管道命令运行。例如,echopwd仍有效,但whichls不会:

+ echo /Users/Shared/android-sdk-macosx
/Users/Shared/android-sdk-macosx

++ which android
/Users/Jenkins/.jenkins/workspace/d_rel/script.sh: line 2: which: command not found
+ echo
[Pipeline] sh
[d_rel] Running shell script
+ pwd
/Users/Jenkins/.jenkins/workspace/d_rel
[Pipeline] sh
[d_rel] Running shell script
+ ls -la /Users/Shared/
/Users/Jenkins/.jenkins/workspace/d_rel/script.sh: line 2: ls: command not found

(我在上面的代码段中用d_release-3-0-3dummy.1gpbab-FJUYVCADYU5YVX7LNYATQTVORNDAKFSYICGSZRW4QXTUW5E2OMEQ@tmp/durable-f249f287替换了字符串d_rel,以提高可读性。)

改变了什么?如果现在限制某些命令,我​​该怎么做才能启用所有命令?

1 个答案:

答案 0 :(得分:2)

尝试以下结构:

withEnv(['ANDROID_HOME=/Users/Shared/android-sdk-macosx',]) {
    withEnv(["PATH+ADK=${env.ANDROID_HOME}/tools:${env.ANDROID_HOME}/platform-tools"]) {
        sh 'echo y | android update sdk --no-ui --all --filter platform,tool,platform-tool,extra-android-m2repository'
    }
}

我不认为$ANDROID_HOME会正确扩展。使用刚刚定义的变量的example位于官方管道示例文档中。

PATH+XYZ似乎是追加$PATH的{​​{3}}方法。