final GitSCM gitSCM = (GitSCM) scm;
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) {
listener.error("IOException publishing in git plugin");
tempEnvironment = new EnvVars();
}
String confName = gitSCM.getGitConfigNameToUse();
if (StringUtils.isNotBlank(confName)) {
tempEnvironment.put(GitConstants.GIT_COMMITTER_NAME_ENV_VAR, confName);
tempEnvironment.put(GitConstants.GIT_AUTHOR_NAME_ENV_VAR, confName);
}
String confEmail = gitSCM.getGitConfigEmailToUse();
if (StringUtils.isNotBlank(confEmail)) {
tempEnvironment.put(GitConstants.GIT_COMMITTER_EMAIL_ENV_VAR, confEmail);
tempEnvironment.put(GitConstants.GIT_AUTHOR_EMAIL_ENV_VAR, confEmail);
}
final EnvVars environment = tempEnvironment;
final FilePath workingDirectory = gitSCM.workingDirectory(workspacePath);
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 tagName = new StringBuilder()
.append(GitConstants.INTERNAL_TAG_NAME_PREFIX)
.append(GitConstants.HYPHEN_SYMBOL)
.append(projectName)
.append(GitConstants.HYPHEN_SYMBOL)
.append(buildNumber)
.toString();
git.deleteTag(tagName);
// And add the success / fail state into the tag.
tagName += "-" + buildResult.toString();
git.tag(tagName, GitConstants.INTERNAL_TAG_COMMENT_PREFIX + buildNumber);
PreBuildMergeOptions mergeOptions = gitSCM.getMergeOptions();
if (mergeOptions.doMerge() && buildResult.isBetterOrEqualTo(
Result.SUCCESS)) {
RemoteConfig remote = mergeOptions.getMergeRemote();
listener.getLogger().println(new StringBuilder().append("Pushing result ")
.append(tagName)
.append(" to ")