.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 ")
.append(mergeOptions.getMergeTarget())
.append(" branch of ")
.append(remote.getName())
.append(" repository")
.toString());
git.push(remote, "HEAD:" + mergeOptions.getMergeTarget());
// } else {
//listener.getLogger().println("Pushing result " + buildnumber + " to origin repository");
//git.push(null);
}
return true;
}
});
} catch (Throwable e) {
listener.error("Failed to push merge to origin repository: " + e.getMessage());
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, "Hudson 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) {
listener.error("Failed to push tag " + tagName + " to " + targetRepo
+ ": " + e.getMessage());
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;