From 25173ef6eda6cfc3dfa3f2315e46ae536cba95fb Mon Sep 17 00:00:00 2001 From: Mike Bonnet Date: Feb 18 2019 21:50:37 +0000 Subject: add a Builder class The Builder class and build() method will launch a build from a set of objects or the name of a BuildConfig. It will optionally wait until the build completes, streaming the build logs to the console, if available. --- diff --git a/src/com/redhat/c3i/util/Builder.groovy b/src/com/redhat/c3i/util/Builder.groovy new file mode 100644 index 0000000..e6cfa28 --- /dev/null +++ b/src/com/redhat/c3i/util/Builder.groovy @@ -0,0 +1,62 @@ +// Runs OpenShift builds and waits until they've completed. +// Mike Bonnet (mikeb@redhat.com), 2019-02-15 + +package com.redhat.c3i.util + +def build(List models, boolean wait, Object... args) { + openshift.withCluster() { + def objects = openshift.apply(models) + def bc = objects.narrow("bc") + return _build(bc, wait, args) + } +} + +def build(bcname, boolean wait, Object... args) { + openshift.withCluster() { + def bc = openshift.selector(bcname) + return _build(bc, wait, args) + } +} + +def _build(bc, boolean wait, Object... args) { + def build = bc.startBuild(args as String[]) + if (wait) { + _wait(build) + } + return build.name() +} + +def wait(buildsel) { + openshift.withCluster() { + def build = openshift.selector(buildsel) + return _wait(build) + } +} + +def _wait(build) { + echo "Waiting for ${build.name()} to start..." + timeout(5) { + build.watch { + return !(it.object().status.phase in ["New", "Pending", "Unknown"]) + } + } + if (build.object().spec.strategy.type == "JenkinsPipeline") { + echo "Waiting for ${build.name()} to complete..." + build.logs("--tail=1") + timeout(60) { + build.watch { + it.object().status.phase != "Running" + } + } + } else { + echo "Following build logs..." + while (build.object().status.phase == "Running") { + build.logs("--tail=1", "--timestamps=true", "-f") + } + } + def buildobj = build.object() + if (buildobj.status.phase != "Complete") { + error "Build ${buildobj.metadata.name} ${buildobj.status.phase}" + } + return build.name() +} diff --git a/vars/c3i.groovy b/vars/c3i.groovy index b9f7247..d102d25 100644 --- a/vars/c3i.groovy +++ b/vars/c3i.groovy @@ -7,3 +7,9 @@ def cleanup(String... apps) { deployer.cleanup(apps) } } + +def build(modelsOrName, boolean wait = true, Object... args) { + def builder = new com.redhat.c3i.util.Builder() + def buildname = builder.build(modelsOrName, wait, args) + return buildname +}