At times, we might have stages that are fetching data and stages that are utilizing this data. In a case like this, we would want the latter stage to wait until the former stage is completed.

We can do this by using the needs keyword.

What is the needs keyword?

needs is a keyword provided while defining the stage. needs accepts an array of strings where every string should be a valid stage ID in the pipeline.

When needs is specified at any stage, this stage will not execute until all the stages passed in the needs field have completed execution.

Following is how needs can be defined for a stage:

- id: some independent stage
  script: "function handleRequest() {return {'customField': 'just some text'}}"
- id: dependent stage
    - some independent stage
  use: reactivesearchQuery

Note that in the above case, we have two stages defined:

  • some independent stage
  • dependent stage

As the ID's suggest, the dependent stage requires the stage with ID some independent stage in order to run. When this is the case, the needs keyword will make sure that all the stages mentioned in the needs field are executed first before the stage itself gets executed.

A stage that depends on multiple other stages

Let's say we want to define a stage that depends on multiple other stages, we can do that by the needs field as well.

Let's say our stage needs stages with ID's stage_1, stage_2 and stage_3 to execute before it actually executes.

We can make this happen with the following syntax:

- id: final stage
    - stage_1
    - stage_2
    - stage_3