in src/main/java/com/microsoft/jenkins/appservice/commands/GitDeployCommand.java [51:108]
public void execute(IGitDeployCommandData context) {
try {
final PublishingProfile pubProfile = context.getPublishingProfile();
final Run run = context.getRun();
final TaskListener listener = context.getListener();
final EnvVars env = run.getEnvironment(listener);
final FilePath ws = context.getWorkspace();
if (ws == null) {
context.logError("Workspace is null");
context.setDeploymentState(DeploymentState.HasError);
return;
}
final FilePath repo = ws.child(DEPLOY_REPO);
final String gitExe = getGitExe(run, listener);
GitClient git = Git.with(listener, env)
.in(repo)
.using(gitExe)
.getClient();
git.addCredentials(pubProfile.gitUrl(), new UsernamePasswordCredentialsImpl(
CredentialsScope.SYSTEM, "", "", pubProfile.gitUsername(), pubProfile.gitPassword()));
git.clone_().url(pubProfile.gitUrl()).execute();
// Sometimes remote repository is bare and the master branch doesn't exist
Set<Branch> branches = git.getRemoteBranches();
for (Branch branch : branches) {
if (branch.getName().equals(DEPLOY_REMOTE_BRANCH)) {
git.checkout().ref(DEPLOY_BRANCH).execute();
break;
}
}
cleanWorkingDirectory(git);
final FilePath sourceDir = ws.child(Util.fixNull(context.getSourceDirectory()));
final String targetDir = Util.fixNull(context.getTargetDirectory());
copyAndAddFiles(git, repo, sourceDir, targetDir, context.getFilePath());
if (!isWorkingTreeChanged(git)) {
context.logStatus("Deploy repository is up-to-date. Nothing to commit.");
context.setDeploymentState(DeploymentState.Success);
return;
}
git.commit(env.expand(DEPLOY_COMMIT_MESSAGE));
git.push().to(new URIish(pubProfile.gitUrl())).execute();
context.setDeploymentState(DeploymentState.Success);
} catch (IOException | InterruptedException | URISyntaxException e) {
e.printStackTrace();
context.logError("Fail to deploy using Git: " + e.getMessage());
context.setDeploymentState(DeploymentState.HasError);
}
}