コード例 #1
0
ファイル: AbstractBuild.java プロジェクト: mervynli/hudson
  @Override
  public EnvVars getEnvironment(TaskListener log) throws IOException, InterruptedException {
    EnvVars env = super.getEnvironment(log);
    FilePath ws = getWorkspace();
    if (ws
        != null) // if this is done very early on in the build, workspace may not be decided yet.
                 // see HUDSON-3997
    env.put("WORKSPACE", ws.getRemote());
    // servlet container may have set CLASSPATH in its launch script,
    // so don't let that inherit to the new child process.
    // see http://www.nabble.com/Run-Job-with-JDK-1.4.2-tf4468601.html
    env.put("CLASSPATH", "");

    JDK jdk = project.getJDK();
    if (jdk != null) {
      Computer computer = Computer.currentComputer();
      if (computer != null) { // just in case were not in a build
        jdk = jdk.forNode(computer.getNode(), log);
      }
      jdk.buildEnvVars(env);
    }
    project.getScm().buildEnvVars(this, env);

    if (buildEnvironments != null) for (Environment e : buildEnvironments) e.buildEnvVars(env);

    for (EnvironmentContributingAction a :
        Util.filter(getActions(), EnvironmentContributingAction.class)) a.buildEnvVars(this, env);

    EnvVars.resolve(env);

    return env;
  }
コード例 #2
0
 /** Exposes the name/value as an environment variable. */
 @Override
 public void buildEnvVars(AbstractBuild<?, ?> build, EnvVars env) {
   env.put(name, Boolean.toString(value));
   env.put(
       name.toUpperCase(Locale.ENGLISH),
       Boolean.toString(value)); // backward compatibility pre 1.345
 }
コード例 #3
0
ファイル: GitUtils.java プロジェクト: pnyheim/git-plugin
  /**
   * An attempt to generate at least semi-useful EnvVars for polling calls, based on previous build.
   * Cribbed from various places.
   */
  public static EnvVars getPollEnvironment(
      AbstractProject p, FilePath ws, Launcher launcher, TaskListener listener)
      throws IOException, InterruptedException {
    EnvVars env;

    AbstractBuild b = (AbstractBuild) p.getLastBuild();

    if (b != null) {
      Node lastBuiltOn = b.getBuiltOn();

      if (lastBuiltOn != null) {
        env = lastBuiltOn.toComputer().getEnvironment().overrideAll(b.getCharacteristicEnvVars());
      } else {
        env = new EnvVars(System.getenv());
      }

      String rootUrl = Hudson.getInstance().getRootUrl();
      if (rootUrl != null) {
        env.put("HUDSON_URL", rootUrl);
        env.put("BUILD_URL", rootUrl + b.getUrl());
        env.put("JOB_URL", rootUrl + p.getUrl());
      }

      if (!env.containsKey("HUDSON_HOME")) {
        env.put("HUDSON_HOME", Hudson.getInstance().getRootDir().getPath());
      }

      if (ws != null) {
        env.put("WORKSPACE", ws.getRemote());
      }

      p.getScm().buildEnvVars(b, env);

      StreamBuildListener buildListener =
          new StreamBuildListener((OutputStream) listener.getLogger());

      for (NodeProperty nodeProperty : Hudson.getInstance().getGlobalNodeProperties()) {
        Environment environment = nodeProperty.setUp(b, launcher, (BuildListener) buildListener);
        if (environment != null) {
          environment.buildEnvVars(env);
        }
      }

      if (lastBuiltOn != null) {
        for (NodeProperty nodeProperty : lastBuiltOn.getNodeProperties()) {
          Environment environment = nodeProperty.setUp(b, launcher, buildListener);
          if (environment != null) {
            environment.buildEnvVars(env);
          }
        }
      }

      EnvVars.resolve(env);
    } else {
      env = new EnvVars(System.getenv());
    }

    return env;
  }
