Skip to content

Commit

Permalink
extract workspace even if build fails
Browse files Browse the repository at this point in the history
  • Loading branch information
suryagaddipati committed Dec 7, 2016
1 parent 3b03003 commit 3cf0685
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 260 deletions.
Expand Up @@ -43,12 +43,42 @@

public class BuildConfiguration {

private Map config;
private final Map config;

public BuildConfiguration(Map config) {
public BuildConfiguration(final Map config) {
this.config = config;
}

public static ShellCommands getCheckoutCommands(final Map<String, Object> dotCiEnvVars) {
final String gitCloneUrl = (String) dotCiEnvVars.get("DOTCI_DOCKER_COMPOSE_GIT_CLONE_URL");
final GitUrl gitRepoUrl = new GitUrl(gitCloneUrl);
final boolean isPrivateRepo = Boolean.parseBoolean((String) dotCiEnvVars.get("DOTCI_IS_PRIVATE_REPO"));
final String gitUrl = isPrivateRepo ? gitRepoUrl.getGitUrl() : gitCloneUrl;
final ShellCommands shellCommands = new ShellCommands();
shellCommands.add("chmod -R u+w . ; find . ! -path \"./deploykey_rsa.pub\" ! -path \"./deploykey_rsa\" -delete");
shellCommands.add("git init");
shellCommands.add(format("git remote add origin %s", gitUrl));

if (dotCiEnvVars.get("DOTCI_PULL_REQUEST") != null) {
if (isPrivateRepo) {

shellCommands.add(format("ssh-agent bash -c \"ssh-add -D && ssh-add \\%s/deploykey_rsa && git fetch origin '+refs/pull/%s/merge:' \"", dotCiEnvVars.get("WORKSPACE"), dotCiEnvVars.get("DOTCI_PULL_REQUEST")));
} else {
shellCommands.add(format("git fetch origin \"+refs/pull/%s/merge:\"", dotCiEnvVars.get("DOTCI_PULL_REQUEST")));
}
shellCommands.add("git reset --hard FETCH_HEAD");
} else {
if (isPrivateRepo) {

shellCommands.add(format("ssh-agent bash -c \"ssh-add -D && ssh-add \\%s/deploykey_rsa && git fetch origin %s \"", dotCiEnvVars.get("WORKSPACE"), dotCiEnvVars.get("DOTCI_BRANCH")));
} else {
shellCommands.add(format("git fetch origin %s", dotCiEnvVars.get("DOTCI_BRANCH")));
}
shellCommands.add(format("git reset --hard %s", dotCiEnvVars.get("SHA")));
}
return shellCommands;
}

public ShellCommands getBeforeRunCommandsIfPresent() {
return getShellCommands("before_run");
}
Expand All @@ -57,12 +87,13 @@ public ShellCommands getAfterRunCommandsIfPresent() {
return getShellCommands("after_run");
}

public ShellCommands getCommands(Combination combination, Map<String, Object> dotCiEnvVars) {
String dockerComposeContainerName = combination.get("script");
String projectName = (String) dotCiEnvVars.get("COMPOSE_PROJECT_NAME");
String fileName = getDockerComposeFileName();
public List<ShellCommands> getCommands(final Combination combination, final Map<String, Object> dotCiEnvVars) {
final List<ShellCommands> allCommands = new ArrayList<>();
final String dockerComposeContainerName = combination.get("script");
final String projectName = (String) dotCiEnvVars.get("COMPOSE_PROJECT_NAME");
final String fileName = getDockerComposeFileName();

ShellCommands shellCommands = new ShellCommands();
final ShellCommands shellCommands = new ShellCommands();
shellCommands.add(BuildConfiguration.getCheckoutCommands(dotCiEnvVars));

shellCommands.add(String.format("trap \"docker-compose -f %s down; exit\" PIPE QUIT INT HUP EXIT TERM", fileName));
Expand All @@ -71,58 +102,53 @@ public ShellCommands getCommands(Combination combination, Map<String, Object> do
appendCommands("before_each", shellCommands);

shellCommands.add(String.format("docker-compose -f %s pull", fileName));
if (config.get("run") != null) {
Map runConfig = (Map) config.get("run");
String dockerComposeRunCommand = getDockerComposeRunCommand(dockerComposeContainerName, fileName, runConfig);
if (this.config.get("run") != null) {
final Map runConfig = (Map) this.config.get("run");
final String dockerComposeRunCommand = getDockerComposeRunCommand(dockerComposeContainerName, fileName, runConfig);
shellCommands.add(format("export COMPOSE_CMD='%s'", dockerComposeRunCommand));
shellCommands.add(" set +e && hash unbuffer >/dev/null 2>&1 ; if [ $? = 0 ]; then set -e && unbuffer $COMPOSE_CMD ;else set -e && $COMPOSE_CMD ;fi");
}
extractWorkingDirIntoWorkSpace(dockerComposeContainerName, projectName, shellCommands);

appendCommands("after_each", shellCommands);
if (!isParallelized()) {
appendCommands("after_run", shellCommands);
}
return shellCommands;
allCommands.add(shellCommands);
allCommands.add(getCopyWorkDirIntoWorkspaceCommands(dockerComposeContainerName, projectName));
return allCommands;
}

private String getDockerComposeRunCommand(String dockerComposeContainerName, String fileName, Map runConfig) {
Object dockerComposeCommand = runConfig.get(dockerComposeContainerName);
private String getDockerComposeRunCommand(final String dockerComposeContainerName, final String fileName, final Map runConfig) {
final Object dockerComposeCommand = runConfig.get(dockerComposeContainerName);
if (dockerComposeCommand != null) {
return String.format("docker-compose -f %s run -T %s %s", fileName, dockerComposeContainerName, dockerComposeCommand);
} else {
return String.format("docker-compose -f %s run %s ", fileName, dockerComposeContainerName);
}
}

private void extractWorkingDirIntoWorkSpace(String dockerComposeContainerName, String projectName, ShellCommands shellCommands) {
if (config.get("plugins") != null) {
shellCommands.add(getCopyWorkDirIntoWorkspaceCommands(dockerComposeContainerName, projectName));
}
}

public AxisList getAxisList() {
String dockerComposeContainerName = getOnlyRun();
final String dockerComposeContainerName = getOnlyRun();
AxisList axisList = new AxisList(new Axis("script", dockerComposeContainerName));
if (isParallelized()) {
Set commandKeys = ((Map) config.get("run")).keySet();
axisList = new AxisList(new Axis("script", new ArrayList<String>(commandKeys)));
final Set commandKeys = ((Map) this.config.get("run")).keySet();
axisList = new AxisList(new Axis("script", new ArrayList<>(commandKeys)));
}
return axisList;
}

public String getOnlyRun() {
Map runConfig = (Map) config.get("run");
final Map runConfig = (Map) this.config.get("run");

return (String) runConfig.keySet().iterator().next();
}

public boolean isParallelized() {
return ((Map) config.get("run")).size() > 1;
return ((Map) this.config.get("run")).size() > 1;
}

public ShellCommands getCopyWorkDirIntoWorkspaceCommands(String run, String projectName) {
ShellCommands copyCommands = new ShellCommands();
public ShellCommands getCopyWorkDirIntoWorkspaceCommands(final String run, final String projectName) {
final ShellCommands copyCommands = new ShellCommands(false);
copyCommands.add(String.format("if docker inspect %s_%s_1 &>/dev/null ; then containerName=%s_%s_1 ; else containerName=%s_%s_run_1 ; fi ; export containerName", projectName, run, projectName, run, projectName, run));
copyCommands.add("export workingDir=`docker inspect -f '{{ .Config.WorkingDir }}' $containerName | sed -e 's|^/||g'`");
copyCommands.add("stripComponents=0 ; if [ ! \"x\" == \"x$workingDir\" ]; then set +e ; (( stripComponents+=1 )) ; set -e ; fi ; export stripComponents");
Expand All @@ -132,69 +158,39 @@ public ShellCommands getCopyWorkDirIntoWorkspaceCommands(String run, String proj
}

public List<DotCiPluginAdapter> getPlugins() {
List plugins = config.get("plugins") != null ? (List) config.get("plugins") : Collections.emptyList();
final List plugins = this.config.get("plugins") != null ? (List) this.config.get("plugins") : Collections.emptyList();
return new DotCiExtensionsHelper().createPlugins(plugins);
}

public List<PostBuildNotifier> getNotifiers() {
List notifiers = config.get("notifications") != null ? (List) config.get("notifications") : Collections.emptyList();
final List notifiers = this.config.get("notifications") != null ? (List) this.config.get("notifications") : Collections.emptyList();
return new DotCiExtensionsHelper().createNotifiers(notifiers);
}

public String getDockerComposeFileName() {
return config.get("docker-compose-file") != null ? (String) config.get("docker-compose-file") : "docker-compose.yml";
}

public static ShellCommands getCheckoutCommands(Map<String, Object> dotCiEnvVars) {
String gitCloneUrl = (String) dotCiEnvVars.get("DOTCI_DOCKER_COMPOSE_GIT_CLONE_URL");
GitUrl gitRepoUrl = new GitUrl(gitCloneUrl);
boolean isPrivateRepo = Boolean.parseBoolean((String) dotCiEnvVars.get("DOTCI_IS_PRIVATE_REPO"));
String gitUrl = isPrivateRepo ? gitRepoUrl.getGitUrl() : gitCloneUrl;
ShellCommands shellCommands = new ShellCommands();
shellCommands.add("chmod -R u+w . ; find . ! -path \"./deploykey_rsa.pub\" ! -path \"./deploykey_rsa\" -delete");
shellCommands.add("git init");
shellCommands.add(format("git remote add origin %s", gitUrl));

if (dotCiEnvVars.get("DOTCI_PULL_REQUEST") != null) {
if (isPrivateRepo) {

shellCommands.add(format("ssh-agent bash -c \"ssh-add -D && ssh-add \\%s/deploykey_rsa && git fetch origin '+refs/pull/%s/merge:' \"", dotCiEnvVars.get("WORKSPACE"), dotCiEnvVars.get("DOTCI_PULL_REQUEST")));
} else {
shellCommands.add(format("git fetch origin \"+refs/pull/%s/merge:\"", dotCiEnvVars.get("DOTCI_PULL_REQUEST")));
}
shellCommands.add("git reset --hard FETCH_HEAD");
} else {
if (isPrivateRepo) {

shellCommands.add(format("ssh-agent bash -c \"ssh-add -D && ssh-add \\%s/deploykey_rsa && git fetch origin %s \"", dotCiEnvVars.get("WORKSPACE"), dotCiEnvVars.get("DOTCI_BRANCH")));
} else {
shellCommands.add(format("git fetch origin %s", dotCiEnvVars.get("DOTCI_BRANCH")));
}
shellCommands.add(format("git reset --hard %s", dotCiEnvVars.get("SHA")));
}
return shellCommands;
return this.config.get("docker-compose-file") != null ? (String) this.config.get("docker-compose-file") : "docker-compose.yml";
}

private void appendCommands(String key, ShellCommands commands) {
ShellCommands added = getShellCommands(key);
private void appendCommands(final String key, final ShellCommands commands) {
final ShellCommands added = getShellCommands(key);
if (added != null) {
commands.add(added);
}
}

private ShellCommands getShellCommands(String key) {
Object value = config.get(key);
private ShellCommands getShellCommands(final String key) {
final Object value = this.config.get(key);
if (value == null) {
return null;
}

ShellCommands commands = new ShellCommands();
final ShellCommands commands = new ShellCommands();
if (value instanceof String) {
commands.add((String) value);
} else if (value instanceof List) {
List l = (List) value;
final List l = (List) value;

for (Object v : l) {
for (final Object v : l) {
if (!(v instanceof String)) {
throw new RuntimeException(String.format("Unexpected type: %s. Expected String for key: %s", v.getClass().getName(), key));
}
Expand All @@ -205,6 +201,6 @@ private ShellCommands getShellCommands(String key) {
}

public boolean isSkipped() {
return config.containsKey("skip");
return this.config.containsKey("skip");
}
}
Expand Up @@ -87,15 +87,24 @@ public Result runBuild(final DynamicBuild build, final BuildExecutionContext bui

@Override
public Result runSubBuild(final Combination combination, final BuildExecutionContext buildExecutionContext, final BuildListener listener) throws IOException, InterruptedException {
final ShellCommands commands = this.buildConfiguration.getCommands(combination, buildExecutionContext.getBuildEnvironmentVariables());
final List<ShellCommands> commands = this.buildConfiguration.getCommands(combination, buildExecutionContext.getBuildEnvironmentVariables());
return runCommands(commands, buildExecutionContext, listener);
}


private Result doCheckout(final Map<String, Object> buildEnvironment, final BuildExecutionContext buildExecutionContext, final BuildListener listener) throws IOException, InterruptedException {
final ShellCommands commands = BuildConfiguration.getCheckoutCommands(buildEnvironment);
return runCommands(commands, buildExecutionContext, listener);
}

private Result runCommands(final List<ShellCommands> commandList, final BuildExecutionContext buildExecutionContext, final BuildListener listener) throws IOException, InterruptedException {
final Result result = Result.SUCCESS;
for (final ShellCommands commands : commandList) {
result.combine(runCommands(commands, buildExecutionContext, listener));
}
return result;
}

private Result runCommands(final ShellCommands commands, final BuildExecutionContext buildExecutionContext, final BuildListener listener) throws IOException, InterruptedException {
if (commands == null) {
return Result.SUCCESS;
Expand Down

0 comments on commit 3cf0685

Please sign in to comment.