Search Unity

How can I get apk/ipa file at the PostProcessBuild ?

Discussion in 'Unity Build Automation' started by ThainaYu, Apr 26, 2018.

  1. ThainaYu

    ThainaYu

    Joined:
    Nov 4, 2016
    Posts:
    18
    I would like to write a script in editor build process to just upload apk/ipa to another service that can distribute our app (HockeyApp specifically)

    I don't want to have another machine that just listen to webhook to download ipa/apk from your link and upload to another service. I would like to have upload those from the cloud build directly

    Can I do that? Is it possible?
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I believe you can create your own post build script (with [PostProcessBuild] attribute). I think it should be ok and you should be able to do whatever you need. never tested it myself though.
     
  3. ThainaYu

    ThainaYu

    Joined:
    Nov 4, 2016
    Posts:
    18
    @liortal There was 2 points here

    1 - Was unity PostProcessBuild triggered after creation of ipa/apk? And if it is how can I get the path to that file?

    2 - Can we do HTTP POST request in unity cloud build to the third party?
     
  4. dannyd

    dannyd

    Unity Technologies

    Joined:
    Jun 3, 2014
    Posts:
    785
    1. See the docs for PostProcessBuildAttribute - it's passed the path of the exported build. For Android that will be the APK. For iOS however, it's the exported Xcode project since Xcode needs to run separately to build the actual IPA.
    2. Yes. There should be some examples of others doing that in threads on the forums here.
     
  5. ThainaYu

    ThainaYu

    Joined:
    Nov 4, 2016
    Posts:
    18
    @dannyd Thank you very much but so you mean in cloud build we don't have a way to get ipa by the build script?

    Or we could have some observer logic wait for Xcode building to get ipa?
     
  6. dannyd

    dannyd

    Unity Technologies

    Joined:
    Jun 3, 2014
    Posts:
    785
    Ya there isn't a way to get the IPA with the PostProcessBuildAttribute or post-export method in UCB right now. I think the webhook method is probably the right answer for now.
     
  7. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,164
    @dannyd This is so sad what we have to spend our money to launch unity build machine that actually can upload IPA directly but we need to waste more money to create a server listening to webhook that really do nothing but download IPA from the link to upload it another place

    Pease fix this. If you could have webhook to ping 3rd party service. I think you should have a way to make a ping on localhost cloud machine that still running when the IPA was finished built
     
  8. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,164
    Is this issue has any plan for yet?
     
  9. sam_ettinger_sm

    sam_ettinger_sm

    Joined:
    Dec 1, 2016
    Posts:
    19
    Here are the shell scripts I use to automatically push builds to HockeyApp. Put these in your repo, then, under "Advanced Options", put in the appropriate script as a "Post-build script".

    For Android devices:
    Code (csharp):
    1. #!/bin/bash
    2.  
    3. # Instructions:
    4. #
    5. # 1) Replace the ____ on line 14 with your corresponding HockeyApp App Token. https://rink.hockeyapp.net/manage/auth_tokens
    6. # 2) Replace the ____ on line 15 with the file name of your APK. (This should be the same as your Build Target name, e.g. "Android Development")
    7. # 3) Commit this script to the Assets/Editor/ folder.
    8. # 4) In Cloud Build, edit the Advanced Settings for the build target for which you want to run the script, and add the relative path to the "Post Build Script Path" entry.
    9. # 5) For further details, or if API does not respond as expected, check HockeyApp documentation:
    10. #        https://support.hockeyapp.net/kb/api/api-apps#upload-app
    11. # 6) If HockeyApp responds "the app could not be created" confirm that the BundleID/package name output by UCB
    12. #    matches that in the project you are publishing to.
    13.  
    14. APP_TOKEN=____
    15. BUILD_TARGET="____"
    16.  
    17. curl \
    18. -F "ipa=@$2/${BUILD_TARGET}.apk" \
    19. -F "notes_type=1" \
    20. -F "status=2" \
    21. -F "notify=0" \
    22. -F "mandatory=0" \
    23. -H "X-HockeyAppToken: ${APP_TOKEN}" \
    24. https://rink.hockeyapp.net/api/2/apps/upload
    25.  
    For Apple devices:
    Code (csharp):
    1. #!/bin/bash
    2.  
    3. # Instructions:
    4. #
    5. # 1) Replace the ____ on line 13 with your corresponding HockeyApp App Token. https://rink.hockeyapp.net/manage/auth_tokens
    6. # 2) Commit this script to the Assets/Editor/ folder.
    7. # 3) In Cloud Build, edit the Advanced Settings for the build target for which you want to run the script, and add the relative path to the "Post Build Script Path" entry.
    8. # 4) For further details, or if API does not respond as expected, check HockeyApp documentation:
    9. #        https://support.hockeyapp.net/kb/api/api-apps#upload-app
    10. # 5) If HockeyApp responds "the app could not be created" confirm that the BundleID/package name output by UCB
    11. #    matches that in the project you are publishing to.
    12.  
    13. APP_TOKEN=____
    14.  
    15. curl \
    16. -F "ipa=@$2/build.ipa" \
    17. -F "dsym=@$2/build.app.dSYM.zip" \
    18. -F "notes_type=1" \
    19. -F "status=2" \
    20. -F "notify=0" \
    21. -F "mandatory=0" \
    22. -F "tags=dev" \
    23. -H "X-HockeyAppToken: ${APP_TOKEN}" \
    24. https://rink.hockeyapp.net/api/2/apps/upload
    25.  
    EDIT: one other thing to note: if you are using Unity 5.x or earlier, change the line
    -F "dsym=@$2/build.app.dSYM.zip" \
    so it reads
    -F "dsym=@$2/build.dSYM.zip" \
    instead.
     
    AvalonXT, lloydv and Thaina like this.
  10. lloydv

    lloydv

    Joined:
    Sep 15, 2015
    Posts:
    55
    @sam_ettinger_sm Thanks for this! I'm trying to setup auto-deployment to Deploygate and your example is very helpful. How did you figure out the "@$2" bit for the build path? Is there some documentation for that somewhere?
     
  11. EnduvoJD

    EnduvoJD

    Joined:
    Aug 12, 2016
    Posts:
    53

    For number 2, I have a lot of trouble finding information about doing HTTP requests in post build processes. Is the shell script with cURL above the way to go? I had UnityWebRequests working in Editor, but am having trouble with them in post build stuff.
     
  12. EnduvoJD

    EnduvoJD

    Joined:
    Aug 12, 2016
    Posts:
    53
    @dannyd Nevermind, got the HTTP requests in Unity Cloud post build working with this:

    Code (CSharp):
    1.     private static void TestSlackMessageBlockingUWR(string message)
    2.     {
    3.         string bodyJsonString = JsonUtility.ToJson(new SlackBasicMessage
    4.         {
    5.             text = message
    6.         }).ToString();
    7.  
    8.         using (var request = new UnityWebRequest(Webhook_URL, "POST"))
    9.         {
    10.             byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
    11.             request.uploadHandler = new UploadHandlerRaw(bodyRaw);
    12.             request.downloadHandler = new DownloadHandlerBuffer();
    13.             request.SetRequestHeader("Content-Type", "application/json");
    14.             request.SendWebRequest();
    15.             while (!request.isDone) ;
    16.             if (request.isNetworkError)
    17.             {
    18.                 Debug.Log("Request failed: " + request.error);
    19.             }
    20.             else
    21.             {
    22.                 Debug.Log("WWW result: " + request.downloadHandler.text);
    23.             }
    24.             return;
    25.         }
    26.     }
     
    zzzz789 likes this.