private boolean evaluateResult(Result result) {
   List<SubBuild> builders = getBuilders();
   for (SubBuild subBuild : builders) {
     Result buildResult = subBuild.getResult();
     if (buildResult != null && buildResult.isWorseThan(result)) {
       return true;
     }
   }
   return false;
 }
예제 #2
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;
    }
  }