コード例 #4
0
 public void setUp() throws Exception {
   super.setUp();
   EnvVars env = new EnvVars();
   // we don't want Maven, Ant, etc. to be discovered in the path for this test to work,
   // but on Unix these tools rely on other basic Unix tools (like env) for its operation,
   // so empty path breaks the test.
   env.put("PATH", "/bin:/usr/bin");
   env.put("M2_HOME", "empty");
   slave = createSlave(new LabelAtom("slave"), env);
   project = createFreeStyleProject();
   project.setAssignedLabel(slave.getSelfLabel());
 }
コード例 #5
0
ファイル: MavenBuild.java プロジェクト: kiy0taka/jenkins
  /**
   * Exposes {@code MAVEN_OPTS} to forked processes.
   *
   * <p>See {@link MavenModuleSetBuild#getEnvironment(TaskListener)} for discussion.
   */
  @Override
  public EnvVars getEnvironment(TaskListener log) throws IOException, InterruptedException {
    EnvVars envs = super.getEnvironment(log);

    // We need to add M2_HOME and the mvn binary to the PATH so if Maven
    // needs to run Maven it will pick the correct one.
    // This can happen if maven calls ANT which itself calls Maven
    // or if Maven calls itself e.g. maven-release-plugin
    MavenInstallation mvn = project.getParent().getMaven();
    if (mvn == null)
      throw new hudson.AbortException(Messages.MavenModuleSetBuild_NoMavenConfigured());
    mvn = mvn.forEnvironment(envs).forNode(Computer.currentComputer().getNode(), log);
    envs.put("M2_HOME", mvn.getHome());
    envs.put("PATH+MAVEN", mvn.getHome() + "/bin");
    return envs;
  }
コード例 #6
0
  @Test
  public void testConvertRecipientList_recipientStringShouldBeExpanded()
      throws AddressException, UnsupportedEncodingException {
    envVars.put("EMAIL_LIST", "*****@*****.**");

    Set<InternetAddress> internetAddresses =
        emailRecipientUtils.convertRecipientString("$EMAIL_LIST", envVars);

    assertEquals(1, internetAddresses.size());
    assertTrue(internetAddresses.contains(new InternetAddress("*****@*****.**")));
  }
コード例 #7
0
  @Test
  public void assertComputerEnvVarIsResolved() throws Exception {
    EnvVars map = new EnvVars();
    map.put("ENV_VAR", "This is an env var");

    when(computer.getEnvironment()).thenReturn(map);

    BuildVariableResolver resolver = new BuildVariableResolver(project, computer);
    assertEquals(
        "Variable resolution was incorrect", "This is an env var", resolver.resolve("ENV_VAR"));
    verifyZeroInteractions(project);
  }
コード例 #8
0
  @Test
  public void assertBuildEnvVarIsResolved() throws Exception {
    EnvVars map = new EnvVars();
    map.put("BUILD_ID", "121212");

    when(build.getProject()).thenReturn(project);
    when(build.getEnvironment(TaskListener.NULL)).thenReturn(map);

    BuildVariableResolver resolver = new BuildVariableResolver(build, computer);
    assertEquals("Variable resolution was incorrect", "121212", resolver.resolve("BUILD_ID"));
    verify(build).getEnvironment(TaskListener.NULL);
    verifyZeroInteractions(project);
  }
コード例 #9
0
ファイル: Computer.java プロジェクト: recampbell/jenkins
  /**
   * Creates an environment variable override to be used for launching processes on this node.
   *
   * @see ProcStarter#envs(Map)
   * @since 1.489
   */
  public EnvVars buildEnvironment(TaskListener listener) throws IOException, InterruptedException {
    EnvVars env = new EnvVars();

    Node node = getNode();
    if (node == null) return env; // bail out

    for (NodeProperty nodeProperty : Jenkins.getInstance().getGlobalNodeProperties()) {
      nodeProperty.buildEnvVars(env, listener);
    }

    for (NodeProperty nodeProperty : node.getNodeProperties()) {
      nodeProperty.buildEnvVars(env, listener);
    }

    // TODO: hmm, they don't really belong
    String rootUrl = Hudson.getInstance().getRootUrl();
    if (rootUrl != null) {
      env.put("HUDSON_URL", rootUrl); // Legacy.
      env.put("JENKINS_URL", rootUrl);
    }

    return env;
  }
