0

I want to monitor a kubernetes pod created after redeploy task and once it is completed, I want to check the liquibase logs. If it is successful, I want to delete the job. How can I achieve this in gradle? I don't want to undeploy immediately after redeploy. So doLast is not an option. The following code doesn't keeps printing ob has not completed yet

task undeployAfterCompleted() {
    group "kubernetes"
    description "Undeploy the liquibase update job after completion"

    def output = new ByteArrayOutputStream()
    commandLine "bash", "-c", "kubectl get po -n pbr | grep 'liquibase'  | awk '{ print \$3 }'"

    while(!output.toString().equals('Completed')) {
        sleep(5 * 1000)
        println "Job has not completed yet."
        commandLine "bash", "-c", "kubectl get po -n pbr | grep 'liquibase'  | awk '{ print \$3 }'"
    }

    tasks.undeploy.execute()
}
tkruse
  • 10,222
  • 7
  • 53
  • 80
javagirl
  • 41
  • 1
  • 7
  • Does this answer your question? [Run gradle task X after "connectedAndroidTest" task is successful](https://stackoverflow.com/questions/34797260/run-gradle-task-x-after-connectedandroidtest-task-is-successful) – tkruse Dec 04 '19 at 23:43
  • No..I am not depending on one task to run another. I am solely checking the result of a kubectl command to execute a task called undeploy. – javagirl Dec 04 '19 at 23:57
  • My question is how to write a while loop to keep listening to the result of a kubectl command executed every 10 mins or so – javagirl Dec 04 '19 at 23:59
  • so I think first all functional code should go into doLast (or doFirst), because you need to run this in the gradle execution phase, not the configuration phase. Then I am not sure which part of your current code is supposed to update the output variable. Maybe exec would help:https://stackoverflow.com/questions/11093223 – tkruse Dec 05 '19 at 00:07

1 Answers1

1

I added a doLast block to achieve this:

doLast {
    String status = getStatus()
    println status.length()
    while (status != "Completed") {
        println "not"
        sleep(5 * 1000)

        if (getStatus() == "Completed") {
            println "did"
            break
        }
    }
    tasks.undeploy.execute()
}
javagirl
  • 41
  • 1
  • 7