Using environment variables in fastlane step

I’m trying to use a fastlane step to send my APK to crashlytics, similarly as what I do from my own computer.

However I can’t get access the environment variable BITRISE_APK_PATH in my script.

I tried the following in my (local) fastfile and some variations but no success, I always the error message : “You have to either pass an ipa or an apk file to the Crashlytics action”

ApkPath = $BITRISE_APK_PATH

crashlytics(
apk_path: ApkPath
)

Thanks in advances for any help,

2 Likes

Hi @raphaelmina,

Thanks for asking this here! :wink:

fastlane uses Ruby as it’s config language, and in Ruby you reference environment variables with ENV['ENV_KEY']. In the case above: ENV['BITRISE_APK_PATH']. Note: there’s no $ in the ID!

The $ENV_KEY / ${ENV_KEY} environment variable value reference style is common in script languages like Bash, which is the most common “glue” script language on Mac/Linux, that’s why we opted to use that style in inputs. But in general, these are just environment variables, so you just have to check how in the language of your choice you can reference env vars.

If you have any questions just let us know!
Happy Building! :slight_smile:

Hi @viktorbenei,

Thanks for your answer and your precisions. Indeed, now I succesfully get the variable, since I’m able to display it. However, I have a subsequent issue, here is my log. Is this linked or should I create a separate topic ?

$ fastlane "fabric_upload"
[08:25:22]: -------------------------------------------------
[08:25:22]: --- Step: Verifying required fastlane version ---
[08:25:22]: -------------------------------------------------
[08:25:22]: Your fastlane version 2.23.0 matches the minimum requirement of 2.3.1  ✅
[08:25:22]: ------------------------------
[08:25:22]: --- Step: default_platform ---
[08:25:22]: ------------------------------
[08:25:22]: Driving the lane 'fabric_upload' 🚀
[08:25:22]: ApkPath is /bitrise/deploy/app-dev-release.apk //This is a puts
[08:25:22]: -------------------------
[08:25:22]: --- Step: crashlytics ---
[08:25:22]: -------------------------
[08:25:22]: Downloading Crashlytics Support Library - this might take a minute...
[08:25:24]: $ unzip '/root/Library/CrashlyticsAndroid/crashlytics-devtools.zip' -d '/root/Library/CrashlyticsAndroid'
[08:25:24]: ▸ Archive:  /root/Library/CrashlyticsAndroid/crashlytics-devtools.zip
[08:25:24]: ▸ inflating: /root/Library/CrashlyticsAndroid/crashlytics_build.xml
[08:25:24]: ▸ inflating: /root/Library/CrashlyticsAndroid/crashlytics_build_base.xml
[08:25:24]: ▸ inflating: /root/Library/CrashlyticsAndroid/crashlytics-devtools.jar
[08:25:24]: Succesfully downloaded Crashlytics Support Library to '/root/Library/CrashlyticsAndroid/crashlytics-devtools.jar'
[08:25:24]: Uploading the build to Crashlytics Beta. Time for some ☕️.
[08:25:25]:  WARN - The upload distribution task failed because it received invalid input. Contact support@fabric.io for help.
Exception in thread "main" com.crashlytics.tools.android.exception.PluginException: Distribution upload failed.
	at com.crashlytics.tools.android.DeveloperTools.processProperties(DeveloperTools.java:667)
	at com.crashlytics.tools.android.DeveloperTools.processArgsInternal(DeveloperTools.java:348)
	at com.crashlytics.tools.android.DeveloperTools.main(DeveloperTools.java:273)
Caused by: com.crashlytics.tools.android.exception.DistributionException: The upload distribution task failed because it received invalid input. Contact support@fabric.io for help.
	at com.crashlytics.tools.android.DistributionTasks.uploadDistribution(DistributionTasks.java:87)
	at com.crashlytics.tools.android.DeveloperTools.processProperties(DeveloperTools.java:665)
	... 2 more

+------------------+---------------+
|           Lane Context           |
+------------------+---------------+
| DEFAULT_PLATFORM | android       |
| PLATFORM_NAME    |               |
| LANE_NAME        | fabric_upload |
+------------------+---------------+
[08:25:25]: Shell command exited with exit status 1 instead of 0.

Are you sure that the APK is there? You should see the path in the log in the step’s log which generates the APK.

Just a debug tip: you can use our CLI to run the build locally on your Mac/Linux, should be easier/faster to debug it. More info e.g. here:

Thanks for the tip.

Yes, I’m quite sure it’s here, because the line
ApkPath is /bitrise/deploy/app-dev-release.apk
is actually the output of
"puts "ApkPath is #{ApkPath}"

And the Gradle Runner output shows :

Move apk files...
  find "." "-path" "*.apk" "!" "-path" "*-unaligned.apk"
  copy ./app/build/outputs/apk/app-prod-release.apk to /bitrise/deploy/app-prod-release.apk
  copy ./app/build/outputs/apk/app-dev-release.apk to /bitrise/deploy/app-dev-release.apk
  The apk path is now available in the Environment Variable: $BITRISE_APK_PATH (value: /bitrise/deploy/app-dev-release.apk)
1 Like

It might also be that the mentioned “input” is not the APK path (e.g. a missing API key or similar) - it’s probably better to contact fastlane support, there’s nothing obvious/bitrise related popping out of the log… The local debug I linked might help, maybe you can figure out what causes this issue that way.

Yes, I guess you’re right. I’ll try in local.

Thanks for the solution for the initial issue !

Any time, and let us know if we can help with anything!

Just in case it can help anyone, what worked for me was to access them by using environment_variable(get: "env_var_name") action from fastlane.
I attach an image for better understanding.