コード例 #10
0
  /** Tests the generated list of custom fields. */
  @Bug(13173)
  public void testListOfCustomFields() {
    EnvVars envVars = new EnvVars();
    @SuppressWarnings({"unchecked", "rawtypes"})
    VariableResolver<String> varRes = new ByMap.ByMap(envVars);

    envVars.put("BUILD_ID", "1");
    String[] customFieldsNames = builder.createArrayOfCustomFieldsNames(varRes, envVars);

    assertNotNull(customFieldsNames);
    assertTrue(customFieldsNames.length == 3);
    assertEquals(customFieldsNames[0], "class");
    assertEquals(customFieldsNames[1], "time");
    assertEquals(customFieldsNames[2], "sample-job-1");
  }
コード例 #11
0
  @Test
  public void testCC() throws Exception {
    envVars.put("EMAIL_LIST", "[email protected], cc:[email protected], [email protected]");

    Set<InternetAddress> internetAddresses =
        emailRecipientUtils.convertRecipientString("$EMAIL_LIST", envVars);

    assertEquals(2, internetAddresses.size());
    assertTrue(internetAddresses.contains(new InternetAddress("*****@*****.**")));
    assertTrue(internetAddresses.contains(new InternetAddress("*****@*****.**")));

    internetAddresses =
        emailRecipientUtils.convertRecipientString("$EMAIL_LIST", envVars, EmailRecipientUtils.CC);

    assertEquals(1, internetAddresses.size());
    assertTrue(internetAddresses.contains(new InternetAddress("*****@*****.**")));
  }
コード例 #12
0
  @Test
  public void testUTF8() throws Exception {
    envVars.put("EMAIL_LIST", "[email protected], cc:[email protected], 愛嬋 <*****@*****.**>");

    Set<InternetAddress> internetAddresses =
        emailRecipientUtils.convertRecipientString("$EMAIL_LIST", envVars);
    assertEquals(2, internetAddresses.size());

    assertTrue(internetAddresses.contains(new InternetAddress("*****@*****.**")));
    InternetAddress addr = new InternetAddress("*****@*****.**");
    addr.setPersonal(MimeUtility.encodeWord("愛嬋", "UTF-8", "B"));
    assertTrue(internetAddresses.contains(addr));

    internetAddresses =
        emailRecipientUtils.convertRecipientString("$EMAIL_LIST", envVars, EmailRecipientUtils.CC);

    assertEquals(1, internetAddresses.size());
    assertTrue(internetAddresses.contains(new InternetAddress("*****@*****.**")));
  }
