Issue
Question: How do I pass an environment variable to a gradle task in Jenkins? Need assistance with step 3 in 'How My Jenkins Job Works'
Research I've done:
How Do I Set Environment Variable In Gradle via Task?
Passing Command Line Arguments in Gradle
Pass job parameter to gradle task in scripted pipeline
How My Jenkins Job Works:
- I have a parameter that allows a file to be uploaded to a personal cloud storage
- It then returns a URL link pointing to this particular file from the cloud storage and stored in an environment variable:
APK_URL
2) Example Link: https://my-storage.com/5/fileVersion/12345.apk
output=$(curl -F file=@"$WORKSPACE/build/SomeProject" -H "Retention: $RETENTION_DAYS" [https://www.myCloudServer.online/fileuploader?key=$MY_KEY](https://www.myCloudServer.online/fileuploader?key=$MY_KEY))
APK_URL=$(echo "$output" | sed -n 's/.*"file_url":"\([^"]*\)".*/\1/p' | sed 's/\\//g')
echo "APK URL": ${env.APK_URL}"
- The
APK_URL
variable is then passed to a gradle command that takes in a url string to run my automation against it.-DbuildUrl=${env.APK.URL}
Just to be clear, this works when I do this locally, so I know passing a URL here works. I just want to accomplish the same thing in Jenkins.
clean test --tests "appium.some.app.Tests.*" -DincludeTags=$RUN_TYPE -DwebhookUrl=[https://hooks.slack.com/services/someSlackChannel](https://hooks.slack.com/services/someSlackChannel) -DbuildUrl=${env.APK_URL}
- Automation begins
Jenkins Configuration
Error Log
*I've changed how some of the errors read, such as urls and any other sensitive information
15:11:29 Copying file to build/SomeAppFile
15:11:34 [verify-android] $ /bin/sh -xe /tmp/jenkins84982492834209384.sh
15:11:34 + curl -F file=@/home/jenkins/workspace/my-workSpace/validate/verify-android/build/SomeAppFile -H Retention: https://www.myCloudStorage.online/f1euploader?key=[***********]
15:11:34 % Total % Received % Xferd Average Speed Time Time Time Current
15:11:34 Dload Upload Total Spent Left Speed
15:11:37
15:11:37 + output={"file_url":"https:\/\/www.myCloudStorage.online\/11\//SomeApplicationFile","retention_days":11,"release_pending":false,"results":"success","time_of_release":-1}
15:11:37 + echo {"file_url":"https:\/\/www.myCloudStorage.online\/11\//SomeApplicationFile","retention_days":11,"release_pending":false,"results":"success","time_of_release":-1}
15:11:37 + sed -n s/.*"file_url":"\([^"]*\)".*/\1/p
15:11:37 + sed s/\\//g
15:11:37 + APK_URL=https://www.myCloudStorage.online/11/SomeApplicationFile
15:11:37 + echo https://www.myCloudStorage.online/11/SomeApplicationFile
15:11:37 https://www.myCloudStorage.online/11/SomeApplicationFile
15:11:37 [Gradle] - Launching build.
15:11:37 [qae-verify-android-build] $ "/home/jenkins/workspace/my-workspace/validate/verify-android/gradlew" clean test --tests com.some.app.TESTS.* -DincludeTags=smoke -DwebhookUrl=https://hook.slack.com/services/98asdf9a87fg9a87fg9a87fga989f98 -DbuildUrl=${env.APK_URL}
15:11:38 Starting a Gradle Daemon, 1 busy Daemon could not be reused, use --status for details
15:11:44 > Task :clean
15:11:44 > Task :compileJava NO-SOURCE
15:11:44 > Task :processResources NO-SOURCE
15:11:44 > Task :classes UP-TO-DATE
15:11:54
15:11:54 > Task :compileTestJava
15:11:54 Note: Some input files use unchecked or unsafe operations.
15:11:54 Note: Recompile with -Xlint:unchecked for details.
15:11:58
15:11:58 > Task :processTestResources
15:11:58 > Task :testClasses
15:12:01
15:12:01 > Task :test
15:12:01
15:12:01 Gradle Test Executor 1 STANDARD_ERROR
15:12:01 SLF4J: No SLF4J providers were found.
15:12:01 SLF4J: Defaulting to no-operation (NOP) logger implementation
15:12:01 SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.
15:12:01 SLF4J: Class path contains SLF4J bindings targeting slf4j-api versions 1.7.x or earlier.
15:12:01 SLF4J: Ignoring binding found at [jar:file:/home/jenkins/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-log4j12/1.7.5/6edffc576ce104ec769d954618764f39f0f0f10d/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
15:12:01 SLF4J: See https://www.slf4j.org/codes.html#ignoredBindings for an explanation.
15:13:29
15:13:29 SomeRandomAndroidUiTest STANDARD_ERROR
15:13:29 com.synergy.core.exceptions.SessionNotStartedException: Failed to start session after 3 attempts with results:
[[{"results":"Session 525356b3-bd65-40bf-b9cb-fb855a4e2404 failed to start with error: Failed to decode app package file at
${env.APK_URL}. The AppPackage capability must be either a valid url to an Android APK package OR a valid file path to an
Android APK package!"},{"results":"Session 525356b3-bd65-40bf-b9cb-fb855a4e2404 failed to start with
error: Session 525356b3-bd65-40bf-b9cb-fb855a4e2404 did not have any entries in the device proxy log after app launch.
This is indicative of a network\/proxy issue with your device under test.
To disable this check set the VerifyNetworkConnectivity capability to false."}],[{"results":"Session a511602d-14ca-434f-9d96-93faf9ff5fb5
failed to start with error: Failed to decode app package file at ${env.APK_URL}.
The AppPackage capability must be either a valid url to an Android APK package OR a valid file path
to an Android APK package!"}],[{"results":"Session 5abd7803-4661-4cbb-aed7-d41ef0718f42 failed to start with error:
Failed to decode app package file at ${env.APK_URL}.
The AppPackage capability must be either a valid url to an Android APK package OR a valid file path to an Android APK package!"}]]
Solution
You've kind of exceeded the Jenkins model for the gradle plugin, which encourages a self-contained repeatable build. Once you go off getting things from other places like the cloud, it's not a self contained repeatable build.
There is an environment variable plugin, which might help you, but I'd recommend, don't use the gradle task, use the shell task and just put in your commands, as you have originally, being careful to exit with a failure exit code, if any stage fails.
APK_URL=$(curl -F file=@"$WORKSPACE/build/SomeProject" -H "Retention: $RETENTION_DAYS" [https://www.myCloudServer.online/fileuploader?key=$MY_KEY](https://www.myCloudServer.online/fileuploader?key=$MY_KEY) |
sed -n 's/.*"file_url":"\([^"]*\)".*/\1/p' | sed 's/\\//g') &&
./gradlew clean test --tests "appium.some.app.Tests.*" -DincludeTags=$RUN_TYPE -DwebhookUrl=[https://hooks.slack.com/services/someSlackChannel](https://hooks.slack.com/services/someSlackChannel) -DbuildUrl=${APK_URL}
Answered By - xpusostomos Answer Checked By - Dawn Plyler (WPSolving Volunteer)