Drone CI 并行任务

并行执行

首先,使用 depends_on 参数指定当前任务所依赖的上一个任务,ForEach 插件可将数组中的每个元素当作独立步骤执行:

kind: pipeline
name: default

steps:
  - name: build
    image: golang
    commands:
      - go build
      
  - name: test
    image: golang
    depends_on: 
      - build
    commands:
      - go test

  - name: deploy
    image: plugins/deploy
    settings:
      host: ![img](file:///C:\Users\JJM\AppData\Roaming\Tencent\QQTempSys\%W@GJ$ACOF(TYDYECOKVDYB.png)production.example.com

  - name: parallel-step-1
    image: busybox
    depends_on:
      - deploy
    foreach:
      from:
        kind: config
        data:
          FOO: bar1
          BAZ: qux1
      action:
        image: alpine/git
        clone:
          disable_auth_config: true
          depth: 1
          url: ![img](file:///C:\Users\JJM\AppData\Roaming\Tencent\QQTempSys\[5UQ[BL(6~BS2JV6W}N6[%S.png)https://github.com/octocat/hello-world.git
        commands:
          - echo $(pwd)

  - name: parallel-step-2
    image: busybox
    depends_on:
      - deploy
    foreach:
      from:
        kind: config
        data:
          FOO: bar2
          BAZ: qux2
      action:
        image: alpine/git     
        clone:
          disable_auth_config: true        
          depth: 1       
          url: ![img](file:///C:\Users\JJM\AppData\Roaming\Tencent\QQTempSys\[5UQ[BL(6~BS2JV6W}N6[%S.png)https://github.com/octocat/hello-world.git   
        commands:
          - echo $(pwd)        

在最后两个步骤中,我们使用了 depends_on 进行了并行执行。其中 parallel-step-1parallel-step-2 步骤从 deploy 步骤继承依赖(需要等待前面步骤执行完),但是不等待彼此。也就是说,这两个步骤将同时启动,并行运行。此处参数 foreach 表示将数组中的所有元素分别当做一个新的独立步骤,并在多个副本之间进行进程通信和资源共享。

通过在 YAML 文件中明确定义这些步骤及其间的依赖关系,防止在相互依赖的任务之间产生死锁或循环错误。

posted @ 2023-03-10 11:40  wosperry  阅读(169)  评论(0编辑  收藏  举报