Skip to content

Commit

Permalink
[FIXED JENKINS-13932] Build can now be failed if package installation…
Browse files Browse the repository at this point in the history
… fails.
  • Loading branch information
orrc committed Apr 11, 2013
1 parent bcb3412 commit 59e2ec5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
24 changes: 21 additions & 3 deletions src/main/java/hudson/plugins/android_emulator/InstallBuilder.java
Expand Up @@ -14,10 +14,14 @@
import hudson.plugins.android_emulator.sdk.Tool;
import hudson.plugins.android_emulator.util.Utils;
import hudson.tasks.Builder;
import hudson.util.ForkOutputStream;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.json.JSONObject;

Expand All @@ -32,11 +36,15 @@ public class InstallBuilder extends AbstractBuilder {
/** Whether the APK should be uninstalled from the device before installation. */
private final boolean uninstallFirst;

/** Whether to fail the build if installation isn't successful. */
private final boolean failOnInstallFailure;

@DataBoundConstructor
@SuppressWarnings("hiding")
public InstallBuilder(String apkFile, boolean uninstallFirst) {
public InstallBuilder(String apkFile, boolean uninstallFirst, boolean failOnInstallFailure) {
this.apkFile = Util.fixEmptyAndTrim(apkFile);
this.uninstallFirst = uninstallFirst;
this.failOnInstallFailure = failOnInstallFailure;
}

public String getApkFile() {
Expand All @@ -47,6 +55,10 @@ public boolean shouldUninstallFirst() {
return uninstallFirst;
}

public boolean shouldFailBuildOnFailure() {
return failOnInstallFailure;
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
Expand Down Expand Up @@ -86,11 +98,17 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

// Execute installation
AndroidEmulator.log(logger, Messages.INSTALLING_APK(apkPath.getName()));
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ForkOutputStream forkStream = new ForkOutputStream(logger, stdout);
String args = String.format("%s install -r \"%s\"", deviceIdentifier, apkPath.getName());
Utils.runAndroidTool(launcher, build.getEnvironment(TaskListener.NULL), logger, logger,
Utils.runAndroidTool(launcher, build.getEnvironment(TaskListener.NULL), forkStream, logger,
androidSdk, Tool.ADB, args, apkPath.getParent());

// TODO: Evaluate success/failure and fail the build (if the user said we should do so)
Pattern p = Pattern.compile("^Success$", Pattern.MULTILINE);
boolean success = p.matcher(stdout.toString()).find();
if (!success && failOnInstallFailure) {
return false;
}
return true;
}

Expand Down
Expand Up @@ -7,8 +7,14 @@

<f:entry help="${resURL}/plugin/android-emulator/help-uninstallFirst.html">
<f:checkbox id="android-emulator.uninstallFirst" name="android-emulator.uninstallFirst"
title="${%Uninstall existing APK first}"
checked="${instance.shouldUninstallFirst()}" />
<label class="attach-previous">${%Uninstall existing APK first}</label>
</f:entry>

<f:entry help="${resURL}/plugin/android-emulator/help-failOnInstallFailure.html">
<f:checkbox id="android-emulator.failOnInstallFailure" name="android-emulator.failOnInstallFailure"
title="${%Fail the build if installation fails}"
checked="${h.defaultToTrue(instance.shouldFailBuildOnFailure())}" />
</f:entry>

</j:jelly>
2 changes: 2 additions & 0 deletions src/main/webapp/help-failOnInstallFailure.html
@@ -0,0 +1,2 @@
If the given APK fails to be installed (i.e. the Android package manager does not return "Success")
and this option is enabled, then the build will be marked as failed.

0 comments on commit 59e2ec5

Please sign in to comment.