コード例 #13
0
  @Override
  public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, final BuildListener listener)
      throws InterruptedException {

    // during matrix build, the push back would happen at the very end only once for the whole
    // matrix,
    // not for individual configuration build.
    if (build instanceof MatrixRun) {
      return true;
    }

    SCM scm = build.getProject().getScm();

    if (!(scm instanceof GitSCM)) {
      return false;
    }

    final GitSCM gitSCM = (GitSCM) scm;

    if (gitSCM.getUseShallowClone()) {
      listener.getLogger().println("GitPublisher disabled while using shallow clone.");
      return true;
    }

    final String projectName = build.getProject().getName();
    final FilePath workspacePath = build.getWorkspace();
    final int buildNumber = build.getNumber();
    final Result buildResult = build.getResult();

    // If pushOnlyIfSuccess is selected and the build is not a success, don't push.
    if (pushOnlyIfSuccess && buildResult.isWorseThan(Result.SUCCESS)) {
      listener
          .getLogger()
          .println(
              "Build did not succeed and the project is configured to only push after a successful build, so no pushing will occur.");
      return true;
    } else {
      final String gitExe = gitSCM.getGitExe(build.getBuiltOn(), listener);
      EnvVars tempEnvironment;
      try {
        tempEnvironment = build.getEnvironment(listener);
      } catch (IOException e) {
        e.printStackTrace(listener.error("Failed to build up environment"));
        tempEnvironment = new EnvVars();
      }

      String confName = gitSCM.getGitConfigNameToUse();
      if ((confName != null) && (!confName.equals(""))) {
        tempEnvironment.put("GIT_COMMITTER_NAME", confName);
        tempEnvironment.put("GIT_AUTHOR_NAME", confName);
      }
      String confEmail = gitSCM.getGitConfigEmailToUse();
      if ((confEmail != null) && (!confEmail.equals(""))) {
        tempEnvironment.put("GIT_COMMITTER_EMAIL", confEmail);
        tempEnvironment.put("GIT_AUTHOR_EMAIL", confEmail);
      }

      final EnvVars environment = tempEnvironment;
      final FilePath workingDirectory = gitSCM.workingDirectory(workspacePath, environment);

      boolean pushResult = true;
      // If we're pushing the merge back...
      if (pushMerge) {
        boolean mergeResult;
        try {
          mergeResult =
              workingDirectory.act(
                  new FileCallable<Boolean>() {
                    private static final long serialVersionUID = 1L;

                    public Boolean invoke(File workspace, VirtualChannel channel)
                        throws IOException {

                      IGitAPI git =
                          new GitAPI(gitExe, new FilePath(workspace), listener, environment);

                      // We delete the old tag generated by the SCM plugin
                      String buildnumber = "jenkins-" + projectName + "-" + buildNumber;
                      git.deleteTag(buildnumber);

                      // And add the success / fail state into the tag.
                      buildnumber += "-" + buildResult.toString();

                      git.tag(buildnumber, "Jenkins Build #" + buildNumber);

                      PreBuildMergeOptions mergeOptions = gitSCM.getMergeOptions();

                      if (mergeOptions.doMerge() && buildResult.isBetterOrEqualTo(Result.SUCCESS)) {
                        RemoteConfig remote = mergeOptions.getMergeRemote();
                        listener
                            .getLogger()
                            .println(
                                "Pushing HEAD to branch "
                                    + mergeOptions.getMergeTarget()
                                    + " of "
                                    + remote.getName()
                                    + " repository");

                        git.push(remote, "HEAD:" + mergeOptions.getMergeTarget());
                      } else {
                        // listener.getLogger().println("Pushing result " + buildnumber + " to
                        // origin repository");
                        // git.push(null);
                      }

                      return true;
                    }
                  });
        } catch (Throwable e) {
          e.printStackTrace(listener.error("Failed to push merge to origin repository"));
          build.setResult(Result.FAILURE);
          mergeResult = false;
        }

        if (!mergeResult) {
          pushResult = false;
        }
      }
      if (isPushTags()) {
        boolean allTagsResult = true;
        for (final TagToPush t : tagsToPush) {
          boolean tagResult = true;
          if (t.getTagName() == null) {
            listener.getLogger().println("No tag to push defined");
            tagResult = false;
          }
          if (t.getTargetRepoName() == null) {
            listener.getLogger().println("No target repo to push to defined");
            tagResult = false;
          }
          if (tagResult) {
            final String tagName = environment.expand(t.getTagName());
            final String targetRepo = environment.expand(t.getTargetRepoName());

            try {
              tagResult =
                  workingDirectory.act(
                      new FileCallable<Boolean>() {
                        private static final long serialVersionUID = 1L;

                        public Boolean invoke(File workspace, VirtualChannel channel)
                            throws IOException {

                          IGitAPI git =
                              new GitAPI(gitExe, new FilePath(workspace), listener, environment);

                          RemoteConfig remote = gitSCM.getRepositoryByName(targetRepo);

                          if (remote == null) {
                            listener
                                .getLogger()
                                .println("No repository found for target repo name " + targetRepo);
                            return false;
                          }

                          if (t.isCreateTag()) {
                            if (git.tagExists(tagName)) {
                              listener
                                  .getLogger()
                                  .println(
                                      "Tag "
                                          + tagName
                                          + " already exists and Create Tag is specified, so failing.");
                              return false;
                            }
                            git.tag(tagName, "Jenkins Git plugin tagging with " + tagName);
                          } else if (!git.tagExists(tagName)) {
                            listener
                                .getLogger()
                                .println(
                                    "Tag "
                                        + tagName
                                        + " does not exist and Create Tag is not specified, so failing.");
                            return false;
                          }

                          listener
                              .getLogger()
                              .println("Pushing tag " + tagName + " to repo " + targetRepo);
                          git.push(remote, tagName);

                          return true;
                        }
                      });
            } catch (Throwable e) {
              e.printStackTrace(
                  listener.error("Failed to push tag " + tagName + " to " + targetRepo));
              build.setResult(Result.FAILURE);
              tagResult = false;
            }
          }

          if (!tagResult) {
            allTagsResult = false;
          }
        }
        if (!allTagsResult) {
          pushResult = false;
        }
      }

      if (isPushBranches()) {
        boolean allBranchesResult = true;
        for (final BranchToPush b : branchesToPush) {
          boolean branchResult = true;
          if (b.getBranchName() == null) {
            listener.getLogger().println("No branch to push defined");
            return false;
          }
          if (b.getTargetRepoName() == null) {
            listener.getLogger().println("No branch repo to push to defined");
            return false;
          }
          final String branchName = environment.expand(b.getBranchName());
          final String targetRepo = environment.expand(b.getTargetRepoName());

          if (branchResult) {
            try {
              branchResult =
                  workingDirectory.act(
                      new FileCallable<Boolean>() {
                        private static final long serialVersionUID = 1L;

                        public Boolean invoke(File workspace, VirtualChannel channel)
                            throws IOException {

                          IGitAPI git =
                              new GitAPI(gitExe, new FilePath(workspace), listener, environment);

                          RemoteConfig remote = gitSCM.getRepositoryByName(targetRepo);

                          if (remote == null) {
                            listener
                                .getLogger()
                                .println("No repository found for target repo name " + targetRepo);
                            return false;
                          }

                          listener
                              .getLogger()
                              .println(
                                  "Pushing HEAD to branch "
                                      + branchName
                                      + " at repo "
                                      + targetRepo);
                          git.push(remote, "HEAD:" + branchName);

                          return true;
                        }
                      });
            } catch (Throwable e) {
              e.printStackTrace(
                  listener.error("Failed to push branch " + branchName + " to " + targetRepo));
              build.setResult(Result.FAILURE);
              branchResult = false;
            }
          }

          if (!branchResult) {
            allBranchesResult = false;
          }
        }
        if (!allBranchesResult) {
          pushResult = false;
        }
      }

      if (isPushNotes()) {
        boolean allNotesResult = true;
        for (final NoteToPush b : notesToPush) {
          boolean noteResult = true;
          if (b.getnoteMsg() == null) {
            listener.getLogger().println("No note to push defined");
            return false;
          }

          b.setEmptyTargetRepoToOrigin();
          final String noteMsg = environment.expand(b.getnoteMsg());
          final String noteNamespace = environment.expand(b.getnoteNamespace());
          final String targetRepo = environment.expand(b.getTargetRepoName());
          final boolean noteReplace = b.getnoteReplace();

          if (noteResult) {
            try {
              noteResult =
                  workingDirectory.act(
                      new FileCallable<Boolean>() {
                        private static final long serialVersionUID = 1L;

                        public Boolean invoke(File workspace, VirtualChannel channel)
                            throws IOException {

                          IGitAPI git =
                              new GitAPI(gitExe, new FilePath(workspace), listener, environment);

                          RemoteConfig remote = gitSCM.getRepositoryByName(targetRepo);

                          if (remote == null) {
                            listener
                                .getLogger()
                                .println("No repository found for target repo name " + targetRepo);
                            return false;
                          }

                          listener
                              .getLogger()
                              .println(
                                  "Adding note \""
                                      + noteMsg
                                      + "\" to namespace \""
                                      + noteNamespace
                                      + "\"");

                          if (noteReplace) git.addNote(noteMsg, noteNamespace);
                          else git.appendNote(noteMsg, noteNamespace);

                          git.push(remote, "refs/notes/*");

                          return true;
                        }
                      });
            } catch (Throwable e) {
              e.printStackTrace(
                  listener.error(
                      "Failed to add note \"" + noteMsg + "\" to \"" + noteNamespace + "\""));
              build.setResult(Result.FAILURE);
              noteResult = false;
            }
          }

          if (!noteResult) {
            allNotesResult = false;
          }
        }
        if (!allNotesResult) {
          pushResult = false;
        }
      }

      return pushResult;
    }
  }
