Aphex
Aphex

Reputation: 7500

Jenkins Pipeline sh: command not found - are certain commands not available now?

I have the following stage in my Jenkins pipeline:

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'
}

This used to work perfectly. However, I just updated to Jenkins 2.44 and the latest Pipeline plugins and since then, the android command doesn't run anymore. Here's the error message from the jenkins log:

+ 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

And it isn't just android. It looks to me like some security setting has changed or something, and certain commands can't run via the sh pipeline command anymore. For example, echo and pwd still work, but which or ls don't:

+ 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

(I've replaced the string d_release-3-0-3dummy.1gpbab-FJUYVCADYU5YVX7LNYATQTVORNDAKFSYICGSZRW4QXTUW5E2OMEQ@tmp/durable-f249f287 with d_rel in the snippet above to improve readability.)

What changed? If certain commands are restricted now, what can I do to enable all commands to run?

Upvotes: 3

Views: 7599

Answers (1)

Grisha Levit
Grisha Levit

Reputation: 8617

Try something structured like:

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'
    }
}

I don't think $ANDROID_HOME will get expanded correctly otherwise. An example of using a variable that was just defined is in the official Pipeline Examples doc.

The PATH+XYZ thing seems to be the suggested way to append to $PATH.

Upvotes: 3

Related Questions