简体   繁体   中英

Wait for specific stage to complete in Jenkins parallel stages

I have stages running in parallel. Each stage has sub-stages.

stage("Prepare VMs") {
  parallel {
    stage("Prepare Database VM") {
      stage("Install Database") {...}
      stage("Upgrade Database") {...}
    }
    stage("Prepare Server VM") {
      stage("Install Server") {...}
      stage("Start Server") {...}
    }
    stage("Prepare Client VM") {
      stage("Install Client") {...}
    }
  }
}

As the above is now, the sub-stages of each stage run sequentially, but can run in parallel with the other stage.

What I want is for Start Server to wait until Upgrade Database finish. In other words, Install Server can run while Install Database or Upgrade Database run, but Start Server has to wait for Upgrade Database to finish. Is this possible?

My initial approach was to use an AtomicBoolean , but my company's Jenkins does not permit it.

final AtomicBoolean DATABASE_READY = new AtomicBoolean(false);

stage("Prepare VMs") {
  parallel {
    stage("Prepare Database VM") {
      stage("Install Database") {...}
      // At the end of the stage, it sets DATABASE_READY to true.
      stage("Upgrade Database") {...}
    }
    stage("Prepare Server VM") {
      stage("Install Server") {...}
      // Waits for DATABASE_READY.
      stage("Start Server") {...}
    }
    stage("Prepare Client VM") {
      stage("Install Client") {...}
    }
  }
}

Nor can I use CountDownLatch or similar.

Let the continuous stages take care of that for you.

stage("Prepare VMs 1") {
  parallel {
    stage("Prepare Database VM") {
      stage("Install Database") {...}
    }
    stage("Prepare Server VM") {
      stage("Install Server") {...}
    }
  }
}
stage("Prepare VMs 2") {
  // This will only run when the previous group has finished.
  parallel {
    stage("Prepare Database VM") {
      stage("Upgrade Database") {...}
    }
    stage("Prepare Server VM") {
      stage("Start Server") {...}
    }
  }
}

I still can't understand why you can't use sequential stages for your use case :) But anyway here is how you can achieve this with lockable resources. You can read more on the plugin here and here .

pipeline {
    agent any
    stages {
        stage("Prepare VMs") {
            parallel {
                stage("Prepare Database VM") {
                    stages {
                        stage("Install Database") {
                            steps {
                                echo "Install Database"
                            }
                        }
                        stage("Upgrade Database") {
                            steps {
                                lock('lock') {
                                    echo "Upgrade Database start"
                                    sleep 30
                                    echo "Upgrade Database End"
                                }
                            }
                        }
                    }
                }

                stage("Prepare Server VM") {
                    stages {
                        stage("Install Server") {
                            steps {
                                echo "Install Server"
                            }
                        }
                        stage("Start Server") {
                             steps {
                                lock('lock') {
                                    echo "Starting Server"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM