是否可以跨多个 gitlab 管道"lock"一组作业



我有多个作业使用单个外部资源(服务器(。第一个作业将应用部署到环境,第二个作业在此环境中执行测试,第三个作业在此环境中执行集成测试。

我知道有资源组选项。但它只锁定了工作。如果两个管道同时运行,我需要从第一个管道执行job1job2job3,并且只有当第一个管道释放资源 - 第二个管道可以启动jobs1-3时。有没有办法实现这一目标?还有其他工作正在管道中 - 它们应该同时工作。

这应该可以在 13.9 中使用进程模式 =oldest_firstresource_group来实现。详情请见: https://docs.gitlab.com/ee/ci/resource_groups/index.html#pipeline-level-concurrency-control-with-cross-projectparent-child-pipelines

我认为它可以通过needsresource_group关键字以及gitlab API来实现。

每个作业都会收到它所属的管道 ID 作为predefined-variable。如果使用 gitlab api,则可以查看管道中其他作业的状态。如果你能使用这个状态,needsresource_group关键词,我认为你可以实现你的意图。有关更多详细信息,请参阅以下代码的说明及其注释。

stages:
- ready
- build
job1:
stage: build
needs: [starting_signal]
script: 
- sleep 10 && echo "job1"
job2:
stage: build
needs: [starting_signal]
script:
- sleep 20 && echo "job2"
job3:
stage: build
needs: [starting_signal]
script:
- sleep 30 && echo "job3"
starting_signal:
stage: ready
script:
- # TODO: You need to implement it using the GitLab API.
- # The starting condition for "job1-3" is
- # that this `starting_signal` job finished successfully.
- # And the condition that ends with the success of this job
- # is that `traffic_light` becomes running.
traffic_light: 
stage: ready
resource_group: traffic_light
script:
- # TODO: You need to implement it using the GitLab API.
- # The end condition for `traffic_light` is
- # the end of job1-3 execution.
- # In other words, this job must be checked and waited
- # through gitlab api until job 1,2,3 is finished.
- # Since this job locks the execution of a `traffic_light` job
- # in another pipeline, the `starting_signal` job in another 
- # pipeline does not succeed.

(我没有自己测试过,所以这种方法需要复习。

参考资料:

  • https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
  • https://docs.gitlab.com/ee/ci/yaml/#needs
  • https://docs.gitlab.com/ee/api/jobs.html#list-pipeline-jobs

为作业1-3设置专用运行器。

  1. 设置一个具有唯一标签的新运行器,例如"jobs-1-2-3",并将选项concurrent设置为1

  2. 将唯一标签(例如"jobs-1-2-3"(添加到相关作业中。

    job1:
    tags:
    - jobs-1-2-3
    job2:
    tags:
    - jobs-1-2-3
    job3:
    tags:
    - jobs-1-2-3
    

恕我直言,这更省力,更可靠。

最新更新