private BuildCommand cmdClone(String workDir, int cloneDepth) { ArrayList<String> cloneArgs = new ArrayList<>(); cloneArgs.add("clone"); cloneArgs.add("--no-checkout"); cloneArgs.add(format("--branch=%s", material.branchWithDefault())); if (material.isShallowClone()) { cloneArgs.add(format("--depth=%s", String.valueOf(cloneDepth))); } cloneArgs.add(material.getUrlArgument().forCommandline()); cloneArgs.add(workDir); return exec("git", cloneArgs.toArray(new String[cloneArgs.size()])) .setTest(isNotRepository(workDir)); }
private BuildCommand cleanWorkingDir(String workDir) { return compose( cleandir(workDir).setTest(isNotRepository(workDir)), cleandir(workDir).setTest(isRepoUrlChanged(workDir)), cleandir(workDir).setTest(isBranchChanged(workDir)), material.isShallowClone() ? noop() : cleandir(workDir).setTest(isShallow(workDir)) ).setTest(test("-d", workDir)); }
private BuildCommand unshallowIfNeeded(String workingDir, Revision revision, Integer[] steps) { if (steps.length == 0) { return noop(); } int depth = steps[0]; return compose( compose( echo("[GIT] Unshallowing repository with depth %d", depth), exec("git", "fetch", "origin", format("--depth=%d", depth)).setWorkingDirectory(workingDir), unshallowIfNeeded(workingDir, revision, Arrays.copyOfRange(steps, 1, steps.length)) ).setTest(revisionNotExists(workingDir, revision)) ).setTest(isShallow(workingDir)); }
public BuildCommand updateTo(String baseDir, RevisionContext revisionContext) { Revision revision = revisionContext.getLatestRevision(); String workingDir = material.workingdir(new File(baseDir)).getPath(); UrlArgument url = material.getUrlArgument(); return compose( echoWithPrefix(format("Start updating %s at revision %s from %s", material.updatingTarget(), revision.getRevision(), url.forDisplay())), secret(url.forCommandline(), url.forDisplay()), secret(material.getPassword(), "*********************"), cleanupAndUpdate(workingDir, revision).setTest(shouldDoCleanupAndUpdate(workingDir)), freshCheckout(workingDir, revision).setTest(isNotRepository(workingDir)), freshCheckout(workingDir, revision).setTest(test("-nd", workingDir)), freshCheckout(workingDir, revision).setTest(repoUrlChanged(workingDir)), echoWithPrefix(format("Done.\n")) ); }
private BuildCommand cloneIfNeeded(String workDir, int cloneDepth) { return compose( mkdirs(workDir).setTest(test("-nd", workDir)), cleanWorkingDir(workDir), cmdClone(workDir, cloneDepth)); }
private BuildCommand cleanupUnversionedFiles(String workingDir) { return compose( echo("[GIT] Cleaning all unversioned files in working copy"), exec("git", "submodule", "foreach", "--recursive", "git", "clean", "-fdd") .setTest(hasSubmodules(workingDir)), exec("git", "clean", "-dff")) .setWorkingDirectoryRecursively(workingDir); }
private BuildCommand refreshWorkingDir() { return BuildCommand.compose( cleanWorkingDir(), mkdirs(workingDirectory()).setTest(test("-nd", workingDirectory()))); }
@Test public void testDirectoryExistsBeforeMkdir() { File dir = new File(sandbox, "foo"); runBuild(mkdirs("foo"), Passed); runBuild(mkdirs("foo").setTest(test("-nd", dir.getPath())), Passed); assertThat(new File(sandbox, "foo").isDirectory(), is(true)); } }
private BuildCommand cleanWorkingDir() { if (!assignment.shouldCleanWorkingDir()) { return noop(); } return BuildCommand.compose( cleandir(workingDirectory()), echoWithPrefix("Cleaning working directory \"$%s\" since stage is configured to clean working directory", workingDirectory()) ).setTest(test("-d", workingDirectory())); }
@Override public void run() { buildSession.build(compose( echo("after sleep").setTest(execSleepScript(50)))); } });
@Test public void shouldNotFailBuildWhenTestCommandFail() { runBuild(echo("foo").setTest(fail("")), Passed); assertThat(statusReporter.singleResult(), is(Passed)); }
@Test public void shouldNotFailBuildWhenTestEqWithComposedCommandOutputFail() { runBuild(echo("foo").setTest(test("-eq", "42", compose(fail("42")))), Passed); assertThat(statusReporter.singleResult(), is(Passed)); assertThat(console.output(), containsString("foo")); } }
@Test public void shouldNotFailBuildWhenComposedTestCommandFail() { runBuild(echo("foo").setTest(compose(echo(""), fail(""))), Passed); assertThat(statusReporter.singleResult(), is(JobResult.Passed)); }
private BuildCommand updateSubmodules(String workingDir) { return compose( echo("[GIT] Removing modified files in submodules"), exec("git", "submodule", "foreach", "--recursive", "git", "checkout", "."), echo("[GIT] Updating git sub-modules"), exec("git", "submodule", "init"), exec("git", "submodule", "sync"), exec("git", "submodule", "foreach", "--recursive", "git", "submodule", "sync"), exec("git", "submodule", "update"), echo("[GIT] Git sub-module status"), exec("git", "submodule", "status")) .setTest(hasSubmodules(workingDir)) .setWorkingDirectoryRecursively(workingDir); }
@Test public void encodeDecode() { BuildCommand bc = BuildCommand.compose(new BuildCommand("bar1", map("arg1", "1", "arg2", "2")), BuildCommand.compose(new BuildCommand("barz"))); bc.setRunIfConfig("any"); bc.setTest(new BuildCommand("t", map("k1", "v1"))); bc.setOnCancel(BuildCommand.compose(BuildCommand.echo("foo"), BuildCommand.echo("bar"))); assertThat(MessageEncoding.decodeData(MessageEncoding.encodeData(bc), BuildCommand.class), is(bc)); } }