Archive BEP files and JSON files for investigation if needed

More investigation after #91.

Change-Id: If7f66e8b5e9f8ab132d0ef3962a1cc82c874a48c
diff --git a/jenkins/lib/src/build/bazel/ci/BazelUtils.groovy b/jenkins/lib/src/build/bazel/ci/BazelUtils.groovy
index 843fa07..3fee4d1 100644
--- a/jenkins/lib/src/build/bazel/ci/BazelUtils.groovy
+++ b/jenkins/lib/src/build/bazel/ci/BazelUtils.groovy
@@ -157,6 +157,16 @@
   }
 
   @NonCPS
+  private static def copyCommands(cp_lines, log, test_folder) {
+    if (log != null) {
+      def uri = URI.create(log.uri)
+      def relativePath = uri.path.substring(uri.path.indexOf("/testlogs/") + 10)
+      cp_lines.add("mkdir -p \$(dirname '${test_folder}/${relativePath}')")
+      cp_lines.add("cp -r '${uri.path}' '${test_folder}/${relativePath}'")
+    }
+  }
+
+  @NonCPS
   def generateTestLogsCopy(events, test_folder) {
     // To avoid looking at all the files, including the stalled output log, we parse the events
     // from the build.
@@ -164,13 +174,13 @@
     def cp_lines = []
     events.each { event ->
       if("testResult" in event) {
-        def log = event.testResult.testActionOutput.find { it.name == "test.xml" }
-        if (log != null) {
-          def uri = URI.create(log.uri)
-          def relativePath = uri.path.substring(uri.path.indexOf("/testlogs/") + 10)
-          cp_lines.add("mkdir -p \$(dirname '${test_folder}/${relativePath}')")
-          cp_lines.add("cp -r '${uri.path}' '${test_folder}/${relativePath}'")
-        }
+        copyCommands(cp_lines,
+                     event.testResult.testActionOutput.find { it.name == "test.xml" },
+                     test_folder)
+        // Also copy the test log
+        copyCommands(cp_lines,
+                     event.testResult.testActionOutput.find { it.name == "test.log" },
+                     test_folder)
       }
     }
     return cp_lines.join('\n')
@@ -183,7 +193,16 @@
     def res = script.sh(
       script: "rm -fr ${test_folder}\n" + generateTestLogsCopy(testEvents(), test_folder),
       returnStatus: true)
+    // Archive BEP files
+    if (script.fileExists(BUILD_EVENTS_FILE)) {
+      script.archiveArtifacts artifacts: BUILD_EVENTS_FILE
+    }
+    if (script.fileExists(TEST_EVENTS_FILE)) {
+      script.archiveArtifacts artifacts: TEST_EVENTS_FILE
+    }
     if (res == 0) {
+      // Archive the test logs and xml files
+      script.archiveArtifacts artifacts: "${test_folder}/**/test.log"
       script.junit testResults: "${test_folder}/**/test.xml", allowEmptyResults: true
     }
   }