All Products
Search
Document Center

Alibaba Cloud DevOps:Use the needs syntax to control job dependencies

Last Updated:Mar 31, 2025

This topic describes how to use the needs syntax to control the job execution order.

  • The YAML file of an Alibaba Cloud DevOps pipeline supports job dependency definition based on the stages.<stage_id>.jobs.<job_id>.needs syntax.

  • Typical scenario: app1 and app2 must be built and deployed together. The app1 build job depends on the completion of app1 unit testing and app1 code scanning jobs. The app2 build job depends on the completion of app2 unit testing and app2 code scanning jobs. To improve efficiency, app1 and app2 build jobs can be run concurrently. The deployment of app2 depends on the completion of app1 deployment.

  • The following figure provides an example of using YAML to build and deploy app1 and app2.

    36351c343af4d1893f29be7a99a8c829

    sources: 
      my_repo1:
        type: gitSample
        name: app1 code source
        endpoint: https://atomgit.com/flow-example/spring-boot.git
        branch: master
      my_repo2:
        type: gitSample
        name: app2 code source
        endpoint: https://atomgit.com/flow-example/node-expressjs.git
        branch: master
    defaultWorkspace: my_repo1
    stages:
      build_stage:
        name: Build
        jobs:
          test_job1:
            name: app1 unit testing
            steps:
              command_step:
                name: Execute the command
                step: Command
                with:
                  run: |
                    echo This is build job...
          scan_job1:
            name: app1 code scanning
            steps:
              command_step:
                name: Execute the command
                step: Command
                with:
                  run: |
                    echo This is build job...
          test_job2:
            name: app2 unit testing
            steps:
              command_step:
                name: Execute the command
                step: Command
                with:
                  run: |
                    echo This is build job...
          scan_job2:
            name: app2 code scanning
            steps:
              command_step:
                name: Execute the command
                step: Command
                with:
                  run: |
                    echo This is build job...
          build_job1:
            name: app1 build
            # Declares dependent jobs. app1 build depends on the completion of both the app1 unit testing and code scanning jobs.
            needs: 
              - test_job1
              - scan_job1
            steps:
              command_step:
                name: Execute the command
                step: Command
                with:
                  run: |
                    echo This is build job...
          build_job2:
            name: app2 build
            # Declare dependent jobs. app2 build depends on the completion of both the app2 unit testing and code scanning jobs.
            needs: 
              - test_job2
              - scan_job2
            steps:
              command_step:
                name: Execute the command
                step: Command
                with:
                  run: |
                    echo This is build job...
      deploy_stage:
        name: Deployment
        jobs:
          deploy_job1:
            name: app1 deployment
            steps:
              command_step:
                name: Execute the command
                step: Command
                with:
                  run: echo This is deploy env 1...
          deploy_job2:
            name: app2 deployment
            # Declare dependent jobs. app2 deployment depends on the completion of the app1 deployment job.
            needs: deploy_job1
            steps:
              command_step:
                name: Execute the command
                step: Command
                with:
                  run: echo This is deploy env 2...