domingo, 17 de septiembre de 2017

Including a Jenkins link in the crashlytics report

I've been implementing some mobile application automatic tests using appium (appium.io). We also use Jenkins (https://jenkikns.io) as continuous integration tool. We compile and test the applications as Jenkins jobs. The apps include crashlytics (https://try.crashlytics.com/) to send a report everytime the application crashes. We are thinking it would be good to know the crash report was generated by a jenkins job and have a link to the offending job, so we can access all the logs that are stored as Jenkins artifacts. This how we do it. First we obtain the job url via Jenkins environment variables. This is the python code:

jenkins_url = os.environ ("JENKINS_URL") + "/job/" + os.environ ("JOB_NAME") + \
          "/" + os.environ ("BUILD_NUMBER")
Then we instruct appium to pass this url as an environment varaible to the application as desired capabilities. iOS

args = { "args": [], "env" : { "JENKINS_URL": jenkins_url }}
desired_caps["processArguments"] = json.dumps (args)
Android

desired_caps ["optionalIntentArguments"] = '--es "android.intent.extra.JENKINS_URL" ' +  '"' + \
        jenkins_url + '"'
Then, we read the value of the variable inside application code and set it as a crashlytics key. swift (iOS):

       NSString *jenkinsUrl = environment  [@"JENKINS_URL"];
       if  (jenkinsUrl) {
            [CrashlyticsKit setObjectValue:jenkinsUrl forKey:@"JENKINS_URL"];
       }
Java (android):

Intent intent = getIntent ();
String jenkins_url = intent.getStringExtra ("android.intent.extra.JENKINS_URL");

if (jenkins_url != null) {
    Crashlytics.setString ("JENKINS_URL", jenkins_url);
}