コード例 #14
0
 /**
  * Sets up a standard {@link ProcStarter} for the current context.
  *
  * @return A ready ProcStarter
  * @throws IOException
  * @throws InterruptedException
  */
 public ProcStarter getProcStarter() throws IOException, InterruptedException {
   final EnvVars buildEnvironment = build.getEnvironment(TaskListener.NULL);
   buildEnvironment.put("ANDROID_ADB_SERVER_PORT", Integer.toString(adbServerPort));
   return launcher.launch().stdout(new NullStream()).stderr(logger()).envs(buildEnvironment);
 }
コード例 #15
0
ファイル: GitSCM.java プロジェクト: anagri/Hudson-GIT-plugin
  @Override
  public boolean checkout(
      final AbstractBuild build,
      Launcher launcher,
      final FilePath workspace,
      final BuildListener listener,
      File changelogFile)
      throws IOException, InterruptedException {

    listener
        .getLogger()
        .println(
            "Checkout:"
                + workspace.getName()
                + " / "
                + workspace.getRemote()
                + " - "
                + workspace.getChannel());
    listener.getLogger().println("Using strategy: " + choosingStrategy);

    final String projectName = build.getProject().getName();
    final int buildNumber = build.getNumber();
    final String gitExe = getDescriptor().getGitExe();

    final String buildnumber = "hudson-" + projectName + "-" + buildNumber;

    final BuildData buildData = getBuildData(build.getPreviousBuild(), true);

    if (buildData != null && buildData.lastBuild != null) {
      listener.getLogger().println("Last Built Revision: " + buildData.lastBuild.revision);
    }

    final EnvVars environment = build.getEnvironment(listener);

    final String singleBranch = getSingleBranch(build);

    Revision tempParentLastBuiltRev = null;

    if (build instanceof MatrixRun) {
      MatrixBuild parentBuild = ((MatrixRun) build).getParentBuild();
      if (parentBuild != null) {
        BuildData parentBuildData = parentBuild.getAction(BuildData.class);
        if (parentBuildData != null) {
          tempParentLastBuiltRev = parentBuildData.getLastBuiltRevision();
        }
      }
    }

    final Revision parentLastBuiltRev = tempParentLastBuiltRev;

    final Revision revToBuild =
        workspace.act(
            new FileCallable<Revision>() {
              private static final long serialVersionUID = 1L;

              public Revision invoke(File localWorkspace, VirtualChannel channel)
                  throws IOException {
                FilePath ws = new FilePath(localWorkspace);
                listener
                    .getLogger()
                    .println(
                        "Checkout:"
                            + ws.getName()
                            + " / "
                            + ws.getRemote()
                            + " - "
                            + ws.getChannel());

                IGitAPI git = new GitAPI(gitExe, ws, listener, environment);

                if (git.hasGitRepo()) {
                  // It's an update

                  listener.getLogger().println("Fetching changes from the remote Git repository");

                  for (RemoteConfig remoteRepository : getRepositories()) {
                    fetchFrom(git, localWorkspace, listener, remoteRepository);
                  }

                } else {
                  listener.getLogger().println("Cloning the remote Git repository");

                  // Go through the repositories, trying to clone from one
                  //
                  boolean successfullyCloned = false;
                  for (RemoteConfig rc : remoteRepositories) {
                    try {
                      git.clone(rc);
                      successfullyCloned = true;
                      break;
                    } catch (GitException ex) {
                      listener.error(
                          "Error cloning remote repo '%s' : %s", rc.getName(), ex.getMessage());
                      if (ex.getCause() != null) {
                        listener.error("Cause: %s", ex.getCause().getMessage());
                      }
                      // Failed. Try the next one
                      listener.getLogger().println("Trying next repository");
                    }
                  }

                  if (!successfullyCloned) {
                    listener.error("Could not clone from a repository");
                    throw new GitException("Could not clone");
                  }

                  // Also do a fetch
                  for (RemoteConfig remoteRepository : getRepositories()) {
                    fetchFrom(git, localWorkspace, listener, remoteRepository);
                  }

                  if (git.hasGitModules()) {
                    git.submoduleInit();
                    git.submoduleUpdate();
                  }
                }

                if (parentLastBuiltRev != null) return parentLastBuiltRev;

                IBuildChooser buildChooser = createBuildChooser(git, listener, buildData);

                Collection<Revision> candidates =
                    buildChooser.getCandidateRevisions(false, singleBranch);
                if (candidates.size() == 0) return null;
                return candidates.iterator().next();
              }
            });

    if (revToBuild == null) {
      // getBuildCandidates should make the last item the last build, so a re-build
      // will build the last built thing.
      listener.error("Nothing to do");
      return false;
    }
    listener.getLogger().println("Commencing build of " + revToBuild);
    environment.put(GIT_COMMIT, revToBuild.getSha1String());
    Object[] returnData; // Changelog, BuildData

    if (mergeOptions.doMerge()) {
      if (!revToBuild.containsBranchName(mergeOptions.getRemoteBranchName())) {
        returnData =
            workspace.act(
                new FileCallable<Object[]>() {
                  private static final long serialVersionUID = 1L;

                  public Object[] invoke(File localWorkspace, VirtualChannel channel)
                      throws IOException {
                    IGitAPI git =
                        new GitAPI(gitExe, new FilePath(localWorkspace), listener, environment);

                    IBuildChooser buildChooser = createBuildChooser(git, listener, buildData);

                    // Do we need to merge this revision onto MergeTarget

                    // Only merge if there's a branch to merge that isn't
                    // us..
                    listener
                        .getLogger()
                        .println(
                            "Merging " + revToBuild + " onto " + mergeOptions.getMergeTarget());

                    // checkout origin/blah
                    ObjectId target = git.revParse(mergeOptions.getRemoteBranchName());
                    git.checkout(target.name());

                    try {
                      git.merge(revToBuild.getSha1().name());
                    } catch (Exception ex) {
                      listener
                          .getLogger()
                          .println(
                              "Branch not suitable for integration as it does not merge cleanly");

                      // We still need to tag something to prevent
                      // repetitive builds from happening - tag the
                      // candidate
                      // branch.
                      git.checkout(revToBuild.getSha1().name());

                      git.tag(buildnumber, "Hudson Build #" + buildNumber);

                      buildChooser.revisionBuilt(revToBuild, buildNumber, Result.FAILURE);

                      return new Object[] {null, buildChooser.getData()};
                    }

                    if (git.hasGitModules()) {
                      git.submoduleUpdate();
                    }

                    // Tag the successful merge
                    git.tag(buildnumber, "Hudson Build #" + buildNumber);

                    StringBuilder changeLog = new StringBuilder();

                    if (revToBuild.getBranches().size() > 0)
                      listener
                          .getLogger()
                          .println("Warning : There are multiple branch changesets here");

                    try {
                      for (Branch b : revToBuild.getBranches()) {
                        Build lastRevWas =
                            buildData == null ? null : buildData.getLastBuildOfBranch(b.getName());
                        if (lastRevWas != null) {
                          changeLog.append(
                              putChangelogDiffsIntoFile(
                                  git,
                                  b.name,
                                  lastRevWas.getSHA1().name(),
                                  revToBuild.getSha1().name()));
                        }
                      }
                    } catch (GitException ge) {
                      changeLog.append("Unable to retrieve changeset");
                    }

                    Build buildData = buildChooser.revisionBuilt(revToBuild, buildNumber, null);
                    GitUtils gu = new GitUtils(listener, git);
                    buildData.mergeRevision = gu.getRevisionForSHA1(target);

                    // Fetch the diffs into the changelog file
                    return new Object[] {changeLog.toString(), buildChooser.getData()};
                  }
                });
        BuildData returningBuildData = (BuildData) returnData[1];
        build.addAction(returningBuildData);
        return changeLogResult((String) returnData[0], changelogFile);
      }
    }

    // No merge

    returnData =
        workspace.act(
            new FileCallable<Object[]>() {
              private static final long serialVersionUID = 1L;

              public Object[] invoke(File localWorkspace, VirtualChannel channel)
                  throws IOException {
                IGitAPI git =
                    new GitAPI(gitExe, new FilePath(localWorkspace), listener, environment);
                IBuildChooser buildChooser = createBuildChooser(git, listener, buildData);

                // Straight compile-the-branch
                listener.getLogger().println("Checking out " + revToBuild);
                git.checkout(revToBuild.getSha1().name());

                // if( compileSubmoduleCompares )
                if (doGenerateSubmoduleConfigurations) {
                  SubmoduleCombinator combinator =
                      new SubmoduleCombinator(git, listener, localWorkspace, submoduleCfg);
                  combinator.createSubmoduleCombinations();
                }

                if (git.hasGitModules()) {
                  git.submoduleInit();
                  git.submoduleSync();

                  // Git submodule update will only 'fetch' from where it
                  // regards as 'origin'. However,
                  // it is possible that we are building from a
                  // RemoteRepository with changes
                  // that are not in 'origin' AND it may be a new module that
                  // we've only just discovered.
                  // So - try updating from all RRs, then use the submodule
                  // Update to do the checkout

                  for (RemoteConfig remoteRepository : getRepositories()) {
                    fetchFrom(git, localWorkspace, listener, remoteRepository);
                  }

                  // Update to the correct checkout
                  git.submoduleUpdate();
                }

                // Tag the successful merge
                git.tag(buildnumber, "Hudson Build #" + buildNumber);

                StringBuilder changeLog = new StringBuilder();

                int histories = 0;

                try {
                  for (Branch b : revToBuild.getBranches()) {
                    Build lastRevWas =
                        buildData == null ? null : buildData.getLastBuildOfBranch(b.getName());

                    if (lastRevWas != null) {
                      listener.getLogger().println("Recording changes in branch " + b.getName());
                      changeLog.append(
                          putChangelogDiffsIntoFile(
                              git,
                              b.name,
                              lastRevWas.getSHA1().name(),
                              revToBuild.getSha1().name()));
                      histories++;
                    } else {
                      listener.getLogger().println("No change to record in branch " + b.getName());
                    }
                  }
                } catch (GitException ge) {
                  changeLog.append("Unable to retrieve changeset");
                }

                if (histories > 1)
                  listener
                      .getLogger()
                      .println("Warning : There are multiple branch changesets here");

                buildChooser.revisionBuilt(revToBuild, buildNumber, null);

                if (getClean()) {
                  listener.getLogger().println("Cleaning workspace");
                  git.clean();
                }

                // Fetch the diffs into the changelog file
                return new Object[] {changeLog.toString(), buildChooser.getData()};
              }
            });
    build.addAction((Action) returnData[1]);

    return changeLogResult((String) returnData[0], changelogFile);
  }
 /** Exposes the name/value as an environment variable. */
 @Override
 public void buildEnvVars(AbstractBuild<?, ?> build, EnvVars env) {
   env.put(name, label);
 }