Search code examples
jenkinsjenkins-pipelinejenkins-groovy

Jenkins run stage repeatedly and parallely until another stage has finished


stage("Run A and Run B") {
  parallel {
    stage('A') {

    }
    stage('B') {

    }
  }
}

Above setup will run stages A and B parallely. But i am looking for a way to run B repeatedly every 1 minutes, until stage A has finished. Could someone advice on on how do i tweak this?

Alternatively, if there is some way to get the status of stage A inside stage B , that would suffice my need.

For anyone curious, my use case is : I have some test running on stage A which will take few hours, and stage B has some code to zip the latest report from the test output and push it to a remote location


Solution

  • You can achieve what you want using a global variable.
    The variable will be updated by the first parallel stage, which will use the variable to signal it has finished running, simultaneously the variable will be sampled by the second stage to verify if execution should proceed.
    Something like:

    def running = true
    
    pipeline {
        ...
        ...
        stage("Run A and Run B") {
        parallel  {
            stage('A') {
               // Run your stage A code
               // When finished update the running parameter
               running = false
            }
            stage('B') {
                while (running){
                    // Run your stage B code as long as stage A is running
                    sleep 60
                }
            }
        }
    }