Search Unity

Question Unity Cloud Build Fastlane for Android

Discussion in 'Unity Build Automation' started by stevekrueger52, Oct 22, 2021.

  1. stevekrueger52

    stevekrueger52

    Joined:
    Jan 25, 2016
    Posts:
    2
    I'm currently building an app that targets both Android and iOS, and using Unity Cloud Build as a CI manager. Currently, I have one build profile each set up for iOS and Android, and in the iOS profile there's a field for specifying a custom fastfile configuration, since Unity relies on fastlane to deploy to AppStoreConnect.

    However, the Fastlane API suggests it also works with the Android platform and deploying to the Google Play Console, but there is no such option for it in the Android version of the Unity Cloud Build profile. It still builds the apps correctly which can be downloaded and uploaded to the Console manually, but I (and likely many others) would like the process to be automated as well.

    Is this a feature that is planned for a future release of Unity Cloud Build, and if so, when?

    Failing that, the best workaround I can think of is calling a shell script in the post-build script field, which in turn calls on Fastlane to run the lanes I want. How feasible is this? Is fastlane only installed in the iOS image of the cloud builder? Would I need the shell script to install/verify the existence of fastlane in order to execute the lanes I want to run?
     
    dan_ginovker likes this.
  2. raudas

    raudas

    Joined:
    Mar 18, 2021
    Posts:
    3
    I've been messing with this for a while. Used to have a ridiculously complicated bash script for this, until I realized that fastlane is installed, and you can easily use it in a bash script in the post build event. Now it's literally a 4 line script (minus comments), here it is:

    Code (csharp):
    1. #!/bin/bash
    2.  
    3. # Passed in as environment variables from CI, you must get this from Google and put
    4. # it in the environment variable PLAYSTORE_KEY in the Unity build config.
    5. # $PLAYSTORE_KEY - The JSON file with the credentials for the Google Developer account
    6.  
    7. # You can also just hardcode your package name, e.g. com.candycrush.game or whatever here...
    8. PACKAGE_NAME=$(cat "$WORKSPACE/build.json" | jq -j '.[].bundleid')
    9.  
    10. # Unity environment variables replace the "\n" signs from the private key with spaces for some reason,
    11. # so we replaces spaces with "\n" signs again so it works properly.
    12. KEY_WITH_NEWLINES=$(echo $PLAYSTORE_KEY | jq '.private_key |= sub(" (?!PRIVATE|KEY)"; "\n"; "g")' -c -j)
    13.  
    14. # You could also use shorter argument names here, but DO NOT use -e for --release-status, there's some error there where
    15. # fastlane thinks -e should mean the -env option and fails.
    16. # Also, you could put the "draft" and "internal" into environment variables if you want to never have to modify the script
    17. # again and just control it with environment variables.
    18. fastlane supply --package_name "$PACKAGE_NAME" --aab "$UNITY_PLAYER_PATH"  --json_key_data "$KEY_WITH_NEWLINES" --release-status draft --track internal
    19.  
    So just put that in a "upload.sh" file, add it as the post build file in the build config. Get your google key in json format and put it in as the env variable "PLAYSTORE_KEY". No additional fastlane configuration is needed, which was a pleasant surprise, it actually works with just the right parameters passed in.

    Hope this helps :)
     
  3. swiftster

    swiftster

    Joined:
    Dec 18, 2015
    Posts:
    2
    Thanks so much for sharing that script @raudas! We had been using firebase, but wanted to switch over to Google Play Console... this script worked right out of the box for that!
     
    raudas likes this.
  4. raudas

    raudas

    Joined:
    Mar 18, 2021
    Posts:
    3
    Great, happy to hear it helped :)
     
  5. Sharminator

    Sharminator

    Joined:
    Jul 24, 2020
    Posts:
    4
    Hi,

    Thanks for this. Trying it out myself but getting this error:

    Code (CSharp):
    1. 7683: Executing Post-Build Script at scripts/upload.sh
    2. 7684: [07:03:12]: [33mSeems like launching fastlane takes a while - please run[0m
    3. 7685: [07:03:12]:
    4. 7686: [07:03:12]: [36m$ [sudo] gem cleanup[0m
    5. 7687: [07:03:12]:
    6. 7688: [07:03:12]: [33mto uninstall outdated gems and make fastlane launch faster[0m
    7. 7689: [07:03:12]: [33mAlternatively it's recommended to start using a Gemfile to lock your dependencies[0m
    8. 7690: [07:03:12]: [33mTo get started with a Gemfile, run[0m
    9. 7691: [07:03:12]:
    10. 7692: [07:03:12]: [36m$ bundle init[0m
    11. 7693: [07:03:12]: [36m$ echo 'gem "fastlane"' >> Gemfile[0m
    12. 7694: [07:03:12]: [36m$ bundle install[0m
    13. 7695: [07:03:12]:
    14. 7696: [07:03:12]: [33mAfter creating the Gemfile and Gemfile.lock, commit those files into version control[0m
    15. 7697: [07:03:12]: [33mGet started using a Gemfile for fastlane https://docs.fastlane.tools/getting-started/ios/setup/#use-a-gemfile[0m
    16. 7698: [07:03:26]: Sending anonymous analytics information
    17. 7699: [07:03:26]: Learn more at https://docs.fastlane.tools/#metrics
    18. 7700: [07:03:26]: No personal or sensitive data is sent.
    19. 7701: [07:03:26]: You can disable this by adding `opt_out_usage` at the top of your Fastfile
    20. 7702: [07:03:26]: [31mError setting value 'BUILD_PATH/p/.build/last/default-android/Default Android.apk' for option 'aab'[0m
    21. 7703: [31m
    22. 7704: [!] aab file is not an aab[0m
    23. 7705: #######################################################################
    24. 7706: # fastlane 2.204.3 is available. You are on 2.182.0.
    25. 7707: # You should use the latest version.
    26. 7708: # Please update using `gem install fastlane`.
    27. 7709: #######################################################################
    28. 7710: [32m2.204.3 Improvements[0m
    29. 7711: * [trainer][scan] identify skipped tests in `xcresult` and export to Junit format and output in scan (#19957) via Igor Makarov
    30. 7712: * [Fastlane.Swift] Swift fastlane upgrader #18933 (#19914) via Enrique Garcia
    31. 7713: * [pem][spaceship] update development push certificate type ID (#19879) via Igor Makarov
    32. 7714: * [snapshot] fix compile error on macCatalyst (#19917) via Philipp Arndt
    33. 7715: * [Fastlane.Swift] readPodspec: return map of [String: Any] (#19953) via Hais Deakin
    34. 7716: * [match] update :force_for_new_certificates option description (#19938) via Wolfgang Lutz
    35. 7717: [32m2.204.2 App Store Connect API is fixed - reverts local filtering[0m
    36. 7718: * Revert "[spaceship][deliver][pilot] temporarily fix finding app by filtering by bundle id locally (#19900)" (#19906) via Josh Holtz (@joshdholtz)
    37. 7719: Version `2.204.1` will be removed from RubyGems as the temporary fix is no longer needed
    38. 7720: [32m2.204.1 Temporary App Store Connect Fix[0m
    39. 7721: * [spaceship][deliver][pilot] temporarily fix finding app by filtering by bundle id locally (#19900) via Josh Holtz (@joshdholtz)
    40. 7722: [32mTo see all new releases, open https://github.com/fastlane/fastlane/releases[0m
    41. 7723: [32mPlease update using `gem install fastlane`[0m
    42. 7724: ! build of 'default-android' failed. Post-Build script exited with status 1. Aborting.
    43. 7725: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/bvr-3.8.7.2.4.2/lib/bvr/build/build_handler.rb:104:in `compile'
    44. 7726: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/bvr-3.8.7.2.4.2/lib/bvr/project.rb:129:in `call'
    45. 7727: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/bvr-3.8.7.2.4.2/lib/bvr/project.rb:129:in `build_chain'
    46. 7728: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/bvr-3.8.7.2.4.2/lib/bvr/project.rb:80:in `build'
    47. 7729: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/bvr-3.8.7.2.4.2/lib/bvr/thor/tasks/project.rb:288:in `call'
    48. 7730: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/bvr-3.8.7.2.4.2/lib/bvr/thor/tasks/project.rb:288:in `build_steps'
    49. 7731: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/bvr-3.8.7.2.4.2/lib/bvr/thor/tasks/project.rb:117:in `build'
    50. 7732: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor/command.rb:27:in `run'
    51. 7733: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor/invocation.rb:126:in `invoke_command'
    52. 7734: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor.rb:369:in `dispatch'
    53. 7735: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor/base.rb:444:in `start'
    54. 7736: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor/runner.rb:44:in `method_missing'
    55. 7737: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/bvr-3.8.7.2.4.2/lib/bvr/thor/runner.rb:25:in `block in method_missing'
    56. 7738: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/bvr-3.8.7.2.4.2/lib/bvr/timing.rb:10:in `wrap'
    57. 7739: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/bvr-3.8.7.2.4.2/lib/bvr/thor/runner.rb:24:in `method_missing'
    58. 7740: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor/command.rb:29:in `run'
    59. 7741: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor/command.rb:126:in `run'
    60. 7742: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor/invocation.rb:126:in `invoke_command'
    61. 7743: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor.rb:369:in `dispatch'
    62. 7744: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor/base.rb:444:in `start'
    63. 7745: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/bvr-3.8.7.2.4.2/lib/bvr/thor/tasks/buildjobs.rb:120:in `buildscript'
    64. 7746: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor/command.rb:27:in `run'
    65. 7747: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor/invocation.rb:126:in `invoke_command'
    66. 7748: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor.rb:369:in `dispatch'
    67. 7749: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor/base.rb:444:in `start'
    68. 7750: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor/runner.rb:44:in `method_missing'
    69. 7751: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/bvr-3.8.7.2.4.2/lib/bvr/thor/runner.rb:25:in `block in method_missing'
    70. 7752: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/bvr-3.8.7.2.4.2/lib/bvr/timing.rb:10:in `wrap'
    71. 7753: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/bvr-3.8.7.2.4.2/lib/bvr/thor/runner.rb:24:in `method_missing'
    72. 7754: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor/command.rb:29:in `run'
    73. 7755: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor/command.rb:126:in `run'
    74. 7756: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor/invocation.rb:126:in `invoke_command'
    75. 7757: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor.rb:369:in `dispatch'
    76. 7758: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/thor-0.19.4/lib/thor/base.rb:444:in `start'
    77. 7759: /home/buildbot/.rvm/gems/ruby-2.4.2/gems/bvr-3.8.7.2.4.2/bin/bvr_runner:11:in `<main>'
    78. 7760: Publishing build 2 of 16767722914694/167929e7-f8c5-4349-b265-cffd612f9fd6 for target 'default-android'...
    79. 7761: Uploading extra_data/artifacts/icon.png
    80. 7762:   ...done
    81. 7763: Uploading extra_data/build_report/files.json
    82. 7764:   ...done
    83. 7765: Uploading extra_data/build_report/files.reflected.json  ...done
    84. 7766: Uploading extra_data/build_report/steps.reflected.json  ...done
    85. 7767: Uploading extra_data/build_report/strippingInfo.json
    86. 7768:   ...done
    87. 7769: Uploading extra_data/build_report/summary.json
    88. 7770:   ...done
    89. 7771: Uploading extra_data/build_report/summary.reflected.json
    90. 7772:   ...done
    91. 7773: Uploading extra_data/build_report/v2.steps.json
    92. 7774:   ...done
    93. 7775: Uploading Default Android.apk
    94. 7776:   ...done
    95. 7777: Zipping cache files from Library
    96. 7778: done.
    97. 7779: publishing finished successfully.
    98. 7780: Build failed. Please check the log for further details.
    99. 7781: Using /home/buildbot/.rvm/gems/ruby-2.4.2
    100. 7782: postbuildsteps finished successfully
    101. 7783: postbuildstatus finished successfully.
    102. 7784: Attempting to return legacy license
    103. 7785: No legacy license to return. Exiting
    104. 7786: Finished: FAILURE
     
  6. unity_2686C15E0933CD0D93D9

    unity_2686C15E0933CD0D93D9

    Joined:
    Jun 5, 2021
    Posts:
    1
    Hi everyone, I'm trying this solution but I got this error:

    Google Api Error: forbidden: The caller does not have permission - The caller does not have permission

    Tried a lot of things but didn't work. I hope anyone can help me
     
    dan_ginovker likes this.
  7. L4ZZAR8

    L4ZZAR8

    Joined:
    Jan 18, 2021
    Posts:
    22
    Where do you guys get what to put into the PLAYSTORE_KEY environment variable from?
    Is it from here?
     
  8. FanLee

    FanLee

    Joined:
    Jan 5, 2021
    Posts:
    1
    i try to find solution for the same question) where i can find this "PLAYSTORE_KEY"?

    ok, i find where i can get this key - https://docs.fastlane.tools/getting-started/android/setup/

    but i have a new problem -
    The Android App Bundle was signed with the wrong key. Found: SHA1: bla-bla-bla, expected: not bla-bla-bla
     
    Last edited: May 23, 2022
  9. L4ZZAR8

    L4ZZAR8

    Joined:
    Jan 18, 2021
    Posts:
    22
    could anyone post the iOS version of this so we upload to the appstore?
     
  10. jmassey-tkg

    jmassey-tkg

    Joined:
    Mar 11, 2020
    Posts:
    7
    Another +1 @raudas, thanks for this tip with fastlane and jq.

    One more thing I would add to the above post is disabling xtrace and verbose mode for it:

    set +x
    set +v
    # the script
    set -x
    set -v

    Without this, at least in the current incarnation of UCB, all of your raw secrets will be printed in plain text to the build log when xtrace mode prints them out after expansion.

    For testflight, I haven't experimented with fastlane, but we are able to use the xcode command line pretty easily for uploads:

    if xcrun altool --upload-app -f $UNITY_PLAYER_PATH -u $APPLE_USERID -p $APPLE_PASSWORD --type ios; then
    echo "Uploading to Testflight successful"
    else
    echo "Failed to upload to testflight"
    fi

    Where the apple userid and password env vars are set to whichever appstoreconnect account you want to upload with.

    Fastlane supply is only for google play. Fastlane has the deliver/appstore/upload_to_app_store action for testflight:
    https://docs.fastlane.tools/actions/upload_to_app_store/
     
  11. L4ZZAR8

    L4ZZAR8

    Joined:
    Jan 18, 2021
    Posts:
    22
    Correct, but none of the deliver/appstore/upload_to_app_store aliases actually work for me..

    see here: https://github.com/fastlane/fastlane/issues/20387
    and here: https://forum.unity.com/threads/fastlane-for-ios-and-android.1019485/#post-8224878


    The following just doesn't authenticate..
    Code (CSharp):
    1. #!/bin/bash
    2.  
    3. build_folder_path="$WORKSPACE/.build/last/$TARGET_NAME"
    4. ipa_path="$build_folder_path/build.ipa"
    5.  
    6. echo "###############################################"
    7. echo "Uploading app to TestFlight..."
    8. echo "-----------------------------------------------"
    9. pwd
    10. echo "-----------------------------------------------"
    11. env
    12. echo "-----------------------------------------------"
    13. ls -a .
    14. echo "-----------------------------------------------"
    15. echo "build.json"
    16. cat build.json
    17. echo "-----------------------------------------------"
    18.  
    19. exit_code=0
    20. if fastlane upload_to_testflight --api_key_path "fastlane/api_key_info.json" --ipa "$ipa_path" ; then
    21.     echo "======== Upload IPA to Appstore Connect finished with success"
    22.     # end without error
    23.     exit_code=0
    24. else
    25.     echo "======== ERROR! Upload IPA to Appstore Connect failed"
    26.     echo "-------- Current Folder Content"
    27.     ls -a .
    28.     echo "-----------------------------------------------"
    29.     bundle exec fastlane env
    30.     echo "-----------------------------------------------"
    31.     # end with error
    32.     exit_code=1
    33. fi
    34.  
    35. echo "###############################################"
    36. exit $exit_code
     
  12. doganakdag

    doganakdag

    Joined:
    Oct 25, 2019
    Posts:
    14
    I used this documentation to create my fast lane folder but I don't know how to start upload operation. Is there any example script for that?
     
  13. mruce

    mruce

    Joined:
    Jul 23, 2012
    Posts:
    28
    Have you found the solution? I have the same issue.
     
  14. RobinopdeBeek

    RobinopdeBeek

    Joined:
    Jan 17, 2018
    Posts:
    23
    I've almost got things working, but I'm still getting stuck at:
    "No local metadata, apks, aab, or track to promote were found, make sure to run `fastlane supply init` to setup supply"

    When I try to put `fastlane supply init` in front of fastlane supply I get:
    "[!] No authentication parameters were specified. These must be provided in order to authenticate with Google"

    Looking at the fastlane docs it says that you have to run `fastlane supply init` from [your_project_folder]? Does anyone know how to do this in the UCB environment?

    Didn't you @raudas have to do something similar? Or did you work around this?

    UPDATE
    had a woopsie in my fastlane supply call. I had '-aab' instead of '--aab'. This solved it for me :)
    And thanks for the script!
     
    Last edited: Apr 13, 2023
  15. RobinopdeBeek

    RobinopdeBeek

    Joined:
    Jan 17, 2018
    Posts:
    23